6 Replies - 908 Views - Last Post: 08 January 2012 - 01:37 PM Rate Topic: -----

#1 rileyjstrickland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 11

Creating more than one label from a pre-made label

Posted 07 January 2012 - 11:07 PM

Ok, so I have a label that does certain events. I want to add another label with the click of a button (with the ability to add as many labels as needed.) I also want to make these labels have different text. So in a nutshell what I need help with is:

Creating a new label that does what the first label does (like on click)
Making the button create label after label on click, all of which have different text (like from a textbox)
Is This A Good Question/Topic? 0
  • +

Replies To: Creating more than one label from a pre-made label

#2 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 12:46 AM

You'd need to declare a label:
Dim lbl As Label


Then modify properties such as text, size etc...
Then add it to the forms control collection:
me.Controls.Add(lbl)

Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 12:57 AM

So what you need is creating control(label) at runtime and give them events, I have a simple snippet that shows how to create a control at runtime:
http://www.dreaminco...snippet6195.htm
You can create a method to delegete it as an event for all your controls by giving it the same definition as event subs have and then add handler that reference them
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 03:13 AM

And don't forget the correct event signatures!
Was This Post Helpful? 0
  • +
  • -

#5 rileyjstrickland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-October 11

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 08:47 AM

@smohd That snippet is pretty much what I already had for the code (except for:
            AddHandler .Click, Sub(sender As Object, e As EventArgs)
                                   'put code you need to be executed when button is clicked here
                                   MessageBox.Show("You clicked me, im going..")
                                   sender.dispose() ' this will cause clicked button to go away
                               End Sub
End With

Unfortunately neither snippets will work, because I need to add more than one of the same control (with the how ever much) showing. The amount of the control to be active will depend on the user, and each time that a button is pressed, a new label will be added, but each of these labels will have the same events as the other. Reason for I need them to have the exact events is because I'm making them be able to be dragged. (Also is there anyway for the code to be like
msgbox(clickedlabel.text)
so that when any label is clicked it does msgbox("the clicked label's text")
Was This Post Helpful? 0
  • +
  • -

#6 DimitriV  Icon User is offline

  • They don't think it be like it is, but it do
  • member icon

Reputation: 584
  • View blog
  • Posts: 2,738
  • Joined: 24-July 11

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 12:41 PM

You'd need an event like so:
Sub labelclick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label
lbl = CType(sender, Label)
MsgBox(lbl.Text)
End Sub



Then declare labels:
Dim lbll As Label
With lbll
lbll.Text = "foo"
AddHandler .Click, AddressOf labelclick
End With
Me.Controls.Add(lbll)


And now your label has a bound click event.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Creating more than one label from a pre-made label

Posted 08 January 2012 - 01:37 PM

Quote

Unfortunately neither snippets will work,

The snippet will work exactly according to your situation, simply put that sub and call it everytime you need to create a new label, you will need to change the event code by what you need it to do. It just simple, that snippet was a copy and paste then change things to what you want, you can copy the whole snippet and call it when you need, but you will need to ass other parameters for location, if you dont need sender.dispose() , just remove it.

@jimmyBo: The code you wrote is exactly the same as what he has, but he has it in simplified and merged mode.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1