possible to create one control array with different controls?

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

    #1

    possible to create one control array with different controls?

    Hello,

    I have 3 textboxes and 1 combobox on a form. On entering the control I want
    to select all the text. I can make an array of textboxes like this:

    Dim arrTxt As TextBox() = {txt1, txt2, txt3}

    Then I loop through that array

    Private Sub onEntering(ByVa l sender As Object, ...) Handles _
    txt1.Enter,
    txt2.Enter, txt3.Enter
    CType(sender, TextBox).Select All()
    End Sub

    Is it possible to create a similar control array that I could stuff the
    textboxes and the combobox into? If yes, how is this done? What kind of
    control object could I use to hold different controls? And how do I check if
    sender is a
    Textbox or combobox?

    If CType(sender, TextBox) = True Then...? is this doable?
    If CType(sender, ComboBox) = True Then...?

    Thanks,
    Rich
  • CMM

    #2
    Re: possible to create one control array with different controls?

    "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
    news:6EBE1741-B817-4039-A04B-598771510A9F@mi crosoft.com...[color=blue]
    > Hello,
    >
    > I have 3 textboxes and 1 combobox on a form. On entering the control I
    > want
    > to select all the text. I can make an array of textboxes like this:
    >
    > Dim arrTxt As TextBox() = {txt1, txt2, txt3}
    >
    > Then I loop through that array
    >
    > Private Sub onEntering(ByVa l sender As Object, ...) Handles _
    > txt1.Enter,
    > txt2.Enter, txt3.Enter
    > CType(sender, TextBox).Select All()
    > End Sub
    >
    > Is it possible to create a similar control array that I could stuff the
    > textboxes and the combobox into? If yes, how is this done? What kind of
    > control object could I use to hold different controls?[/color]

    Dim arr As Control() = {txt1, txt2, txt3}
    or
    Dim arr As Object() = {txt1, txt2, txt3}

    [color=blue]
    > And how do I check if
    > sender is a
    > Textbox or combobox?
    >
    > If CType(sender, TextBox) = True Then...? is this doable?
    > If CType(sender, ComboBox) = True Then...?[/color]

    No, you would use:

    If TypeOf sender Is TextBox Then
    ElseIf TypeOf sender Is ComboBox Then

    Also, make sure your event handler handles the lowest common denominator in
    terms of EventArgs. All EventArgs inherit from System.EventArg s... so use
    that.

    arr_Click(ByVal sender As System.Object, ByVal e As System.EventArg s)
    Handles ...

    NOT

    arr_Click(ByVal sender As System.Object, ByVal e As
    SomeListViewSpe cialEventArgs) Handles ...


    --
    -C. Moya



    Comment

    • Rich

      #3
      Re: possible to create one control array with different controls?

      Thanks very much for your reply. Your suggestion worked perfectly!
      (for posterity here is what I did)

      Dim arr As Control()

      Private Sub Form1_Load(ByVa l sender As System.Object, _
      ByVal e As System.EventArg s) Handles MyBase.Load
      arr = New Control() {txt1, txt2, txt3, txt4, cbo1}
      End Sub

      Private Overloads Sub OnEnter(ByVal sender As System.Object, _
      ByVal e As System.EventArg s) Handles _
      txt1.Enter, txt2.Enter, txt3.Enter,
      txt4.Enter, cbo1.Enter
      If TypeOf sender Is TextBox Then
      MessageBox.Show ("Textbox " & CType(sender, TextBox).Name)
      ElseIf TypeOf sender Is ComboBox Then
      MessageBox.Show ("Combobox " & CType(sender, ComboBox).Name)
      End If
      End Sub


      "CMM" wrote:
      [color=blue]
      > "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
      > news:6EBE1741-B817-4039-A04B-598771510A9F@mi crosoft.com...[color=green]
      > > Hello,
      > >
      > > I have 3 textboxes and 1 combobox on a form. On entering the control I
      > > want
      > > to select all the text. I can make an array of textboxes like this:
      > >
      > > Dim arrTxt As TextBox() = {txt1, txt2, txt3}
      > >
      > > Then I loop through that array
      > >
      > > Private Sub onEntering(ByVa l sender As Object, ...) Handles _
      > > txt1.Enter,
      > > txt2.Enter, txt3.Enter
      > > CType(sender, TextBox).Select All()
      > > End Sub
      > >
      > > Is it possible to create a similar control array that I could stuff the
      > > textboxes and the combobox into? If yes, how is this done? What kind of
      > > control object could I use to hold different controls?[/color]
      >
      > Dim arr As Control() = {txt1, txt2, txt3}
      > or
      > Dim arr As Object() = {txt1, txt2, txt3}
      >
      >[color=green]
      > > And how do I check if
      > > sender is a
      > > Textbox or combobox?
      > >
      > > If CType(sender, TextBox) = True Then...? is this doable?
      > > If CType(sender, ComboBox) = True Then...?[/color]
      >
      > No, you would use:
      >
      > If TypeOf sender Is TextBox Then
      > ElseIf TypeOf sender Is ComboBox Then
      >
      > Also, make sure your event handler handles the lowest common denominator in
      > terms of EventArgs. All EventArgs inherit from System.EventArg s... so use
      > that.
      >
      > arr_Click(ByVal sender As System.Object, ByVal e As System.EventArg s)
      > Handles ...
      >
      > NOT
      >
      > arr_Click(ByVal sender As System.Object, ByVal e As
      > SomeListViewSpe cialEventArgs) Handles ...
      >
      >
      > --
      > -C. Moya
      > www.cmoya.com
      >
      >
      >[/color]

      Comment

      • CMM

        #4
        Re: possible to create one control array with different controls?

        Why not fill the array in the declaration instead of waiting until
        Form_Load?

        Private arr As Control() = {txt1, txt2, txt3, txt4, cbo1}

        Keep in mind that your form could be fully loaded and working, but Form_Load
        isn't called until it is *shown* for the first time (this behavior is
        different than VB.Classic). The chances of you (some method of yours or
        event or whatever) of accessing arr before that happens is great. Like this:

        Dim frm as New MyForm
        frm.SetSomeProp ertiesOfControl s()
        frm.Show()

        Doh! Exception on SetSomeProperti es(). My "arr" wasn't initialized yet!!!

        Also, "Private" not "Dim" is the common convention when declaring variables
        at the class level.

        --
        -C. Moya

        "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
        news:017A49C3-5E28-434D-95C1-DBAE00FC07F7@mi crosoft.com...[color=blue]
        > Thanks very much for your reply. Your suggestion worked perfectly!
        > (for posterity here is what I did)
        >
        > Dim arr As Control()
        >
        > Private Sub Form1_Load(ByVa l sender As System.Object, _
        > ByVal e As System.EventArg s) Handles
        > MyBase.Load
        > arr = New Control() {txt1, txt2, txt3, txt4, cbo1}
        > End Sub
        >
        > Private Overloads Sub OnEnter(ByVal sender As System.Object, _
        > ByVal e As System.EventArg s) Handles
        > _
        > txt1.Enter, txt2.Enter, txt3.Enter,
        > txt4.Enter, cbo1.Enter
        > If TypeOf sender Is TextBox Then
        > MessageBox.Show ("Textbox " & CType(sender, TextBox).Name)
        > ElseIf TypeOf sender Is ComboBox Then
        > MessageBox.Show ("Combobox " & CType(sender, ComboBox).Name)
        > End If
        > End Sub
        >
        >
        > "CMM" wrote:
        >[color=green]
        >> "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
        >> news:6EBE1741-B817-4039-A04B-598771510A9F@mi crosoft.com...[color=darkred]
        >> > Hello,
        >> >
        >> > I have 3 textboxes and 1 combobox on a form. On entering the control I
        >> > want
        >> > to select all the text. I can make an array of textboxes like this:
        >> >
        >> > Dim arrTxt As TextBox() = {txt1, txt2, txt3}
        >> >
        >> > Then I loop through that array
        >> >
        >> > Private Sub onEntering(ByVa l sender As Object, ...) Handles _
        >> > txt1.Enter,
        >> > txt2.Enter, txt3.Enter
        >> > CType(sender, TextBox).Select All()
        >> > End Sub
        >> >
        >> > Is it possible to create a similar control array that I could stuff the
        >> > textboxes and the combobox into? If yes, how is this done? What kind
        >> > of
        >> > control object could I use to hold different controls?[/color]
        >>
        >> Dim arr As Control() = {txt1, txt2, txt3}
        >> or
        >> Dim arr As Object() = {txt1, txt2, txt3}
        >>
        >>[color=darkred]
        >> > And how do I check if
        >> > sender is a
        >> > Textbox or combobox?
        >> >
        >> > If CType(sender, TextBox) = True Then...? is this doable?
        >> > If CType(sender, ComboBox) = True Then...?[/color]
        >>
        >> No, you would use:
        >>
        >> If TypeOf sender Is TextBox Then
        >> ElseIf TypeOf sender Is ComboBox Then
        >>
        >> Also, make sure your event handler handles the lowest common denominator
        >> in
        >> terms of EventArgs. All EventArgs inherit from System.EventArg s... so use
        >> that.
        >>
        >> arr_Click(ByVal sender As System.Object, ByVal e As System.EventArg s)
        >> Handles ...
        >>
        >> NOT
        >>
        >> arr_Click(ByVal sender As System.Object, ByVal e As
        >> SomeListViewSpe cialEventArgs) Handles ...
        >>
        >>
        >> --
        >> -C. Moya
        >> www.cmoya.com
        >>
        >>
        >>[/color][/color]


        Comment

        • CMM

          #5
          Re: possible to create one control array with different controls?

          Also the choice of name for your event handler, "OnEnter," is 100% BAD.
          That's a sub of the base FORM class which is why I surmise you put
          "Overloads" on (I take it the compiler complained and told you to put
          "overloads" and you did it without really knowing what it meant). Why not
          call your event handler something like
          MyControls_Ente r(...) Handles txt1.Enter, txt2.Enter ...

          --
          -C. Moya

          "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
          news:017A49C3-5E28-434D-95C1-DBAE00FC07F7@mi crosoft.com...[color=blue]
          > Thanks very much for your reply. Your suggestion worked perfectly!
          > (for posterity here is what I did)
          >
          > Dim arr As Control()
          >
          > Private Sub Form1_Load(ByVa l sender As System.Object, _
          > ByVal e As System.EventArg s) Handles
          > MyBase.Load
          > arr = New Control() {txt1, txt2, txt3, txt4, cbo1}
          > End Sub
          >
          > Private Overloads Sub OnEnter(ByVal sender As System.Object, _
          > ByVal e As System.EventArg s) Handles
          > _
          > txt1.Enter, txt2.Enter, txt3.Enter,
          > txt4.Enter, cbo1.Enter
          > If TypeOf sender Is TextBox Then
          > MessageBox.Show ("Textbox " & CType(sender, TextBox).Name)
          > ElseIf TypeOf sender Is ComboBox Then
          > MessageBox.Show ("Combobox " & CType(sender, ComboBox).Name)
          > End If
          > End Sub
          >
          >
          > "CMM" wrote:
          >[color=green]
          >> "Rich" <Rich@discussio ns.microsoft.co m> wrote in message
          >> news:6EBE1741-B817-4039-A04B-598771510A9F@mi crosoft.com...[color=darkred]
          >> > Hello,
          >> >
          >> > I have 3 textboxes and 1 combobox on a form. On entering the control I
          >> > want
          >> > to select all the text. I can make an array of textboxes like this:
          >> >
          >> > Dim arrTxt As TextBox() = {txt1, txt2, txt3}
          >> >
          >> > Then I loop through that array
          >> >
          >> > Private Sub onEntering(ByVa l sender As Object, ...) Handles _
          >> > txt1.Enter,
          >> > txt2.Enter, txt3.Enter
          >> > CType(sender, TextBox).Select All()
          >> > End Sub
          >> >
          >> > Is it possible to create a similar control array that I could stuff the
          >> > textboxes and the combobox into? If yes, how is this done? What kind
          >> > of
          >> > control object could I use to hold different controls?[/color]
          >>
          >> Dim arr As Control() = {txt1, txt2, txt3}
          >> or
          >> Dim arr As Object() = {txt1, txt2, txt3}
          >>
          >>[color=darkred]
          >> > And how do I check if
          >> > sender is a
          >> > Textbox or combobox?
          >> >
          >> > If CType(sender, TextBox) = True Then...? is this doable?
          >> > If CType(sender, ComboBox) = True Then...?[/color]
          >>
          >> No, you would use:
          >>
          >> If TypeOf sender Is TextBox Then
          >> ElseIf TypeOf sender Is ComboBox Then
          >>
          >> Also, make sure your event handler handles the lowest common denominator
          >> in
          >> terms of EventArgs. All EventArgs inherit from System.EventArg s... so use
          >> that.
          >>
          >> arr_Click(ByVal sender As System.Object, ByVal e As System.EventArg s)
          >> Handles ...
          >>
          >> NOT
          >>
          >> arr_Click(ByVal sender As System.Object, ByVal e As
          >> SomeListViewSpe cialEventArgs) Handles ...
          >>
          >>
          >> --
          >> -C. Moya
          >> www.cmoya.com
          >>
          >>
          >>[/color][/color]


          Comment

          Working...