Windows Develop Bookmark and Share   
 index > Windows Forms General > Callback for ComboBox
 

Callback for ComboBox

I'm just starting to learn C#, from years of using C.

I have a little panel for my app, it is going to repeat itself a number of time the user selects (it is a little addition testing program for my son; my first C# app - a GUI panel with 2 numbers and you enter the answer and get a new problem).

I added a ComboBox to let the user select the number of times to repeat  10, 20 , 30, ....

I'd like to set the default value to the first in the list, 10. But, I can't figure out how to set it.

When the user selects a value in the ComboBox I'd like to have that call a function.
When I created the ComboBox, this was created, and I added the line of code in it:
        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loop = 10 * (comboBox1.SelectedIndex);

        }
but it doesn't hit it when I set a break point there. Is this what this function is for? Or do I need to create something else?
Sort of like the buttons I created that call functions.

I am using C# WFP with .Net, and have a Window1.xaml where I created the panel by dragging text boxes and buttons and the combobox from the controls and modifying the properties.
The buttons have a "Click" value in the XMAL under the designer window, but I can't figure out how to get something similar for the ComboBox.

Thanks for help.
(if I'm using the wrong terminology, please correct me on that too)
  • Moved byTaylorMichaelLMVP17 hours 51 minutes agoWinForms related (From:Visual C# General)
  •  
uzziah0  17 hours 53 minutes ago
If I understand correctly, you're just trying to associate your event handler...

If that's the case, you'll need to have your ComboBox event tied to your event handler.  You can do this in the property window of the designer, or directly in xaml, like so:


        <ComboBox Height="23" Name="comboBox1" SelectionChanged="comboBox1_SelectionChanged" >
            <ComboBoxItem>10</ComboBoxItem>
            <ComboBoxItem>20</ComboBoxItem>
            <ComboBoxItem>30</ComboBoxItem>
            <ComboBoxItem>40</ComboBoxItem>
            <ComboBoxItem>50</ComboBoxItem>
        </ComboBox>

This will make it call your method (comboBox1_SelectionChanged) on the SelectionChanged event.

Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byuzziah0 15 hours 56 minutes ago
  •  
Reed Copsey, Jr.  17 hours 35 minutes ago
Add SelectedIndex to the xaml:

       <ComboBox Height="23" Name="comboBox1" SelectedIndex="0" SelectionChanged="comboBox1_SelectionChanged" >
            <ComboBoxItem>10</ComboBoxItem>
            <ComboBoxItem>20</ComboBoxItem>
            <ComboBoxItem>30</ComboBoxItem>
            <ComboBoxItem>40</ComboBoxItem>
            <ComboBoxItem>50</ComboBoxItem>
        </ComboBox>

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  17 hours 8 minutes ago
If I understand correctly, you're just trying to associate your event handler...

If that's the case, you'll need to have your ComboBox event tied to your event handler.  You can do this in the property window of the designer, or directly in xaml, like so:


        <ComboBox Height="23" Name="comboBox1" SelectionChanged="comboBox1_SelectionChanged" >
            <ComboBoxItem>10</ComboBoxItem>
            <ComboBoxItem>20</ComboBoxItem>
            <ComboBoxItem>30</ComboBoxItem>
            <ComboBoxItem>40</ComboBoxItem>
            <ComboBoxItem>50</ComboBoxItem>
        </ComboBox>

This will make it call your method (comboBox1_SelectionChanged) on the SelectionChanged event.

Reed Copsey, Jr. - http://reedcopsey.com
  • Marked As Answer byuzziah0 15 hours 56 minutes ago
  •  
Reed Copsey, Jr.  17 hours 35 minutes ago
That's it exactly.

The only thing is, how do I set the default value to the first one, the 10?
uzziah0  17 hours 13 minutes ago
Add SelectedIndex to the xaml:

       <ComboBox Height="23" Name="comboBox1" SelectedIndex="0" SelectionChanged="comboBox1_SelectionChanged" >
            <ComboBoxItem>10</ComboBoxItem>
            <ComboBoxItem>20</ComboBoxItem>
            <ComboBoxItem>30</ComboBoxItem>
            <ComboBoxItem>40</ComboBoxItem>
            <ComboBoxItem>50</ComboBoxItem>
        </ComboBox>

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  17 hours 8 minutes ago
Great.
That does it.
Thanks
uzziah0  15 hours 56 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Resize of nested control
• Closing Form
• Panel unable to set to 'visible = true'
• Adding nodes in AfterLabelEdit event handler of treeview control
• Hidding tabs in a c1dockingtab in visual studio 2005
• Combobox sorting on pocket pc
• How to create Directory open dialogue like a File opne dialogue?
• Making a datgridview fit a tabpage?
• Inherited Forms in 2.0
• Resizing Control