SetItemState doesnt mark automatically - c++

Ive made a search function for my List Control in Report View in my MFC Dialog. It looks like this
m_List.SetItemState((m_List.FindItem(&Finde)),LVIS_SELECTED,LVIS_SELECTED );
It searches the Content that is in the Variable Finde and marks it. Now it should mark the row. But I first have to click somewhere in the program. It doesnt mark the row directly after the function gets called.
Can anyone help me?
Here is the full function
LVFINDINFO Finde;
Finde.flags = LVFI_PARTIAL|LVFI_STRING;
Finde.psz = _T("Siffert");
if ((m_List.FindItem(&Finde)) != -1)
{
m_List.SetItemState((m_List.FindItem(&Finde)),LVIS_SELECTED,LVIS_SELECTED );
//m_List.SetSelectionMark((m_List.FindItem(&Finde)));
}
else
{
MessageBox(_T("No Results"));
}

You need to use the style LVS_SHOWSELALWAYS
Otheriwse the selection is only shown when the control has the focus and is active.
EDIT: Also keep in mind that there is also a LVIS_FOCUSED style, that also forces scrolling to this item.

Related

CMFCOutlookBarTabCtrl::SetActiveTab not working

I have added a CMFCOutlookBar control to a dialog. This outlookbar contains some 12 trees.
As per following link https://msdn.microsoft.com/en-us/library/bb983453.aspx
we can set active tab (in my case tree control) of our wish.
but it doesn't seems to work.
as per above link this function returns non zero value on success. Indeed it is returning 1 when i used it to set tree of my choice. but visually it's not changed.
can someone help me?
Problem solved.
CMFCOutlookBarTabCtrl::SetActiveTab() only works after window has been displayed.
I guess this is because CMFCOutlookBar stores it's previous state to registory and reloads on next run. And this overrides changes made by SetActiveTab(), if we use it before displaying of window.
I had the same problem, and you are correct that on load the tab gets set to the last session value - actually it seems to get set several times during the load process - some of them seem to correspond to each time a tab is added, and then the last time it is called seems to be the tab from the previous session.
The solution is to set the value once the window is ready to be shown. This can be done by overriding the OnShowWindow callback on the view which contains the tab bar.
In my case the tab bar is added in a view called MainFrame, which has a member variable CMFCOutlookBarTabCtrl* m_pOutlookBar; which is initialised in the OnCreate callback.
I can then correctly set the tab by overriding OnShowWindow to contain the following:
void MainFrame::OnShowWindow(BOOL bShow, UINT nStatus)
{
CFrameWndEx::OnShowWindow(bShow, nStatus);
if ((m_pOutlookBar != NULL) && bShow) {
//When the tab bar is shown, select the correctview
for (int tabIdx = 0; tabIdx < m_pOutlookBar->GetTabsNum(); tabIdx++) {
CString requiredLabel;
CString thisLabel;
requiredLabel.LoadString(IDS_OF_TAB); //The ID of the tab wanted
m_pOutlookBar->GetTabLabel(tabIdx,thisLabel);
if (requiredLabel.Compare(thisLabel) == 0) {
//If the tab label matches the one required
m_pOutlookBar->SetActiveTab(tabIdx); //set it as the active one.
break; //done.
}
}
}
}

wxGrid GetSelectedCells returns empty array

I want to delete selected rows when the use click on a button.
so far the code looks like that:
this->grid_ = new wxGrid(parent, ...)
this->grid_->SetSelectionMode(wxGrid::wxGridSelectCells);
// Later, whene the button is clicked
this->grid_->SetFocus();
wxGridCellCoordsArray wx_cells = this->grid_->GetSelectedCells();
The problem is that the wx_cells variable is always empty whatever I select.
I tried with GetSelectedRows with no success.
I've added SetFocus and SetSelectionMode hoping it would help but it did not.
How can I get this to work ?
What version of wxgrid are you using? There seems to be an issue with old wxgrid that it always return empty when calling GetSelectedCells. Perhaps you can refer to http://forums.wxwidgets.org/viewtopic.php?t=6335 to try if it fits in your case.

CTreeCtrl setting selected item programmatically

