Event triger
-
Hi, I have three TextBox and one Button controls. I disable the button when the form loaded and would like to make it enabled when each textbox has input. At this point, I put following code in each textbox_textChanged event:
if(textbox1.text.length!=0 && textbox2.text.length !=0 && textbox2.text.length !=0) button1.enabled = true;
Is there any other way that I don't have to duplicate above codes in each textbox which I would like to check? I appreciate it!!:laugh: -
Hi, I have three TextBox and one Button controls. I disable the button when the form loaded and would like to make it enabled when each textbox has input. At this point, I put following code in each textbox_textChanged event:
if(textbox1.text.length!=0 && textbox2.text.length !=0 && textbox2.text.length !=0) button1.enabled = true;
Is there any other way that I don't have to duplicate above codes in each textbox which I would like to check? I appreciate it!!:laugh:Yes! Create a method that does this, then go to the form designer, select one of your text boxes, click the little lightning (properties) button above the properties, find the correct event in the list, then use the drop-down next to it to select your method. Then repeat for your other text boxes. This attaches the same function to each of the three delegates. Neat. I do the same thing on a web form. Paul
-
Hi, I have three TextBox and one Button controls. I disable the button when the form loaded and would like to make it enabled when each textbox has input. At this point, I put following code in each textbox_textChanged event:
if(textbox1.text.length!=0 && textbox2.text.length !=0 && textbox2.text.length !=0) button1.enabled = true;
Is there any other way that I don't have to duplicate above codes in each textbox which I would like to check? I appreciate it!!:laugh:You could iterate through the controls collection of the Form to find it for all the textboxes. I think this'll work:
bool validate = true; foreach(Textbox txt in this.Controls) {if(txt.Text = "") validate = false;}
if(validate) button1.Enabled = true; That way, if any one of them is false, then your button doesn't get enabled. I'm sure there's a better way to do it though... Your bullshit is so effusive I can smell it across oceans... You impress no-one. You are a world-class sleazeball; an incomparable jerk. No-one is fooled by your idiotic attempts to slant votes. -A. N. Onymous on Bill SerGio -
Yes! Create a method that does this, then go to the form designer, select one of your text boxes, click the little lightning (properties) button above the properties, find the correct event in the list, then use the drop-down next to it to select your method. Then repeat for your other text boxes. This attaches the same function to each of the three delegates. Neat. I do the same thing on a web form. Paul
Oh, I see the "I put each in the textBox_textChanged method" thing. Duh! Don't read my post...:-O Your bullshit is so effusive I can smell it across oceans... You impress no-one. You are a world-class sleazeball; an incomparable jerk. No-one is fooled by your idiotic attempts to slant votes. -A. N. Onymous on Bill SerGio
-
You could iterate through the controls collection of the Form to find it for all the textboxes. I think this'll work:
bool validate = true; foreach(Textbox txt in this.Controls) {if(txt.Text = "") validate = false;}
if(validate) button1.Enabled = true; That way, if any one of them is false, then your button doesn't get enabled. I'm sure there's a better way to do it though... Your bullshit is so effusive I can smell it across oceans... You impress no-one. You are a world-class sleazeball; an incomparable jerk. No-one is fooled by your idiotic attempts to slant votes. -A. N. Onymous on Bill SerGioDavid Stone wrote: _bool validate = true; foreach(Textbox txt in this.Controls) {if(txt.Text = "") validate = false;} That would work if only TextBoxes were on the form.:-O Rather:
foreach (Control ctrl in Controls){
TextBox txt = ctrl as TextBox;
if (txt != null && txt.Text == "") validate = false;
};P Give them a chance! Do it for the kittens, dear God, the kittens!_
-
Yes! Create a method that does this, then go to the form designer, select one of your text boxes, click the little lightning (properties) button above the properties, find the correct event in the list, then use the drop-down next to it to select your method. Then repeat for your other text boxes. This attaches the same function to each of the three delegates. Neat. I do the same thing on a web form. Paul
-
Yes! Create a method that does this, then go to the form designer, select one of your text boxes, click the little lightning (properties) button above the properties, find the correct event in the list, then use the drop-down next to it to select your method. Then repeat for your other text boxes. This attaches the same function to each of the three delegates. Neat. I do the same thing on a web form. Paul
-
David Stone wrote: _bool validate = true; foreach(Textbox txt in this.Controls) {if(txt.Text = "") validate = false;} That would work if only TextBoxes were on the form.:-O Rather:
foreach (Control ctrl in Controls){
TextBox txt = ctrl as TextBox;
if (txt != null && txt.Text == "") validate = false;
};P Give them a chance! Do it for the kittens, dear God, the kittens!_
-
leppie wrote: foreach (Control ctrl in Controls){ TextBox txt = ctrl as TextBox; if (txt != null && txt.Text == "") validate = false;} Thanks for the code. Do I still put the code in each text_changed event? Thanks!
This wont really work unless u have only the 3 text boxes (other controls welcome)in question on the form :) I was just trying to point out that u will get a cast exception if you dont check the type. I would definately go with the other option. Assigned for all 3 ontextchanged events. Obviously, you will need some reset code as well.
if (tb1.Text == "" || tb2.Text == "" || tb3.Text == "" ) validate = false;
Or place the 3 textboxes in a panel :) Give them a chance! Do it for the kittens, dear God, the kittens! -
Yes, it is neat and does save me lots of typing:-O But if I would like to do little bit different for each text_changed event, how do I do? Can I attach more than one method for each event? Thanks!!
[All code in this message is untested] D Shen wrote: But if I would like to do little bit different for each text_changed event, how do I do? Can I attach more than one method for each event? Yes, but it has to be done manually. Go to the form constructor, right underneath
TODO: Add any constructor code after InitializeComponent call
and add something like the following:this.textButtonX.TextChanged += new System.EventHandler(this.MySecondaryMethod);
However, be careful. If you attach two methods to one Delegate, I can't guarantee what order they will run in or even that they will run in the same sequence each time. A lot of testing is in order here. Unless the extra code is unrelated and it doesn't matter which sequence you run in, it might be much safer to simply have a method that works like this:private bool AllEmpty() { return ((textBox1.Text.Length + textBox2.Text.Length + textBox3.Text.Length) == 0) }
then usebutton1.Enabled = !AllEmpty()
in three different event handlers. Paul -
Yes! Create a method that does this, then go to the form designer, select one of your text boxes, click the little lightning (properties) button above the properties, find the correct event in the list, then use the drop-down next to it to select your method. Then repeat for your other text boxes. This attaches the same function to each of the three delegates. Neat. I do the same thing on a web form. Paul
doesn't this cause the form to update on each text update?
Stupidity dies. The end of future offspring. Evolution wins. - A Darwin Awards Haiku
-
doesn't this cause the form to update on each text update?
Stupidity dies. The end of future offspring. Evolution wins. - A Darwin Awards Haiku
Shaun Wilde wrote: doesn't this cause the form to update on each text update? On a web form: yes, in a manner of speaking, though you can skip the update code. Page_Load is called, which I think is your question. On a windows form: no, it simply fires the TextChanged event. Form_Load is not called. Paul