control array question for VB.Net 2005

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I have an application that contains several checkboxes. I originally
created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I
understand the vb2005 supports control arrays. Is this correct? If so, I
would like to convert my checkboxes to a control array, but without having to
recreate them from scratch because their placement was a real pain. Is it
possible to go to the property sheet of each checkbox and assign it a
position in the control array? like checkbox0 would be chk(0), and checkbox1
would be chk(1). Am I doing this correctly?

Thanks,
Rich
 
Rich said:
Hello,

I have an application that contains several checkboxes. I originally
created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I
understand the vb2005 supports control arrays. Is this correct? If so, I
would like to convert my checkboxes to a control array, but without having to
recreate them from scratch because their placement was a real pain. Is it
possible to go to the property sheet of each checkbox and assign it a
position in the control array? like checkbox0 would be chk(0), and checkbox1
would be chk(1). Am I doing this correctly?

Thanks,
Rich

I'm a bit confused on what you are trying to do. You could always go:

Dim Arr(10) as Array
Arr(0) = CheckBox1

That being said you could do this at the begining of your form load and
then have your control array without changing the declaration of the
controls.

Chris
 
Thanks for your reply. Actually, I did do the array thing. Then I tried to
overload the OnEnter event of the controls (actually for textboxes, which is
where I was going to go next with the control array thing). Here is what I
have so far:

Dim arrCtrl As Control()

Private Sub From1_Load(...)
arrCtrl = New TextBox(){txt1, txt2, txt3}
....
End Sub

Private Overloads Sub OnEnter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
arrCtlr().enter
MessageBox.Show("Textbox " & CType(sender, TextBox).Name)
End Sub

I have a problem with ...Handles arrCtlr().Enter
I have tried ...Handles arrCtlr.Enter but I get an error message that says
something about needing to use WithEvents. What is the correct syntax for
passing an array of controls to an overloaded Event procedure?

Thanks,
Rich
 
Write a method to centralize your calls:
Private Sub HandleEnter(ByVal sender As Object, ByVal e As System.EventArgs)
'...
End Sub

Then, for each control in your array, use the AddHandler statement, pointing the Enter event of textbox to that procedure:
AddHandler Textbox.Enter, AddressOf HandleEnter

When you close the form, call the RemoveHandler statement.

That's all.

[]s
Cesar
 
Thanks. I sort of get it. How do I invoke/call AddHandler? I enter a
textbox with the mouse. How does AddHandler get called? My goal is to avoid
writing the same routine 20 times for 20 textboxes.

Private Sub txt1_OnEnter(...) Handles...

How do I write the Addhandler so I only have one routine for all of my
textboxes? Thinking outloud here, do I add the AddHandler call in the array?

arrCtl = new Control(){txt1, txt2, txt3}

I don't know how to add the AddHandler call.


ronchese said:
Write a method to centralize your calls:
Private Sub HandleEnter(ByVal sender As Object, ByVal e As System.EventArgs)
'...
End Sub

Then, for each control in your array, use the AddHandler statement, pointing the Enter event of textbox to that procedure:
AddHandler Textbox.Enter, AddressOf HandleEnter

When you close the form, call the RemoveHandler statement.

That's all.

[]s
Cesar









Rich said:
Thanks for your reply. Actually, I did do the array thing. Then I tried to
overload the OnEnter event of the controls (actually for textboxes, which is
where I was going to go next with the control array thing). Here is what I
have so far:

Dim arrCtrl As Control()

Private Sub From1_Load(...)
arrCtrl = New TextBox(){txt1, txt2, txt3}
...
End Sub

Private Overloads Sub OnEnter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
arrCtlr().enter
MessageBox.Show("Textbox " & CType(sender, TextBox).Name)
End Sub

I have a problem with ...Handles arrCtlr().Enter
I have tried ...Handles arrCtlr.Enter but I get an error message that says
something about needing to use WithEvents. What is the correct syntax for
passing an array of controls to an overloaded Event procedure?

Thanks,
Rich
 
I figured it out.

Dim ctl as control()
Private Sub Form_Load(...)
ctl = new ctl(){txt1, txt2, txt3}
Dim txt As TextBox
For Each txt In ctl
AddHandler txt.Enter, AddressOf HandleEnter
next

Private Sub HandleEnter(...)
MessageBox.Show(Ctype(sender, TextBox).Name)
End Sub

Rich said:
Thanks. I sort of get it. How do I invoke/call AddHandler? I enter a
textbox with the mouse. How does AddHandler get called? My goal is to avoid
writing the same routine 20 times for 20 textboxes.

Private Sub txt1_OnEnter(...) Handles...

How do I write the Addhandler so I only have one routine for all of my
textboxes? Thinking outloud here, do I add the AddHandler call in the array?

arrCtl = new Control(){txt1, txt2, txt3}

I don't know how to add the AddHandler call.


