Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding a new control at runtime. how to define its event code

1 view
Skip to first unread message

Wheres Wally

unread,
May 11, 2003, 8:04:01 PM5/11/03
to
Hi

I current have a combobox on a form that I created in design mode. I can
therefore simply dbl click the control, and code the actions to occur when
events are fired such as when the user changes the selected item.

Now I need to add a combo to my toolbar, and the only way to do it is in
code at run time :

Dim myCombo as new combo()
toolbar1.controls.add(myCombo)

This add a combo box to my toolbar, but of course there are no event linked
to this control.

I want to do things(look up sql tables and so on) when the user changes the
selected item in the combo, but have no idea where to put the code, and how
to make the code fire when the event action happens

What do I do ?


Mattias Sjögren

unread,
May 11, 2003, 9:07:00 PM5/11/03
to

>I want to do things(look up sql tables and so on) when the user changes the
>selected item in the combo, but have no idea where to put the code,

Write a method such as

Private Sub YourHandlerMethod(sender As Object, e As EventArgs)
...
End Sub


>and how
>to make the code fire when the event action happens

AddHandler myCombo.SelectedIndexChanged, AddressOf YourHandlerMethod

Mattias

===
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Herfried K. Wagner

unread,
May 11, 2003, 10:11:12 PM5/11/03
to
Hello,

"Wheres Wally" <no...@none.com> schrieb:


> Dim myCombo as new combo()
> toolbar1.controls.add(myCombo)
>
> This add a combo box to my toolbar, but of course there are
> no event linked to this control.

AddHandler (and RemoveHandler):

\\\
Private Sub Test()
Dim btn As New Button()
With btn
.Left = 0
.Top = 0
.Width = 100
.Height = 100
End With
AddHandler btn.Click, AddressOf Me.MyButton_Click
Me.Controls.Add(btn)
End Sub

Private Sub MyButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
)
MsgBox("It works!")
End Sub
///

Regards,
Herfried K. Wagner


Dhaval Faria

unread,
May 12, 2003, 1:27:51 AM5/12/03
to
well, to make a controls at a runtime with the events you
need to create a class which inherits control.. for
example check out the following code:

In the following exaplme, I am creating textbox at a
runtime, and then I am adding event in it.

first create a class file.

Public Class clsTitleText
Inherits System.Windows.Forms.Label

Public a As Integer
Public b As Integer

Private Sub lblTitleText_MouseDown(ByVal sender As
Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Handles MyBase.MouseDown
MsgBox ("Event Fired")
End Sub

End Class


and now add the following code when u want to create a
control at runtime:

Dim lblTitleText As New clsTitleText()
lblTitleText.Text = "This is runtime designed textbox"
lblTitleText.Height = "85"
lblTitleText.Width = "30"
Dim myPoint As New Point()
myPoint.X = "0"
myPoint.Y = "0"
lblTitleText.Location = myPoint
Form1.Controls.Add(lblTitleText)

I hope this helps,
Dhaval Faria.

Amith

unread,
May 12, 2003, 5:03:50 AM5/12/03
to
Try this....

Dim ctlExtender As VBControlExtender

Private Sub Form_Load()
Set ctlExtender = Controls.Add
("Project1.UserControl1", "MyControl")
With ctlExtender
.Visible = True
.Top = 1000
.Left = 1000
End With
End Sub

Private Sub extObj_ObjectEvent(Info As EventInfo)
' Program the events of the control using Select Case.
Select Case Info.Name
Case "UserName"
' Check User name value.
MsgBox Info.EventParameters("UserName").Value
' Other cases now shown
Case Else ' Unknown Event
' Handle unknown events here.
End Select
End Sub

0 new messages