Fabian_Russ Posted November 20, 2009 Posted November 20, 2009 (edited) Is it possible to program events and subs like the Click Event for a button that was declared without physically being on the form? Example: Dim NewButton as New Button How would I go about firing the click event and using it inside a button that is not physically in the form during design time, but instead a new instance of it being created during runtime? Any help is appreciated, Thanks :D Edited November 20, 2009 by Fabian_Russ Quote
Administrators PlausiblyDamp Posted November 20, 2009 Administrators Posted November 20, 2009 If you already have the actual event handler code written then you can attach it via the AddHandler keyword. e.g. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show("Clicked", "Clicked", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As New Button b.Text = "click me" b.Location = New Point(10, 10) Me.Controls.Add(b) AddHandler b.Click, AddressOf Button1_Click End Sub Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Fabian_Russ Posted November 20, 2009 Author Posted November 20, 2009 If you already have the actual event handler code written then you can attach it via the AddHandler keyword. e.g. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show("Clicked", "Clicked", MessageBoxButtons.OK, MessageBoxIcon.Information) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim b As New Button b.Text = "click me" b.Location = New Point(10, 10) Me.Controls.Add(b) AddHandler b.Click, AddressOf Button1_Click End Sub Wow dude! that's exactly what i'm looking for!!! you're the best! 5/5 star rating!!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.