11 Replies - 119868 Views - Last Post: 13 April 2010 - 08:31 AM Rate Topic: -----

#1 devilsangeleyes   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 24-February 06

Dynamically created buttons

Posted 19 December 2007 - 10:42 AM

Hello,

I've created dynamically buttons (create buttons at runtime). But now I have a problem, how can I use them in methods. I get the comment that they are not declared.

For examlpe:
Private Sub BtnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click

	   Dim txtInput As New TextBox
		txtInput.Top = 10
		txtInput.Left = 10
		txtInput.Width = 190
		txtInput.Height = 25
		txtInput.TextAlign = HorizontalAlignment.Right
		Me.Controls.Add(txtInput)
	   
		Dim btnSeven As New Button
		btnSeven.Text = "7"
		btnSeven.Top = 70
		btnSeven.Left = 10
		btnSeven.Width = 32
		btnSeven.Height = 25
		Me.Controls.Add(btnSeven)

End Sub


Here are the button and textbox created


Then I want to use them:
Private Sub BtnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSeven.Click
			txtInput.Text += btnSeven.Text
End Sub


Here says VS that they are both undeclared.

Does someone knows how I can solve this problem?

Already a thanks for those who want to help :)

Is This A Good Question/Topic? 0
  • +

#17 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5465
  • View blog
  • Posts: 14,418
  • Joined: 18-April 07

Re: Dynamically created buttons

Posted 19 December 2007 - 11:05 AM

I see you are on to the next step. This is called "Wiring up a control". Since you created the control dynamically, you also have to add its events dynamically using AddHandler. The example below uses a button to create another button on the form (pretty much like your last example) but then we wire it up to our own custom function. Here is what it looks like...

Public Class Form1

	' Our button event that kicks off the whole process.
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		' Create the button dynamically and set its properties.
		Dim mybutton As New Button
		mybutton.Left = 10
		mybutton.Top = 10
		mybutton.Text = "My New Button"

		' Use the addhandler and pass it first the event we want it to handle (here we use the click event)
		' Then we give it the address of our custom function called PrintMessage.
		' AddressOf is used to get the address of our function... think of it as pointing to the function.

		AddHandler mybutton.Click, AddressOf PrintMessage

		' Now we add the button to the form.
		Me.Controls.Add(mybutton)
	End Sub

	' Our custom function. Notice how it has the same parameters as a standard click event signature.
	' We must have these parameters setup like this so our custom function knows the sender and any argumeents passed.
	Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)
		MessageBox.Show("Dynamic event happened!")
	End Sub
End Class



Just read the in code comments and pay special attention to the AddHandler section where we first specify the type of event we are detecting and then giving it the address to our custom PrintMessage function.

After you do this, your dynamic button will respond to this event. And before you ask, yes, you could wire up multiple buttons to the same function. That is why you need to specify the sender as a parameter so the function can tell who is sending the event.

Enjoy!

"At DIC we be control wiring code ninjas! Think of us as the programming worlds energy company. We wire up everything!" :snap:
Was This Post Helpful? 2

#18 mattisdada   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Dynamically created buttons

Posted 22 September 2009 - 02:52 AM

View PostMartyr2, on 19 Dec, 2007 - 10:05 AM, said:

I see you are on to the next step. This is called "Wiring up a control". Since you created the control dynamically, you also have to add its events dynamically using AddHandler. The example below uses a button to create another button on the form (pretty much like your last example) but then we wire it up to our own custom function. Here is what it looks like...

CODEBLOCK

Just read the in code comments and pay special attention to the AddHandler section where we first specify the type of event we are detecting and then giving it the address to our custom PrintMessage function.

After you do this, your dynamic button will respond to this event. And before you ask, yes, you could wire up multiple buttons to the same function. That is why you need to specify the sender as a parameter so the function can tell who is sending the event.

Enjoy!

"At DIC we be control wiring code ninjas! Think of us as the programming worlds energy company. We wire up everything!" :snap:

This may seem like a dumb question, but, im adding toolstrip buttons, but for each button i would it to carry off a different value.

So when it creates the control i want button1 to have the value of 1, button2 to have the value of 2, etc.

I know it has something to do with the ByVal thing, but i dont know how that works properly
Was This Post Helpful? 0
  • +
  • -

#19 motcom   User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Dynamically created buttons

Posted 22 September 2009 - 03:07 AM

Hi there,

You can use the "sender" in the PrintMessage function to get a specific button,


Here is an example

(matyr2, i just modified a bit)...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mybutton As New Button
		mybutton.Left = 10
		mybutton.Top = 10
		mybutton.Text = "My New Button"

		' Use the addhandler and pass it first the event we want it to handle (here we use the click event)
		' Then we give it the address of our custom function called PrintMessage.
		' AddressOf is used to get the address of our function... think of it as pointing to the function.

		AddHandler mybutton.Click, AddressOf PrintMessage

		' Now we add the button to the form.
		Me.Controls.Add(mybutton)

	End Sub
	Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)

		If TypeOf sender Is Button Then
			MsgBox(CType(sender, Button).Text)
		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

