Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Event triger

Event triger

Scheduled Pinned Locked Moved C#
question
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D Shen
    wrote on last edited by
    #1

    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:

    P D 2 Replies Last reply
    0
    • D D Shen

      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:

      P Offline
      P Offline
      Paul Riley
      wrote on last edited by
      #2

      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

      D D S 4 Replies Last reply
      0
      • D D Shen

        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:

        D Offline
        D Offline
        David Stone
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • P Paul Riley

          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

          D Offline
          D Offline
          David Stone
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • D David Stone

            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

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            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!_

            D 1 Reply Last reply
            0
            • P Paul Riley

              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

              D Offline
              D Offline
              D Shen
              wrote on last edited by
              #6

              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!!

              P 1 Reply Last reply
              0
              • P Paul Riley

                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

                D Offline
                D Offline
                D Shen
                wrote on last edited by
                #7

                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!!

                1 Reply Last reply
                0
                • L leppie

                  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!_

                  D Offline
                  D Offline
                  D Shen
                  wrote on last edited by
                  #8

                  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!

                  L 1 Reply Last reply
                  0
                  • D D Shen

                    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!

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    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!

                    1 Reply Last reply
                    0
                    • D D Shen

                      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!!

                      P Offline
                      P Offline
                      Paul Riley
                      wrote on last edited by
                      #10

                      [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 use button1.Enabled = !AllEmpty() in three different event handlers. Paul

                      1 Reply Last reply
                      0
                      • P Paul Riley

                        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

                        S Offline
                        S Offline
                        Shaun Wilde
                        wrote on last edited by
                        #11

                        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

                        P 1 Reply Last reply
                        0
                        • S Shaun Wilde

                          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

                          P Offline
                          P Offline
                          Paul Riley
                          wrote on last edited by
                          #12

                          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

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups