Textbox background color

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Pais

    #1

    Textbox background color

    Hi there.

    How can I change the background color of a textbox when it gets the focus?

    I can handle the "Enter" event and do this
    private void txtDummie_Enter (object sender, EventArgs e) {

    txtDummie.BackC olor=Color.Red;

    }

    But how can I use this code to work with all textboxes in form? How can I
    use "sender" argument?

    Thanks in advance.

    Regards,

    Marco


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Textbox background color

    Marco,

    First, you would change your code to this:

    ((TextBox) sender).BackCol or = Color.Red;

    Then, on your form, you would cycle through all the TextBox controls
    (the Controls collection will help you with this, just use the as operator
    to determine if the control is a textbox) and then attach that method to the
    Enter event handler.

    You might want to make sure you attach another event handler to change
    the color back when the textbox loses focus.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Marco Pais" <marco.pais[at]gmail.comwrote in message
    news:eggpABtUIH A.3364@TK2MSFTN GP03.phx.gbl...
    Hi there.
    >
    How can I change the background color of a textbox when it gets the focus?
    >
    I can handle the "Enter" event and do this
    private void txtDummie_Enter (object sender, EventArgs e) {
    >
    txtDummie.BackC olor=Color.Red;
    >
    }
    >
    But how can I use this code to work with all textboxes in form? How can I
    use "sender" argument?
    >
    Thanks in advance.
    >
    Regards,
    >
    Marco
    >
    >

    Comment

    • Marco Pais

      #3
      Re: Textbox background color

      Hi there.

      Thanks for the reply.

      I'm not sure I understand "...just use the as operator...".

      I think I must use something with "+=" bu I don't know how...

      Thanks

      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omescreveu
      na mensagem news:OpNSNItUIH A.3364@TK2MSFTN GP03.phx.gbl...
      Marco,
      >
      First, you would change your code to this:
      >
      ((TextBox) sender).BackCol or = Color.Red;
      >
      Then, on your form, you would cycle through all the TextBox controls
      (the Controls collection will help you with this, just use the as operator
      to determine if the control is a textbox) and then attach that method to
      the Enter event handler.
      >
      You might want to make sure you attach another event handler to change
      the color back when the textbox loses focus.
      >
      >
      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m
      >
      "Marco Pais" <marco.pais[at]gmail.comwrote in message
      news:eggpABtUIH A.3364@TK2MSFTN GP03.phx.gbl...
      >Hi there.
      >>
      >How can I change the background color of a textbox when it gets the
      >focus?
      >>
      >I can handle the "Enter" event and do this
      >private void txtDummie_Enter (object sender, EventArgs e) {
      >>
      > txtDummie.BackC olor=Color.Red;
      >>
      >}
      >>
      >But how can I use this code to work with all textboxes in form? How can I
      >use "sender" argument?
      >>
      >Thanks in advance.
      >>
      >Regards,
      >>
      >Marco
      >>
      >>
      >
      >

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Textbox background color

        Marco,

        When cycling through the Controls collection, you want to do something
        like this:

        foreach (Control control in myForm.Controls )
        {
        // The textbox control.
        TextBox textBox = control as TextBox;

        // If not null, set the handler.
        if (textBox != null)
        {
        // Set the handler.
        textBox.Enter += OnTextBoxEnter;
        }
        }

        Of course OnTextBoxEnter is the method that sets the color to red. You
        could use anonymous methods in C# 2.0 or a lambda expression in C# 3.0 to
        make it even simpler.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Marco Pais" <marco.pais[at]gmail.comwrote in message
        news:eTWiLbtUIH A.1204@TK2MSFTN GP03.phx.gbl...
        Hi there.
        >
        Thanks for the reply.
        >
        I'm not sure I understand "...just use the as operator...".
        >
        I think I must use something with "+=" bu I don't know how...
        >
        Thanks
        >
        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
        escreveu na mensagem news:OpNSNItUIH A.3364@TK2MSFTN GP03.phx.gbl...
        >Marco,
        >>
        > First, you would change your code to this:
        >>
        >((TextBox) sender).BackCol or = Color.Red;
        >>
        > Then, on your form, you would cycle through all the TextBox controls
        >(the Controls collection will help you with this, just use the as
        >operator to determine if the control is a textbox) and then attach that
        >method to the Enter event handler.
        >>
        > You might want to make sure you attach another event handler to change
        >the color back when the textbox loses focus.
        >>
        >>
        >--
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >>
        >"Marco Pais" <marco.pais[at]gmail.comwrote in message
        >news:eggpABtUI HA.3364@TK2MSFT NGP03.phx.gbl.. .
        >>Hi there.
        >>>
        >>How can I change the background color of a textbox when it gets the
        >>focus?
        >>>
        >>I can handle the "Enter" event and do this
        >>private void txtDummie_Enter (object sender, EventArgs e) {
        >>>
        >> txtDummie.BackC olor=Color.Red;
        >>>
        >>}
        >>>
        >>But how can I use this code to work with all textboxes in form? How can
        >>I use "sender" argument?
        >>>
        >>Thanks in advance.
        >>>
        >>Regards,
        >>>
        >>Marco
        >>>
        >>>
        >>
        >>
        >
        >

        Comment

        • Marco Pais

          #5
          Re: Textbox background color

          Ok...

          I'm using this:

          foreach (Control control in this.Controls)

          {

          TextBox textBox = control as TextBox;

          if (textBox != null){

          textBox.Enter += delegate

          {

          //CHANGE BG COLOR

          };

          }

          }

          How can I know wich textbox must I change background color? How can I know
          wich sender has triggered the event?

          Thanks.

          "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omescreveu
          na mensagem news:eaH3TltUIH A.3916@TK2MSFTN GP02.phx.gbl...
          Marco,
          >
          When cycling through the Controls collection, you want to do something
          like this:
          >
          foreach (Control control in myForm.Controls )
          {
          // The textbox control.
          TextBox textBox = control as TextBox;
          >
          // If not null, set the handler.
          if (textBox != null)
          {
          // Set the handler.
          textBox.Enter += OnTextBoxEnter;
          }
          }
          >
          Of course OnTextBoxEnter is the method that sets the color to red. You
          could use anonymous methods in C# 2.0 or a lambda expression in C# 3.0 to
          make it even simpler.
          >
          >
          --
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard. caspershouse.co m
          >
          "Marco Pais" <marco.pais[at]gmail.comwrote in message
          news:eTWiLbtUIH A.1204@TK2MSFTN GP03.phx.gbl...
          >Hi there.
          >>
          >Thanks for the reply.
          >>
          >I'm not sure I understand "...just use the as operator...".
          >>
          >I think I must use something with "+=" bu I don't know how...
          >>
          >Thanks
          >>
          >"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
          >escreveu na mensagem news:OpNSNItUIH A.3364@TK2MSFTN GP03.phx.gbl...
          >>Marco,
          >>>
          >> First, you would change your code to this:
          >>>
          >>((TextBox) sender).BackCol or = Color.Red;
          >>>
          >> Then, on your form, you would cycle through all the TextBox controls
          >>(the Controls collection will help you with this, just use the as
          >>operator to determine if the control is a textbox) and then attach that
          >>method to the Enter event handler.
          >>>
          >> You might want to make sure you attach another event handler to
          >>change the color back when the textbox loses focus.
          >>>
          >>>
          >>--
          >> - Nicholas Paldino [.NET/C# MVP]
          >> - mvp@spam.guard. caspershouse.co m
          >>>
          >>"Marco Pais" <marco.pais[at]gmail.comwrote in message
          >>news:eggpABtU IHA.3364@TK2MSF TNGP03.phx.gbl. ..
          >>>Hi there.
          >>>>
          >>>How can I change the background color of a textbox when it gets the
          >>>focus?
          >>>>
          >>>I can handle the "Enter" event and do this
          >>>private void txtDummie_Enter (object sender, EventArgs e) {
          >>>>
          >>> txtDummie.BackC olor=Color.Red;
          >>>>
          >>>}
          >>>>
          >>>But how can I use this code to work with all textboxes in form? How can
          >>>I use "sender" argument?
          >>>>
          >>>Thanks in advance.
          >>>>
          >>>Regards,
          >>>>
          >>>Marco
          >>>>
          >>>>
          >>>
          >>>
          >>
          >>
          >
          >

          Comment

          • Greg

            #6
            Re: Textbox background color

            On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.comwrote:
            Hi there.
            >
            How can I change the background color of a textbox when it gets the focus?
            >
            I can handle the "Enter" event and do this
            private void txtDummie_Enter (object sender, EventArgs e) {
            >
            txtDummie.BackC olor=Color.Red;
            >
            }
            >
            But how can I use this code to work with all textboxes in form? How can I
            use "sender" argument?
            >
            Thanks in advance.
            >
            Regards,
            >
            Marco
            Actually, it's even easier than the suggestions from other threads...

            Instead of iterating over each control on your form, simply add one
            event handler like so:
            private void OnTextBoxEnter( object sender, EventArgs e)
            {
            ((TextBox)sende r).BackColor = Color.Red;
            }

            Then, make sure each text box subscribes to this event handler method.

            Cheers,

            Greg

            Comment

            • Nicholas Paldino [.NET/C# MVP]

              #7
              Re: Textbox background color

              Marco,

              You need to adjust the code like this:

              textBox.Enter += delegate(object sender, EventArgs eventArgs)
              {
              ((TextBox) sender).BackCol or = Color.Red;
              };


              --
              - Nicholas Paldino [.NET/C# MVP]
              - mvp@spam.guard. caspershouse.co m

              "Marco Pais" <marco.pais[at]gmail.comwrote in message
              news:%23XtfM5tU IHA.4752@TK2MSF TNGP05.phx.gbl. ..
              Ok...
              >
              I'm using this:
              >
              foreach (Control control in this.Controls)
              >
              {
              >
              TextBox textBox = control as TextBox;
              >
              if (textBox != null){
              >
              textBox.Enter += delegate
              >
              {
              >
              //CHANGE BG COLOR
              >
              };
              >
              }
              >
              }
              >
              How can I know wich textbox must I change background color? How can I know
              wich sender has triggered the event?
              >
              Thanks.
              >
              "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
              escreveu na mensagem news:eaH3TltUIH A.3916@TK2MSFTN GP02.phx.gbl...
              >Marco,
              >>
              > When cycling through the Controls collection, you want to do something
              >like this:
              >>
              >foreach (Control control in myForm.Controls )
              >{
              > // The textbox control.
              > TextBox textBox = control as TextBox;
              >>
              > // If not null, set the handler.
              > if (textBox != null)
              > {
              > // Set the handler.
              > textBox.Enter += OnTextBoxEnter;
              > }
              >}
              >>
              > Of course OnTextBoxEnter is the method that sets the color to red.
              >You could use anonymous methods in C# 2.0 or a lambda expression in C#
              >3.0 to make it even simpler.
              >>
              >>
              >--
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >>
              >"Marco Pais" <marco.pais[at]gmail.comwrote in message
              >news:eTWiLbtUI HA.1204@TK2MSFT NGP03.phx.gbl.. .
              >>Hi there.
              >>>
              >>Thanks for the reply.
              >>>
              >>I'm not sure I understand "...just use the as operator...".
              >>>
              >>I think I must use something with "+=" bu I don't know how...
              >>>
              >>Thanks
              >>>
              >>"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
              >>escreveu na mensagem news:OpNSNItUIH A.3364@TK2MSFTN GP03.phx.gbl...
              >>>Marco,
              >>>>
              >>> First, you would change your code to this:
              >>>>
              >>>((TextBox) sender).BackCol or = Color.Red;
              >>>>
              >>> Then, on your form, you would cycle through all the TextBox controls
              >>>(the Controls collection will help you with this, just use the as
              >>>operator to determine if the control is a textbox) and then attach that
              >>>method to the Enter event handler.
              >>>>
              >>> You might want to make sure you attach another event handler to
              >>>change the color back when the textbox loses focus.
              >>>>
              >>>>
              >>>--
              >>> - Nicholas Paldino [.NET/C# MVP]
              >>> - mvp@spam.guard. caspershouse.co m
              >>>>
              >>>"Marco Pais" <marco.pais[at]gmail.comwrote in message
              >>>news:eggpABt UIHA.3364@TK2MS FTNGP03.phx.gbl ...
              >>>>Hi there.
              >>>>>
              >>>>How can I change the background color of a textbox when it gets the
              >>>>focus?
              >>>>>
              >>>>I can handle the "Enter" event and do this
              >>>>private void txtDummie_Enter (object sender, EventArgs e) {
              >>>>>
              >>>> txtDummie.BackC olor=Color.Red;
              >>>>>
              >>>>}
              >>>>>
              >>>>But how can I use this code to work with all textboxes in form? How
              >>>>can I use "sender" argument?
              >>>>>
              >>>>Thanks in advance.
              >>>>>
              >>>>Regards,
              >>>>>
              >>>>Marco
              >>>>>
              >>>>>
              >>>>
              >>>>
              >>>
              >>>
              >>
              >>
              >
              >

              Comment

              • Marco Pais

                #8
                Re: Textbox background color

                Finally got it to work!

                Thanks a lot!

                Regards.

                "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c omescreveu
                na mensagem news:eB%23Hj9tU IHA.4624@TK2MSF TNGP03.phx.gbl. ..
                Marco,
                >
                You need to adjust the code like this:
                >
                textBox.Enter += delegate(object sender, EventArgs eventArgs)
                {
                ((TextBox) sender).BackCol or = Color.Red;
                };
                >
                >
                --
                - Nicholas Paldino [.NET/C# MVP]
                - mvp@spam.guard. caspershouse.co m
                >
                "Marco Pais" <marco.pais[at]gmail.comwrote in message
                news:%23XtfM5tU IHA.4752@TK2MSF TNGP05.phx.gbl. ..
                >Ok...
                >>
                >I'm using this:
                >>
                > foreach (Control control in this.Controls)
                >>
                > {
                >>
                > TextBox textBox = control as TextBox;
                >>
                > if (textBox != null){
                >>
                > textBox.Enter += delegate
                >>
                > {
                >>
                > //CHANGE BG COLOR
                >>
                > };
                >>
                > }
                >>
                > }
                >>
                >How can I know wich textbox must I change background color? How can I
                >know wich sender has triggered the event?
                >>
                >Thanks.
                >>
                >"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                >escreveu na mensagem news:eaH3TltUIH A.3916@TK2MSFTN GP02.phx.gbl...
                >>Marco,
                >>>
                >> When cycling through the Controls collection, you want to do
                >>something like this:
                >>>
                >>foreach (Control control in myForm.Controls )
                >>{
                >> // The textbox control.
                >> TextBox textBox = control as TextBox;
                >>>
                >> // If not null, set the handler.
                >> if (textBox != null)
                >> {
                >> // Set the handler.
                >> textBox.Enter += OnTextBoxEnter;
                >> }
                >>}
                >>>
                >> Of course OnTextBoxEnter is the method that sets the color to red.
                >>You could use anonymous methods in C# 2.0 or a lambda expression in C#
                >>3.0 to make it even simpler.
                >>>
                >>>
                >>--
                >> - Nicholas Paldino [.NET/C# MVP]
                >> - mvp@spam.guard. caspershouse.co m
                >>>
                >>"Marco Pais" <marco.pais[at]gmail.comwrote in message
                >>news:eTWiLbtU IHA.1204@TK2MSF TNGP03.phx.gbl. ..
                >>>Hi there.
                >>>>
                >>>Thanks for the reply.
                >>>>
                >>>I'm not sure I understand "...just use the as operator...".
                >>>>
                >>>I think I must use something with "+=" bu I don't know how...
                >>>>
                >>>Thanks
                >>>>
                >>>"Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om>
                >>>escreveu na mensagem news:OpNSNItUIH A.3364@TK2MSFTN GP03.phx.gbl...
                >>>>Marco,
                >>>>>
                >>>> First, you would change your code to this:
                >>>>>
                >>>>((TextBox ) sender).BackCol or = Color.Red;
                >>>>>
                >>>> Then, on your form, you would cycle through all the TextBox
                >>>>controls (the Controls collection will help you with this, just use
                >>>>the as operator to determine if the control is a textbox) and then
                >>>>attach that method to the Enter event handler.
                >>>>>
                >>>> You might want to make sure you attach another event handler to
                >>>>change the color back when the textbox loses focus.
                >>>>>
                >>>>>
                >>>>--
                >>>> - Nicholas Paldino [.NET/C# MVP]
                >>>> - mvp@spam.guard. caspershouse.co m
                >>>>>
                >>>>"Marco Pais" <marco.pais[at]gmail.comwrote in message
                >>>>news:eggpAB tUIHA.3364@TK2M SFTNGP03.phx.gb l...
                >>>>>Hi there.
                >>>>>>
                >>>>>How can I change the background color of a textbox when it gets the
                >>>>>focus?
                >>>>>>
                >>>>>I can handle the "Enter" event and do this
                >>>>>private void txtDummie_Enter (object sender, EventArgs e) {
                >>>>>>
                >>>>> txtDummie.BackC olor=Color.Red;
                >>>>>>
                >>>>>}
                >>>>>>
                >>>>>But how can I use this code to work with all textboxes in form? How
                >>>>>can I use "sender" argument?
                >>>>>>
                >>>>>Thanks in advance.
                >>>>>>
                >>>>>Regards,
                >>>>>>
                >>>>>Marco
                >>>>>>
                >>>>>>
                >>>>>
                >>>>>
                >>>>
                >>>>
                >>>
                >>>
                >>
                >>
                >
                >

                Comment

                • Nicholas Paldino [.NET/C# MVP]

                  #9
                  Re: Textbox background color

                  If I am not mistaken, that is what the other threads suggest. =)


                  --
                  - Nicholas Paldino [.NET/C# MVP]
                  - mvp@spam.guard. caspershouse.co m

                  "Greg" <gcadmes@gmail. comwrote in message
                  news:cd462477-7d03-4878-b72a-9450c3773db9@q3 9g2000hsf.googl egroups.com...
                  On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.comwrote:
                  >Hi there.
                  >>
                  >How can I change the background color of a textbox when it gets the
                  >focus?
                  >>
                  >I can handle the "Enter" event and do this
                  >private void txtDummie_Enter (object sender, EventArgs e) {
                  >>
                  > txtDummie.BackC olor=Color.Red;
                  >>
                  >}
                  >>
                  >But how can I use this code to work with all textboxes in form? How can I
                  >use "sender" argument?
                  >>
                  >Thanks in advance.
                  >>
                  >Regards,
                  >>
                  >Marco
                  >
                  Actually, it's even easier than the suggestions from other threads...
                  >
                  Instead of iterating over each control on your form, simply add one
                  event handler like so:
                  private void OnTextBoxEnter( object sender, EventArgs e)
                  {
                  ((TextBox)sende r).BackColor = Color.Red;
                  }
                  >
                  Then, make sure each text box subscribes to this event handler method.
                  >
                  Cheers,
                  >
                  Greg

                  Comment

                  Working...