❔ 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
SG972y ago
how are you handling the selection?
Denis
Denis2y 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 >_<OP2y ago
I think it's missing something, coz it doesn't show the message
Denis
Denis2y 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
SG972y ago
and why does your event handler have 2 references + _1 suffix
Denis
Denis2y 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 >_<OP2y ago
I forgot to remove that one, .... it's remove now
SG97
SG972y ago
this is crucial
Baka_Aqua >_<
Baka_Aqua >_<OP2y ago
Denis
Denis2y 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 >_<OP2y ago
SelectedIndexChanged
Denis
Denis2y ago
Then this event won't run, as it is SelectedIndexChanged_1
Baka_Aqua >_<
Baka_Aqua >_<OP2y ago
Any advice of what would be the appropriate event to use?
Denis
Denis2y 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 >_<OP2y ago
Only one in this form Oh okay thanks
Denis
Denis2y ago
Did you make sure the correct event handler is being fired?
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server