how to group similar events?

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

    how to group similar events?

    Hello,

    I have a group of textboxes where I change the text to lower on leave, but I
    am sure there is a more efficient way to do this.

    rivate Sub txt1_Leave(...) Handles txt1.Leave
    Dim str1 As String = txt1.Text.ToLow er
    txt1.Text = str1
    End Sub

    Private Sub txt2_Leave(...) Handles txt2.Leave
    Dim str1 As String = txt2.Text.ToLow er
    txt2.Text = str1
    End Sub

    Private Sub txtI3_Leave(... ) Handles txtIDfld.Leave
    Dim str1 As String = txtIDfld.Text.T oLower
    txtIDfld.Text = str1
    End Sub

    Private Sub txt4_Leave(...) Handles txt4.Leave
    Dim str1 As String = txt4.Text.ToLow er
    txt4.Text = str1
    End Sub


    I think the above can be replaced with something like

    Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
    handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
    Dim str1 As String = sender.ToString .ToLower
    sender = str1
    End Sub

    this line seems to work - str1 appears to get the value from sender
    Dim str1 As String = sender.ToString .ToLower

    But when I try to reapply the new value to sender as below

    sender = str1

    nothing happends. The text in the textbox did not get changed. Any
    suggestions appreciated what I could do to make this work.

    Thanks,
    Rich
  • Chris

    #2
    Re: how to group similar events?

    Rich wrote:[color=blue]
    > Hello,
    >
    > I have a group of textboxes where I change the text to lower on leave, but I
    > am sure there is a more efficient way to do this.
    >
    > rivate Sub txt1_Leave(...) Handles txt1.Leave
    > Dim str1 As String = txt1.Text.ToLow er
    > txt1.Text = str1
    > End Sub
    >
    > Private Sub txt2_Leave(...) Handles txt2.Leave
    > Dim str1 As String = txt2.Text.ToLow er
    > txt2.Text = str1
    > End Sub
    >
    > Private Sub txtI3_Leave(... ) Handles txtIDfld.Leave
    > Dim str1 As String = txtIDfld.Text.T oLower
    > txtIDfld.Text = str1
    > End Sub
    >
    > Private Sub txt4_Leave(...) Handles txt4.Leave
    > Dim str1 As String = txt4.Text.ToLow er
    > txt4.Text = str1
    > End Sub
    >
    >
    > I think the above can be replaced with something like
    >
    > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
    > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
    > Dim str1 As String = sender.ToString .ToLower
    > sender = str1
    > End Sub
    >
    > this line seems to work - str1 appears to get the value from sender
    > Dim str1 As String = sender.ToString .ToLower
    >
    > But when I try to reapply the new value to sender as below
    >
    > sender = str1
    >
    > nothing happends. The text in the textbox did not get changed. Any
    > suggestions appreciated what I could do to make this work.
    >
    > Thanks,
    > Rich[/color]



    Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
    handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
    Dim str1 As String = DirectCast(send er, TextBox).Text.T oLower
    DirectCast(send er, TextBox).Text = str1
    End Sub

    If you turned on "Option Strict On" at the top of your class you would
    have be told about this problem. I recommend always using it.

    Chris

    Comment

    • Rich

      #3
      Re: how to group similar events?

      Thank you that worked. FYI, I did have option strict on. I did think I
      would get a message about sender, but I didn't Hmmm.

      Anyway, thanks for your help.

      "Chris" wrote:
      [color=blue]
      > Rich wrote:[color=green]
      > > Hello,
      > >
      > > I have a group of textboxes where I change the text to lower on leave, but I
      > > am sure there is a more efficient way to do this.
      > >
      > > rivate Sub txt1_Leave(...) Handles txt1.Leave
      > > Dim str1 As String = txt1.Text.ToLow er
      > > txt1.Text = str1
      > > End Sub
      > >
      > > Private Sub txt2_Leave(...) Handles txt2.Leave
      > > Dim str1 As String = txt2.Text.ToLow er
      > > txt2.Text = str1
      > > End Sub
      > >
      > > Private Sub txtI3_Leave(... ) Handles txtIDfld.Leave
      > > Dim str1 As String = txtIDfld.Text.T oLower
      > > txtIDfld.Text = str1
      > > End Sub
      > >
      > > Private Sub txt4_Leave(...) Handles txt4.Leave
      > > Dim str1 As String = txt4.Text.ToLow er
      > > txt4.Text = str1
      > > End Sub
      > >
      > >
      > > I think the above can be replaced with something like
      > >
      > > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
      > > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
      > > Dim str1 As String = sender.ToString .ToLower
      > > sender = str1
      > > End Sub
      > >
      > > this line seems to work - str1 appears to get the value from sender
      > > Dim str1 As String = sender.ToString .ToLower
      > >
      > > But when I try to reapply the new value to sender as below
      > >
      > > sender = str1
      > >
      > > nothing happends. The text in the textbox did not get changed. Any
      > > suggestions appreciated what I could do to make this work.
      > >
      > > Thanks,
      > > Rich[/color]
      >
      >
      >
      > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
      > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
      > Dim str1 As String = DirectCast(send er, TextBox).Text.T oLower
      > DirectCast(send er, TextBox).Text = str1
      > End Sub
      >
      > If you turned on "Option Strict On" at the top of your class you would
      > have be told about this problem. I recommend always using it.
      >
      > Chris
      >[/color]

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: how to group similar events?

        "Rich" <Rich@discussio ns.microsoft.co m> schrieb:[color=blue]
        > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
        > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
        > Dim str1 As String = sender.ToString .ToLower
        > sender = str1
        > End Sub
        >
        > this line seems to work - str1 appears to get the value from sender
        > Dim str1 As String = sender.ToString .ToLower
        >
        > But when I try to reapply the new value to sender as below
        >
        > sender = str1[/color]

        'DirectCast(sen der, TextBox).Text = str1'.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Chris

          #5
          Re: how to group similar events?

          Rich wrote:[color=blue]
          > Thank you that worked. FYI, I did have option strict on. I did think I
          > would get a message about sender, but I didn't Hmmm.
          >
          > Anyway, thanks for your help.
          >
          > "Chris" wrote:
          >
          >[color=green]
          >>Rich wrote:
          >>[color=darkred]
          >>>Hello,
          >>>
          >>>I have a group of textboxes where I change the text to lower on leave, but I
          >>>am sure there is a more efficient way to do this.
          >>>
          >>>rivate Sub txt1_Leave(...) Handles txt1.Leave
          >>> Dim str1 As String = txt1.Text.ToLow er
          >>> txt1.Text = str1
          >>> End Sub
          >>>
          >>> Private Sub txt2_Leave(...) Handles txt2.Leave
          >>> Dim str1 As String = txt2.Text.ToLow er
          >>> txt2.Text = str1
          >>> End Sub
          >>>
          >>> Private Sub txtI3_Leave(... ) Handles txtIDfld.Leave
          >>> Dim str1 As String = txtIDfld.Text.T oLower
          >>> txtIDfld.Text = str1
          >>> End Sub
          >>>
          >>> Private Sub txt4_Leave(...) Handles txt4.Leave
          >>> Dim str1 As String = txt4.Text.ToLow er
          >>> txt4.Text = str1
          >>> End Sub
          >>>
          >>>
          >>>I think the above can be replaced with something like
          >>>
          >>>Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
          >>>handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
          >>> Dim str1 As String = sender.ToString .ToLower
          >>> sender = str1
          >>>End Sub
          >>>
          >>>this line seems to work - str1 appears to get the value from sender
          >>> Dim str1 As String = sender.ToString .ToLower
          >>>
          >>>But when I try to reapply the new value to sender as below
          >>>
          >>> sender = str1
          >>>
          >>>nothing happends. The text in the textbox did not get changed. Any
          >>>suggestion s appreciated what I could do to make this work.
          >>>
          >>>Thanks,
          >>>Rich[/color]
          >>
          >>
          >>
          >>Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
          >>handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
          >> Dim str1 As String = DirectCast(send er, TextBox).Text.T oLower
          >> DirectCast(send er, TextBox).Text = str1
          >>End Sub
          >>
          >>If you turned on "Option Strict On" at the top of your class you would
          >>have be told about this problem. I recommend always using it.
          >>
          >>Chris
          >>[/color][/color]

          Actually you wouldn't get the error now that look at it.


          'This line you do a string to a string
          Dim str1 As String = sender.ToString .ToLower

          This one you assign the object to a string
          sender = str1

          Both of them are allowed since it is an object.
          If you just did:
          Dim str1 As String = sender
          You'd get an error.

          Chris

          Comment

          • Dennis

            #6
            Re: how to group similar events?

            A better way to do it might be in the KeyAscii event to chane upper case keys
            to lower case keys so the user sees only lower case being entered into the
            text box.
            --
            Dennis in Houston


            "Rich" wrote:
            [color=blue]
            > Thank you that worked. FYI, I did have option strict on. I did think I
            > would get a message about sender, but I didn't Hmmm.
            >
            > Anyway, thanks for your help.
            >
            > "Chris" wrote:
            >[color=green]
            > > Rich wrote:[color=darkred]
            > > > Hello,
            > > >
            > > > I have a group of textboxes where I change the text to lower on leave, but I
            > > > am sure there is a more efficient way to do this.
            > > >
            > > > rivate Sub txt1_Leave(...) Handles txt1.Leave
            > > > Dim str1 As String = txt1.Text.ToLow er
            > > > txt1.Text = str1
            > > > End Sub
            > > >
            > > > Private Sub txt2_Leave(...) Handles txt2.Leave
            > > > Dim str1 As String = txt2.Text.ToLow er
            > > > txt2.Text = str1
            > > > End Sub
            > > >
            > > > Private Sub txtI3_Leave(... ) Handles txtIDfld.Leave
            > > > Dim str1 As String = txtIDfld.Text.T oLower
            > > > txtIDfld.Text = str1
            > > > End Sub
            > > >
            > > > Private Sub txt4_Leave(...) Handles txt4.Leave
            > > > Dim str1 As String = txt4.Text.ToLow er
            > > > txt4.Text = str1
            > > > End Sub
            > > >
            > > >
            > > > I think the above can be replaced with something like
            > > >
            > > > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
            > > > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
            > > > Dim str1 As String = sender.ToString .ToLower
            > > > sender = str1
            > > > End Sub
            > > >
            > > > this line seems to work - str1 appears to get the value from sender
            > > > Dim str1 As String = sender.ToString .ToLower
            > > >
            > > > But when I try to reapply the new value to sender as below
            > > >
            > > > sender = str1
            > > >
            > > > nothing happends. The text in the textbox did not get changed. Any
            > > > suggestions appreciated what I could do to make this work.
            > > >
            > > > Thanks,
            > > > Rich[/color]
            > >
            > >
            > >
            > > Private Sub onLeaving(ByVal sender As Object, ByVal e As System.EventArg s)
            > > handles txt1.leave, txt2.leave, txt3.leave, txt4.leave
            > > Dim str1 As String = DirectCast(send er, TextBox).Text.T oLower
            > > DirectCast(send er, TextBox).Text = str1
            > > End Sub
            > >
            > > If you turned on "Option Strict On" at the top of your class you would
            > > have be told about this problem. I recommend always using it.
            > >
            > > Chris
            > >[/color][/color]

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: how to group similar events?

              Rich,

              Be aware that there are here two criteria for efficient. It is more
              efficient to write and maintain. Therefore I use it forever as showed by
              others.

              However it is less efficient to process (what is a very very very little
              bit).

              Cor


              Comment

              Working...