simple q about sub's

hey

I have this code:-

Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave

If textbox1.Text = "" Then
textbox1.Text = "0.00"
End If
End Sub

Only problem is that I have 20 textboxes ( all in the format textboxn) that I want this code to be performed on. At the moment I have this sub repeated 20 times, is there a way to make this code more flexible?

thanks

Joe

Comments

  • You can access the control collection for your form, and check for the textbox type. there are examples on this forum search for 'Typeof' 'Gettype' or '.controls' and you should find it.

    ~rlc
  • : hey
    :
    : I have this code:-
    :
    : Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    :
    : If textbox1.Text = "" Then
    : textbox1.Text = "0.00"
    : End If
    : End Sub
    :
    : Only problem is that I have 20 textboxes ( all in the format textboxn) that I want this code to be performed on. At the moment I have this sub repeated 20 times, is there a way to make this code more flexible?
    :
    : thanks
    :
    : Joe
    :
    Try This
    [Code]
    Public Sub MyProcedure (InputControl as Object)
    If InputControl.Text = "" Then ' Try - InputControl.Text.Length = 0
    InsputControl.Text = "0.00"
    End If
    End Sub

    Private Sub textbox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    Me.MyProcedure(TextBox1)
    End sub

    Private Sub textbox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    Me.MyProcedure(TextBox1)
    End sub

    Private Sub textbox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    Me.MyProcedure(TextBox2)
    End sub

    Private Sub textbox3_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    Me.MyProcedure(TextBox3)
    End sub
    [/Code]
    Call like that all of your 20 texbox's leave event.

    Shahzad Munir.
  • : : hey
    : :
    : : I have this code:-
    : :
    : : Private Sub textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    : :
    : : If textbox1.Text = "" Then
    : : textbox1.Text = "0.00"
    : : End If
    : : End Sub
    : :
    : : Only problem is that I have 20 textboxes ( all in the format textboxn) that I want this code to be performed on. At the moment I have this sub repeated 20 times, is there a way to make this code more flexible?
    : :
    : : thanks
    : :
    : : Joe
    : :
    : Try This
    : [Code]
    : Public Sub MyProcedure (InputControl as Object)
    : If InputControl.Text = "" Then ' Try - InputControl.Text.Length = 0
    : InsputControl.Text = "0.00"
    : End If
    : End Sub
    :
    : Private Sub textbox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    : Me.MyProcedure(TextBox1)
    : End sub
    :
    : Private Sub textbox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    : Me.MyProcedure(TextBox1)
    : End sub
    :
    : Private Sub textbox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    : Me.MyProcedure(TextBox2)
    : End sub
    :
    : Private Sub textbox3_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave
    : Me.MyProcedure(TextBox3)
    : End sub
    : [/Code]
    : Call like that all of your 20 texbox's leave event.
    :
    : Shahzad Munir.
    :
    much easier way

    [code]
    Private Sub textbox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox1.Leave, ADD ALL TEXTBOX.LEAVE HERE
    If sender.Text = "" Then
    sender.Text = "0.00"
    End If
    [/code]

    where it says add all textbox.leave here do eaxactly that. ie textbox2.leave, textbo3.leave,textbox4.leave etc.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories