Edit Win32 combo box items - c++

I have a combo box with a list of names that are editable through a text box on the same form. How would I go about editing the items that are sitting in the combo box when the user changes the name in the text box? There doesn't seem to be any message to update the strings sitting inside the combo box.
Thanks,
Edit: this is the code thats called when the text changes
void txtNameChange(HWND hDlg)
{
if(SelectedIndex != -1)
{
wchar_t Name[255];
GetDlgItemText(hDlg, txtName, Name, 255);
//need to set the item text of an item at a certain index here
}
}

You cannot rename an item in place. You need to delete the item you wish to modify with the CB_DELETESTRING message, and then insert the new value with the CB_INSERTSTRING message.

Related

customize wxTextCtrl autocomplete

I have a wxTextCtrl object and set it to be autocomplete
wxArrayString _myStringArray;
_myStringArray.push_back("abc");
_myStringArray.push_back("alpha");
_myStringArray.push_back("bnm");
_myTextCtrl->AutoComplete(_myStringArray);
I type char 'a' into it. And then a popup shown with a list of related/suggested strings (i.e "abc" and "alpha"). Now I press 'down arrow key' in order to select a string. The first time I press the button the "abc" string is selected. The second time I press the button the "alpha" string is selected.
The problem is changing the string selection by pressing UP and DOWN arrow keys doesn't change the text control value. I want the text control value to be updated when the selected string changed by pressing UP and DOWN arrow key.
I thought I could do that manually if I know the event name. So the question is: what is the event name (or event macro) of changing string selection from a popup in a wxTextCtrl by pressing UP and DOWN arrow key?
Thanks
update: I am succeded on capturing KEY DOWN event by subclassing wxTextCtrl and then add an event handler for EVT_KEY_DOWN event.
void TextCtrlChild::keyHandler(wxKeyEvent& event)
{
int _keyCode = event.GetKeyCode();
if(_keyCode == 315 || _keyCode == 317){ //if UP or DOWN arrow key is pressed
//TO DO: capture the highlighted string from the popup
}
event.Skip();
}
Now the question is how to capture the selected/highlighted string from the popup?
The way autocompletion works is dictated by the system UI conventions, so it doesn't look like a good idea to interfere with it. If you really want to have immediate selection, consider using another control, such as wxChoice, instead.

Prevent selected item from going out of view in QListView

I am using a QListView with only single selection and selection changes handled by the up and down arrows. Everything is working as expected with one minor issue. If many items are added to the top of the list then the currently selected item risks moving down and out of view. Is there any way to detect when this happens? Thanks.
I found solution, but first of all I try to do dimilar thing, I run timer and every 2 second I insert new row to the begin of list and try to detect, is selected item still visible?
//somewhere in the constructor, it is not important
ListModel = new QStandardItemModel();
ui->listView->setModel(ListModel);
Now when I want to insert new row , I wrote code, which checks is the item visible
QStandardItem* Items = new QStandardItem(QString::number(qrand()%100));
ListModel->insertRow(0,Items);
// here we get rect of current selected item
QRect rec = ui->listView->visualRect(ui->listView->currentIndex());
//here we get all listView region, convert it to rect and check is our listView
//contain selected item
if(ui->listView->viewport()->visibleRegion().boundingRect().contains(rec))
{
qDebug() << "visible";//all good
}
else
{
qDebug() << "not visible";//not good, we need do something
//you want to detect it, so you get!
//now you know that item "runs away from you"
//and you can do some actions
someAction();
}
I test it on my computer, when I can see item, I get visible word, when item tries "run away from me" I get not visible
I hope it helps.

listview combobox

I have added combobox to my custom listview and I am trying to set the combobox's text to the current listview subitem.
case CBN_SELCHANGE:
{
char txt[200];
ComboBox_GetText(hCombo, txt, 256);
int i = SendMessage(hListView, LVM_GETHOTITEM,0, 0);
ListView_SetItemText(hListView, i, 2, txt);
}
what am I doing wrong?
The listview "hot" item is the item with the cursor hovered over it and only when hot tracking and hot selection is enabled. The SDK docs for LVM_GETHOTITEM do a poor job explaining what a hot item actually is. The CListCtrl::GetHotItem docs give a bit more detail.
At the time you make the selection from the combo box, the cursor is not hovered over any item in the list box so no text will be set.
You likely want to get the selected item instead:
int i = ListView_GetNextItem(hListView, -1, LVNI_SELECTED);
This post has a bit more information.

How to check which item from menu was chosen? How to send int/wxstring with choosing menu item?