I want to programmatically set the state of a tree ctrl item to be selected and then process it elsewhere. I want to do this to reuse the code that is called when the user clicks it. I try this but its failing, why?
// somewhere in the code
m_tree.SetItemState(hItemToBeSelected, TVIS_SELECTED, TVIS_SELECTED);
CommonFunction();
// elsewhere
CommonFunction()
{
HTREEITEM h = m_tree.GetSelectedItem();// this returns NULL.
}
How can I do this?
You want to use SelectItem instead of SetItemState: http://msdn.microsoft.com/en-us/library/w8hy20sy(v=VS.100).aspx

Using images in QListWidget, is this possible?

I am using QT to create a chat messenger client. To display the list of online users, I'm using a QListWidget, as created like this:
listWidget = new QListWidget(horizontalLayoutWidget);
listWidget->setObjectName("userList");
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(listWidget->sizePolicy().hasHeightForWidth());
listWidget->setSizePolicy(sizePolicy1);
listWidget->setMinimumSize(QSize(30, 0));
listWidget->setMaximumSize(QSize(150, 16777215));
listWidget->setBaseSize(QSize(100, 0));
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Users are shown by constantly refreshing the list, like this: (Note: There are different channels, with different userlists, so refreshing it is the most efficient thing to do, as far as I know.)
void FMessenger::refreshUserlist()
{
if (currentPanel == 0)
return;
listWidget = this->findChild<QListWidget *>(QString("userList"));
listWidget->clear();
QList<FCharacter*> charList = currentPanel->charList();
QListWidgetItem* charitem = 0;
FCharacter* character;
foreach(character, charList)
{
charitem = new QListWidgetItem(character->name());
// charitem->setIcon(QIcon(":/Images/status.png"));
listWidget->addItem(charitem);
}
}
This has always worked perfectly. The line that I commented out is the one I have problems with: my current goal is to be able to display a user's online status with an image, which represents whether they are busy, away, available, etc. Using setIcon() does absolutely nothing though, apparently; the items still show up as they used to, without icons.
I'm aware that this is probably not the way this function needs to be used, but I have found little documentation about it online, and absolutely no useful examples of implementations. My question is, can anybody help me with fixing this problem?
This is how you may conduct your debugging:
Try the constructor that has both icon and text as arguments.
Try to use that icon in another context to ensure it is displayable (construct a QIcon with same argument and use it elsewhere, e.g. QLabel!).
Use icon() from the QListWidgetItem to receive back the icon and then look at that QIcon.
Create a new QListWidget, change nothing, and ordinarily add some stock items in your MainWidget's constructor. See if the icons show up there.

Programmatically change combobox

I need to update a combobox with a new value so it changes the reflected text in it. The cleanest way to do this is after the comboboxhas been initialised and with a message.
So I am trying to craft a postmessage to the hwnd that contains the combobox.
So if I want to send a message to it, changing the currently selected item to the nth item, what would the postmessage look like?
I am guessing that it would involve ON_CBN_SELCHANGE, but I can't get it to work right.
You want ComboBox_SetCurSel:
ComboBox_SetCurSel(hWndCombo, n);
or if it's an MFC CComboBox control you can probably do:
m_combo.SetCurSel(2);
I would imagine if you're doing it manually you would also want SendMessage rather than PostMessage. CBN_SELCHANGE is the notification that the control sends back to you when the selection is changed.
Finally, you might want to add the c++ tag to this question.
A concise version:
const int index = 0;
m_comboBox.PostMessage(CBN_SELCHANGE, index);
What might be going wrong is the selection is being changed inside the selection change message handler, which result in another selection change message.
One way to get around this unwanted feedback loop is to add a sentinel to the select change message handler as shown below:
void onSelectChangeHandler(HWND hwnd)
{
static bool fInsideSelectChange = 0;
//-- ignore the change message if this function generated it
if (fInsideSelectChange == 0)
{
//-- turn on the sentinel
fInsideSelectChange = 1;
//-- make the selection changes as required
.....
//-- we are done so turn off the sentinel
fInsideSelectChange = 0;
}
}
if you fx want to change the title - which is the line shown when combobox is closed, then you can do following:
m_ComboBox.DeleteString(0); // first delete previous if any, 0 = visual string
m_ComboBox.AddString(_T("Hello there"));
put this in fx. in OnCloseupCombo - when event close a dropdownbox fires
ON_CBN_CLOSEUP(IDC_COMBO1, OnCloseupCombo)
This change is a new string not a selection of already assigned combobox elements