#20 mattisdada   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Dynamically created buttons

Posted 22 September 2009 - 05:59 AM

Hmmm i don't quite understand this.

The sender is whatever invoked the event, correct?

I know its on the right track, but i still dont get how to use it.....

This is how i have it structured in my head how it works
pseudo vb net code:
if sender(x) then hello = hix||if sender(1) then hello = hi1
event button:
msgbox(hello)


But that doesnt really work.... Sorry im lost :P Im a beginner....

Stuff like this is much easier in Python.....

This post has been edited by mattisdada: 22 September 2009 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#21 motcom   User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Dynamically created buttons

Posted 22 September 2009 - 06:07 AM

sender in your case will be the button that called the event.

(You could wire it up to some other control else aswell)

Sender is set to object however and needs to be cast to type of button control.
(if it is one)

But it works. Have you tried it yet?
Was This Post Helpful? 0
  • +
  • -

#22 mattisdada   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Dynamically created buttons

Posted 22 September 2009 - 06:14 AM

Yeah.... it didn't work, i am using TOOLSTRIP buttons, will that make a difference?
Was This Post Helpful? 0
  • +
  • -

#23 motcom   User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Dynamically created buttons

Posted 22 September 2009 - 06:30 AM

Hi,

Here is the code for the toolstrip and its button.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim MyToolStripButton As New ToolStripButton

		MyToolStripButton.Text = "My New Button"
		'You would need to increment the name of the button here
		MyToolStripButton.Name = "NewButton1"

		'Most Probalby need to set an image
		MyToolStripButton.Image = Image.FromFile("C:\SomeImage")

		MyToolStripButton.DisplayStyle = ToolStripItemDisplayStyle.Image

		AddHandler MyToolStripButton.Click, AddressOf PrintMessage

		' Now we add the button to the toolstrip.
		Me.ToolStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {MyToolStripButton})
	  

	End Sub
	Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)

		If TypeOf sender Is ToolStripButton Then
			MsgBox(CType(sender, ToolStripButton).Text)
		End If
	End Sub


Was This Post Helpful? 0
  • +
  • -

#24 mattisdada   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 92
  • Joined: 30-May 08

Re: Dynamically created buttons

Posted 22 September 2009 - 06:39 AM

Ah works brilliantly now! But i still dont understand this code here:
   If TypeOf sender Is ToolStripButton Then
			MsgBox(CType(sender, ToolStripButton).Text)
		End If



EDIT: Oh, i figured it out, sweet! Thanks :)

This post has been edited by mattisdada: 22 September 2009 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#26 motcom   User is offline

  • D.I.C Lover
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: Dynamically created buttons

Posted 22 September 2009 - 06:46 AM

Hi,
Basically this part

If TypeOf sender Is ToolStripButton Then



is to check that sender is actually a ToolStripButton. (Remeber in the sub defenition sender is an object and not a ToolStripButton -> "ByVal sender As System.Object" )

The next part

CType(sender, ToolStripButton).Text



is to Convert "sender" to type "ToolStripButton". This is required otherwise we will not be able to access the ".Text" and other properties.

Hope this explains it a bit more.
Was This Post Helpful? 2

#27 Guest_Manishkumar*


Reputation:

Re: Dynamically created buttons

Posted 14 February 2010 - 10:57 PM

View Postmotcom, on 22 September 2009 - 02:07 AM, said:

Hi there,

You can use the "sender" in the PrintMessage function to get a specific button,


Here is an example

(matyr2, i just modified a bit)...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim mybutton As New Button
		mybutton.Left = 10
		mybutton.Top = 10
		mybutton.Text = "My New Button"

		' Use the addhandler and pass it first the event we want it to handle (here we use the click event)
		' Then we give it the address of our custom function called PrintMessage.
		' AddressOf is used to get the address of our function... think of it as pointing to the function.

		AddHandler mybutton.Click, AddressOf PrintMessage

		' Now we add the button to the form.
		Me.Controls.Add(mybutton)

	End Sub
	Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)

		If TypeOf sender Is Button Then
			MsgBox(CType(sender, Button).Text)
		End If
	End Sub




I had taken a palceholder and adding the button in a table of that place holder. Still after adding the handler, the function is not get called..
Was This Post Helpful? 0

#28 Guest_JeffKish*


Reputation:

Re: Dynamically created buttons

Posted 13 April 2010 - 08:31 AM

I have used your example and in theory it should work but the event does not fire when the dinamically create button is clicked.... any suggestions why this can be happening?
Was This Post Helpful? 0

Page 1 of 1