I've got menu in which i need to place dynamically few items( i don't know how many until app launch;-). It's not problem to put item in menu, and connect it`s event to some function. But i need to check which item from menu was chosen. Can i send int or wxString with click at menu item? How?
wxMenu *MyTaskBarIcon::CreatePopupMenu(){
wxMenu *menu = new wxMenu;
menu->Append(ITEM1, _("Item1"));
Connect(ITEM1,wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyTaskBarIcon::Check));
menu->Append(ITEM2, _("Item2"));
Connect(ITEM2,wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyTaskBarIcon::CheckMenu));
menu->AppendSeparator();
menu->Append(PU_EXIT, wxT("E&xit"));
}
void MyTaskBarIcon::Check(wxCommandEvent& event){
//How to send int/wxString to this method?
}
Any ideas?
How about assigning a range of IDs to this menu? Then when you append the items to the menu, you can increment the ID each time, so each item gets a unique ID. Then you create an event handler for each ID, and call a common handler with the ID as a parameter. Or, you could use
wxEvent::GetId()
If the strings vary at runtime then you will have to store the strings in an array, and recall them from there using the ID minus the start of the range ID as the index into the array.
Use GetString() to see what the user selected:
wxString GetString () const
Returns item string for a listbox or choice selection event.

How do I put a checkmark on a menu item that has submenu items. (Visual studio 2008 C++/MFC)

I have a menu that contains submenus.
eg:
Item1
Item2
Item3
item A
Item B
Item3 has items under it.
At any given time 1, 2, or the items under 3 should be checked. Since I don't have an ID for Item3 I have to use the MF_BYPOSITION indicator when I try to set a check on Item3 to indicate one of its children has a checkmark. Item3 should have a checkmark if A or B are checked. I am able to check items 1 and 2 and A and B - but can't figure out item3.
I have not been able to successfully use either ::CheckMenuItem() or ModifyMenu() to set the check mark.
Can someone point me to an example that does this successfully? The docs seem to indicate it can be done, but I have been unable to do it.
EDIT
This is for a menu that is set as the menu for a dlg box. The menu bar has three items - one of which drops down to what is shown above.
Note also, it is used as a popup for a right click, but I will take any suggestions to work in either case.
I've done this before for popup menus. You will need to access the submenu by position, instead of ID. Using your example above, Item 3 would be at position 2:
CMenu popupMenu;
popupMenu.LoadMenu(IDR_MYMENU);
popupMenu.GetSubMenu(0)->CheckMenuItem(2,MF_BYPOSITION|MF_CHECKED);
.
.
.
popupMenu.GetSubMenu(0)->TrackPopupMenu(...);
However, I haven't done this with items in the menu bar.
EDIT by Tim the OP:
For completeness
To get it to work with the menu item you have to get the hmenu
// MENU_POSITION is the zero based location of the menu you want to use. (file, edit, view, help... etc)
HMENU mainMenu = ::GetMenu(m_hWnd);
HMENU subMenu = GetSubMenu( mainMenu, MENU_POSITION);
SetMenuState(subMenu);
A few moments ago I had a similar problem - a standard MFC menu bar containing at least one submenu, and the need to be able to add a check mark to the submenu parent item, when any of the submenu child items were checked.
The easiest solution (for me) turned out to be as simple as performing the update in the standard OnUpdateMenuItem(CCmdUI* pCmdUI) call. In my case I used ON_UPDATE_COMMAND_UI_RANGE() to feed a bunch of menu IDs into the same update call, but the principal is the same for a single ON_UPDATE_COMMAND_UI() map.
The code I used (edited to be more easily inserted into other people's work) is:
void CMyApp::OnUpdateMenu(CCmdUI* pCmdUI)
{
// Note, a submenu parent (which has no editable ID in the resource editor) has the SAME ID as the first child item
if (pCmdUI->m_nID == ID_FIRST_CHILD_MENU && pCmdUI->m_pSubMenu != NULL) {
// Get the child menu so we can see if any child items are checked
CMenu* pSubMenu = pCmdUI->m_pSubMenu;
BOOL fChildChecked = FALSE;
for (UINT i = 0; !fChildChecked && i < pSubMenu->GetMenuItemCount(); ++i) {
// Do something to decide if this child item should be checked...
UINT nChildID = pSubMenu->GetMenuItemID(i);
fChildChecked = IsThisChildChecked(nChildID);
}
// The POSITION of the current menu item is stored in pCmdUI->m_nIndex
CMenu* pMenu = pCmdUI->m_pMenu;
UINT flags = MF_BYPOSITION;
if (fActiveChild) flags |= MF_CHECKED;
pMenu->CheckMenuItem(pCmdUI->m_nIndex, flags);
}
// Set the enabled state of the menu item as you see fit...
pCmdUI->Enable(TRUE);
}
Et voilĂ  the submenu item automagically gains a check mark when any of its child menu items has a check mark.
Hope this helps others looking for similar solutions!
John