ronchese said:
Write a method to centralize your calls:
Private Sub HandleEnter(ByVal sender As Object, ByVal e As System.EventArgs)
'...
End Sub

Then, for each control in your array, use the AddHandler statement, pointing the Enter event of textbox to that procedure:
AddHandler Textbox.Enter, AddressOf HandleEnter

When you close the form, call the RemoveHandler statement.

That's all.

[]s
Cesar









Rich said:
Thanks for your reply. Actually, I did do the array thing. Then I tried to
overload the OnEnter event of the controls (actually for textboxes, which is
where I was going to go next with the control array thing). Here is what I
have so far:

Dim arrCtrl As Control()

Private Sub From1_Load(...)
arrCtrl = New TextBox(){txt1, txt2, txt3}
...
End Sub

Private Overloads Sub OnEnter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles
arrCtlr().enter
MessageBox.Show("Textbox " & CType(sender, TextBox).Name)
End Sub

I have a problem with ...Handles arrCtlr().Enter
I have tried ...Handles arrCtlr.Enter but I get an error message that says
something about needing to use WithEvents. What is the correct syntax for
passing an array of controls to an overloaded Event procedure?

Thanks,
Rich


:

Rich wrote:
Hello,

I have an application that contains several checkboxes. I originally
created this app in VB.Net 2003 and upgraded the app to VB.Net 2005. I
understand the vb2005 supports control arrays. Is this correct? If so, I
would like to convert my checkboxes to a control array, but without having to
recreate them from scratch because their placement was a real pain. Is it
possible to go to the property sheet of each checkbox and assign it a
position in the control array? like checkbox0 would be chk(0), and checkbox1
would be chk(1). Am I doing this correctly?

Thanks,
Rich

I'm a bit confused on what you are trying to do. You could always go:

Dim Arr(10) as Array
Arr(0) = CheckBox1

That being said you could do this at the begining of your form load and
then have your control array without changing the declaration of the
controls.

Chris
 
Yeah!

:^D


Rich said:
I figured it out.

Dim ctl as control()
Private Sub Form_Load(...)
ctl = new ctl(){txt1, txt2, txt3}
Dim txt As TextBox
For Each txt In ctl
AddHandler txt.Enter, AddressOf HandleEnter
next

Private Sub HandleEnter(...)
MessageBox.Show(Ctype(sender, TextBox).Name)
End Sub

Rich said:
Thanks. I sort of get it. How do I invoke/call AddHandler? I enter a
textbox with the mouse. How does AddHandler get called? My goal is to
avoid
writing the same routine 20 times for 20 textboxes.

Private Sub txt1_OnEnter(...) Handles...

How do I write the Addhandler so I only have one routine for all of my
textboxes? Thinking outloud here, do I add the AddHandler call in the
array?

arrCtl = new Control(){txt1, txt2, txt3}

I don't know how to add the AddHandler call.


ronchese said:
Write a method to centralize your calls:
Private Sub HandleEnter(ByVal sender As Object, ByVal e As
System.EventArgs)
'...
End Sub

Then, for each control in your array, use the AddHandler statement,
pointing the Enter event of textbox to that procedure:
AddHandler Textbox.Enter, AddressOf HandleEnter

When you close the form, call the RemoveHandler statement.

That's all.

[]s
Cesar









Thanks for your reply. Actually, I did do the array thing. Then I
tried to
overload the OnEnter event of the controls (actually for textboxes,
which is
where I was going to go next with the control array thing). Here is
what I
have so far:

Dim arrCtrl As Control()

Private Sub From1_Load(...)
arrCtrl = New TextBox(){txt1, txt2, txt3}
...
End Sub

Private Overloads Sub OnEnter(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Handles
arrCtlr().enter
MessageBox.Show("Textbox " & CType(sender, TextBox).Name)
End Sub

I have a problem with ...Handles arrCtlr().Enter
I have tried ...Handles arrCtlr.Enter but I get an error message that
says
something about needing to use WithEvents. What is the correct
syntax for
passing an array of controls to an overloaded Event procedure?

Thanks,
Rich


:

Rich wrote:
Hello,

I have an application that contains several checkboxes. I
originally
created this app in VB.Net 2003 and upgraded the app to VB.Net
2005. I
understand the vb2005 supports control arrays. Is this correct?
If so, I
would like to convert my checkboxes to a control array, but
without having to
recreate them from scratch because their placement was a real
pain. Is it
possible to go to the property sheet of each checkbox and assign
it a
position in the control array? like checkbox0 would be chk(0),
and checkbox1
would be chk(1). Am I doing this correctly?

Thanks,
Rich

I'm a bit confused on what you are trying to do. You could always
go:

Dim Arr(10) as Array
Arr(0) = CheckBox1

That being said you could do this at the begining of your form load
and
then have your control array without changing the declaration of the
controls.

Chris
 
Back
Top