Could anyone tell me How can I change a button by selecting an option in a ComboBox? eg: *NOTE* Using C#, .net 4.0, VS2010. Thanx In Advanced!
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
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.
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 }
You are changing what the button does. For my own benefit, is there a harm in making the button visible/invisible?
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)
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?
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"];
*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
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
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; }
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.