C
C#17mo ago
Baka_Aqua >_<

❔ MessageBox.Show on ComboBox selected item (C# Windows Form)

Can someone help me on how to use MessageBox whenever I click an item from the ComboBox? Example: I will click Earth then I want it to display message about the ID of Earth and Description (From saved Database)
17 Replies
SG97
SG9717mo ago
how are you handling the selection?
Denis
Denis17mo ago
If this is a WinForms application, then check out the events that the combobox has. It should provide you with a SelectionChanged event. That is what you want. Via said.event you'd catch whenever the user selects something and display your message box accordingly
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
I think it's missing something, coz it doesn't show the message
Denis
Denis17mo ago
Is the selected item truly a datarowview? Try writing the following before the if condition:
var selectedItemType = CbxDatashow.SelectedItem.GetType().Name;
MessageBox.Show($"The selected item type is: {selectedItemType}");
var selectedItemType = CbxDatashow.SelectedItem.GetType().Name;
MessageBox.Show($"The selected item type is: {selectedItemType}");
You could also figure this out by setting a breakpoint and debugging I'd place a breakpoint on line 61, you can do this by clicking the line number on the left, or by pressing F9 on your keyboard
SG97
SG9717mo ago
and why does your event handler have 2 references + _1 suffix
Denis
Denis17mo ago
@BakaAqua >< could you please share a screenshot of the WinForms designer window, with the combobox selected and it's event settings displayed? It is possible that you have an incorrect event bound to the control
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
I forgot to remove that one, .... it's remove now
SG97
SG9717mo ago
this is crucial
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
Denis
Denis17mo ago
The name of the event is either wrong or cut off by the window. Is the bound event called CbxDatashow_Selected or is it something longer?
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
SelectedIndexChanged
Denis
Denis17mo ago
Then this event won't run, as it is SelectedIndexChanged_1
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
Any advice of what would be the appropriate event to use?
Denis
Denis17mo ago
How many selectionChanged event handlers do you have in your code It seems that you have multiple I cannot suggest, because you create the event handlers
Baka_Aqua >_<
Baka_Aqua >_<17mo ago
Only one in this form Oh okay thanks
Denis
Denis17mo ago
Did you make sure the correct event handler is being fired?
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.