1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Development C# Need help with ComboBox

Discussion in 'Software' started by NikoBellic, 16 Jan 2011.

  1. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    Could anyone tell me

    How can I change a button by selecting an option in a ComboBox?

    eg:
    [​IMG]

    *NOTE*

    Using C#, .net 4.0, VS2010.

    Thanx In Advanced! :)
     
    Last edited: 16 Jan 2011
  2. yakyb

    yakyb i hate the person above me

    Joined:
    10 Oct 2006
    Posts:
    2,064
    Likes Received:
    36
    you need to use the on selection changed event

    you can then on the event set the button Name


    btnButton.Text = comboBox.SelectedValue.Text

    doesnt sound like a particularly good design option however
     
  3. DMU_Matt

    DMU_Matt mmmm cheesy

    Joined:
    23 Oct 2009
    Posts:
    680
    Likes Received:
    16
    That will only change it aesthetically though, the command that the button executes will still be the same.

    What I would suggest is have both buttons on the form, but only visible when selected in the combo box (which is what I think you are getting at) If you are wanting them to perform different functions that is.

    So you would have something like:

    button1.visible = false
    button2.visible = false

    'if combobox1.value = button1
    then button1.visible = true
    else if combobox1.value = button2
    then button2.visible = true'

    I am a little rusty with this, but something like that should work.
     
    Last edited: 17 Jan 2011
  4. yakyb

    yakyb i hate the person above me

    Joined:
    10 Oct 2006
    Posts:
    2,064
    Likes Received:
    36
    arggghh don't do that

    you could use the event to change a parameter

    or create an instance of an Interface that can be used by the button click

    or even change the onClick event method call alltogether

    all depends an what you are trying to do



    Code:
     IButtonClick clickArgs;
    
    onSelectedValueChanged
    {
    btnButton.Text = comboBox.SelectedValue.Text;
    if (1) 
    clickargs = new OneObject();
    if(2)
    clickargs = new TwoObject();
    }
    
    btnClickEvent( )
    {
    clickArgs.DoSomething();
    }
    
    

    or similar


    Code:
     int number = 1;
    
    onSelectedValueChanged
    {
    btnButton.Text = comboBox.SelectedValue.Text;
    if (SelectedValue.Text == "Button 1") 
    number  = 1;
    if(SelectedValue.Text == "Button 2")
    number  = 2;
    }
    
    
    btnClickEvent( IButtonClick click)
    {
    if(number ==1)
    //DoThis
    if(number ==2)
    //Do This
    }
    
     
    Last edited: 17 Jan 2011
  5. DMU_Matt

    DMU_Matt mmmm cheesy

    Joined:
    23 Oct 2009
    Posts:
    680
    Likes Received:
    16
    You are changing what the button does.

    For my own benefit, is there a harm in making the button visible/invisible?
     
  6. yakyb

    yakyb i hate the person above me

    Joined:
    10 Oct 2006
    Posts:
    2,064
    Likes Received:
    36
    its very difficult to figure out what is going on generally using that. Lots of Events causing loads of Lines of Code (plus it could be slower to render)

    ask your self what happens when i press this button?
    if its 1 then ...
    if its 2 then ...
    as opposed to which button?

    what if there was 10 objects in the comboBox?

    what if the comboBox contents were DB generated and you dont know the number of results

    you could in theory add a new Button through Code on the on Load event, but then you would have to tie each one to a seperate event anyway

    personally i like the Interface approach but that may be over engineering


    making a button visible / enabled i think should be restricted to validation of a complete form or an option else where (i.e GroupBox.visible on a Check box / radio button)
     
    Last edited: 17 Jan 2011
  7. DMU_Matt

    DMU_Matt mmmm cheesy

    Joined:
    23 Oct 2009
    Posts:
    680
    Likes Received:
    16
    Ok, that makes sense. Thanks for clarifying that for me. Much appreciated :)
     
  8. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    Thanx guys, when I get round to trying it, I'll update you on my success :)
     
  9. Crispin Zeke

    Crispin Zeke New Member

    Joined:
    18 Jan 2011
    Posts:
    3
    Likes Received:
    0
    I have wrote program which can search on a local sql data base and generate answers
    when it faces to a null value in table generates error
    how can i compare it by null value?

    routine ways can not help me to do that, is there any special way for comparing by null value?
     
  10. yakyb

    yakyb i hate the person above me

    Joined:
    10 Oct 2006
    Posts:
    2,064
    Likes Received:
    36
    not sure if this is spam or not but i'll answer anyway

    It entirely depends on you data access methodology and your comparison your tryig to do


    however my best guess is that your trying to map (for example) a number which could be null and you are not using the nullable data type

    i.e.

    instead of

    int I = (int)reader["number"];
    use
    int? I = (int?)reader["number"];

    you can then do a check if necessary

    or you could default it to 0 using something similar to below

    int I = (reader["number"] == null)? 0 : (int)reader["number"];
     
  11. NikoBellic

    NikoBellic Tech Addict

    Joined:
    9 May 2009
    Posts:
    96
    Likes Received:
    1
    *UPDATE*

    I've tried the following but with no success:

    Code:
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
    
                {
                    button1.visible = false;
    
                    
    
                    if combobox1.value = Button 1();
                    }
                    button1.visible = true;
                    {
                    
                }
            }
    
    I get 2 errors which are as follows:

    Sytax error, '(' expected
    and when I click the error message it highlights:
    Code:
    combobox1
    located at:
    Code:
    if combobox1.value = Button 1();
    the seconds error is
    ) expected
    & when I click that message it highlights:
    Code:
    1
    Located:
    Code:
    Button 1();
    Code:
    if combobox1.value = Button 1();
    any help would be greatly appreciated :)
     
  12. DMU_Matt

    DMU_Matt mmmm cheesy

    Joined:
    23 Oct 2009
    Posts:
    680
    Likes Received:
    16
    your:

    Code:
     }
     button1.visible = true;
     {
    is wrong, need to flip them round.

    Also:

    Code:
    if combobox1.value = Button 1();
    
    needs to be:

    Code:
    if (comboBox1.Value == "Button 1")
    no ';' and 2 '=' signs, also you need to have brackets around it.

    Not entirely sure about whether the code itself will work, but that should at least sort out your syntax
     
    Last edited: 21 Jan 2011
  13. yakyb

    yakyb i hate the person above me

    Joined:
    10 Oct 2006
    Posts:
    2,064
    Likes Received:
    36
    the errors you are getting are because your syntax is completely out
    Code:
    
    if( true )            //expression to be evaluated i.e. if (1==1) '==' means is equal to in this case 1==1 is true
    {          //Squiggly brackets to encase the actions
       bool result = true;             //singular '=' sets boolean result to true 
       RunSomething(result);      //method call note '(result)' passes the result 'true' to method 
    }          //Squiggly brackets to close the actions
    else    //use else if the above does not occur
    {            //Squiggly brackets to encase the actions
       bool result = false;//singular '=' sets boolean result to false 
       RunSomething(result);   //method call note '(result)' passes the result 'false' to method 
    }    //Squiggly brackets to close the actions
    
    
    the above is a bit of a mess but if you put it in VS it should be clearer
    not the best way of writing the above by the way





    are you still trying to use multiple buttons?
    it looks like it from that code

    how something Like

    Code:
    
    string text = Combobox.selectedValue.Text;
    
    
    switch (text)
    {
    case "Action 1":
    RunAction1();
    break;
    case "Action 2";
    RunAction2();
    break;
    default:
    throw new Exception("Could not find Text");
    break;
    }
    
    
    
    
    
     
    Last edited: 24 Jan 2011
  14. Technologist

    Technologist New Member

    Joined:
    1 Feb 2011
    Posts:
    24
    Likes Received:
    0
    Please use the following functions:

    panel.Controls.Add()
    panel.Controls.Remove()


    These functions are useful in the respective event of your combo box object

    Of course, this is useful if you use something like the the System.Windows.Forms.Panel class for your panels. I hope this helps.
     

Share This Page