Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,072 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,785 people online right now. Registration is fast and FREE... Join Now!




Call a dydnamicly created control

 
Reply to this topicStart new topic

Call a dydnamicly created control

RudyVB.net
27 May, 2008 - 05:47 AM
Post #1

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hello all!

I have a button that is created dynamicly, and based on that button click, a timer and a label with that button. So every button click, there is more than one button created. A timer and label is created for that button.

So when the label = 0, the timer stops. I also want to change the image of the button, when timer stops as well. The thing that is tricky,or at least for me. Each button has it own timer and stuff, so when the image changes, it changes the right button.

I'll supply some code, hopefully this makes sense. I know this can be done, I'm just not sure how to do it.

So this is where I'm creating button based on a boolean value on a sql DB.

CODE
Buttons = GetButtonData()
        For Each bi As ButtonInfo In Buttons
            If bi.Enabled Then
                'Dim prtLabel As New Label
                ' MsgBox(prtLabel.Text.ToString)
                Dim newbutton As New Button
                newbutton.Text = bi.VisText
                newbutton.TextAlign = ContentAlignment.TopCenter
                newbutton.Tag = bi
                newbutton.Width = 102
                newbutton.Height = 82
               newbutton.Top = 60 '+ (0 + bi.VisId)
                newbutton.Left = -95 + (100 * bi.VisId)
                'MsgBox(newbutton.Location.ToString)
                newbutton.Image = My.Resources.pvmetal2
                             If newbutton.Left >= 906 Then
                    newbutton.Top = 175 '+ '(0 + bi.VisId)
                    newbutton.Left = -1095 + (100 * bi.VisId)
                    ' MsgBox(newbutton.Location.ToString)
                End If
                If newbutton.Left >= 906 Then
                    newbutton.Top = 290 '+' (0 + bi.VisId)
                    newbutton.Left = -2095 + (100 * bi.VisId)
                End If
                If newbutton.Left >= 906 Then
                    newbutton.Top = 405 '+ '(0 + bi.VisId)
                    newbutton.Left = -3095 + (100 * bi.VisId)
                End If
               If newbutton.Left >= 906 Then
                    newbutton.Top = 420 '+ (0 + bi.VisId)
                    newbutton.Left = -4095 + (100 * bi.VisId)
                End If
                newbutton.Name = bi.VisId.ToString
               AddHandler newbutton.Click, AddressOf Buttons_Click
                Me.Controls.Add(newbutton)
            End If
        Next


This is the button click event for the button NEW BUTTON.

CODE
Dim btn As Button = sender


        If btn.Tag IsNot Nothing AndAlso TypeOf (btn.Tag) Is ButtonInfo Then

            'btn.Image = My.Resources.pvmetal2red

            Application.DoEvents()


            Try

                Dim frm As New frmInmates(btn.Tag)
                'This sends the name to the connect form
                frm.lblHoldVis.Text = btn.Text
                'frmAT.txtCommand.Text = btn.Tag.ToString

                MsgBox(btn.Name.ToString)
                frm.ShowDialog()

            Finally
'THIS WHERE MY TIMER AND MY LABEL FOR THAT TIMER GETS CREATED
                btn.Image = My.Resources.pvmetal2red
                Dim lbl As New Label
                lbl.Name = "lblTimer"
                lbl.Text = "30"
                lbl.Tag = lbl.Text.ToString
                Dim MinFont As New Font("arial", 12, FontStyle.Bold)
                lbl.Font = MinFont
                lbl.TextAlign = ContentAlignment.MiddleCenter
                lbl.AutoSize = True
                Me.Controls.Add(lbl)
                Dim tmr As New Timer
                tmr.Interval = 1000
                AddHandler tmr.Tick, AddressOf timers_tick
                tmr.Tag = lbl
                tmr.Start()
                Dim C As Control = CType(sender, Control)
                lbl.Location = New Point(C.Left + 30, C.Bottom)
                Dim lblInmV As New Label
                lblInmV.Name = "lblInmVF"
                lblInmV.Text = InmtrxVis
                lblInmV.Font = MinFont
                lblInmV.TextAlign = ContentAlignment.MiddleCenter
                lblInmV.AutoSize = True
                Me.Controls.Add(lblInmV)
                'lblInmV.Location = New Point(C.Left + 30, 45)
                lblInmV.Location = New Point(C.Left + 20, C.Top - 15)
            End Try


        End If


This is the tick event for the dymaiclu created timer

CODE
  Dim lbl As Label = CType(sender, Timer).Tag
        Dim i As Int32 = CInt(lbl.Tag)

        i -= 1

        lbl.Tag = i


        'If lbl.Text < 10 Then
        '    lbl.Text = "0" & i.ToString
        'Else
        lbl.Text = i.ToString

        'End If

        If i = 0 Then
            CType(sender, Timer).Stop()



            If lbl.Text = "0" Then
                
THIS IS WHERE I NEED TO ACCESS "'btn.Image = My.Resources.pvmetal2red"  But I'm not sure how to access btn, because it's only in the above sub.  I can't make it public, or it will only make 1 button, and not the many that needed to be created.


            End If


        End If


Thanks for your help on this!

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Call A Dydnamicly Created Control
27 May, 2008 - 08:46 AM
Post #2

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
OK.

I have never used Tag the way you seem to be using it, but lets see if I have this right.

You are basically pointing to the label associated to the Timer by using the Timer's Tag property.


It looks like you are using the Tag property of the Label to hold the current value of the counter, but you are also holding that value in string form in the Label's Text property.

Could you, instead, use the Label's Tag property in the same way you use the Timer's Tag property to point to the button associated with the Label and just decrement the Text property?


CODE

'existing code - I guess you are holding the original value here
lbl.Tag = lbl.Text.ToString

'instead do...
lbl.Tag = btn



This post has been edited by djkitt: 27 May, 2008 - 08:49 AM
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Call A Dydnamicly Created Control
27 May, 2008 - 09:16 AM
Post #3

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

QUOTE(djkitt @ 27 May, 2008 - 09:46 AM) *

OK.

I have never used Tag the way you seem to be using it, but lets see if I have this right.

You are basically pointing to the label associated to the Timer by using the Timer's Tag property.


It looks like you are using the Tag property of the Label to hold the current value of the counter, but you are also holding that value in string form in the Label's Text property.

Could you, instead, use the Label's Tag property in the same way you use the Timer's Tag property to point to the button associated with the Label and just decrement the Text property?


CODE

'existing code - I guess you are holding the original value here
lbl.Tag = lbl.Text.ToString

'instead do...
lbl.Tag = btn




Hi Kitt!

Thanks for the reply!

I already have the button that is holding the click. My problem is, I want to do this.

CODE
If lbl.Text = "0" Then
                
                btn.Image = My.Resources.pvmetal2

            End If


In the Tick event. But btn is not declared anymore, so I won't work. How can I get access to the btn?

Thanks!

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Call A Dydnamicly Created Control
27 May, 2008 - 01:25 PM
Post #4

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
QUOTE(RudyVB.net @ 27 May, 2008 - 12:16 PM) *


Hi Kitt!

Thanks for the reply!

I already have the button that is holding the click. My problem is, I want to do this.

CODE
If lbl.Text = "0" Then
                
                btn.Image = My.Resources.pvmetal2

            End If


In the Tick event. But btn is not declared anymore, so I won't work. How can I get access to the btn?

Thanks!

Rudy


Hey Rudy,

I think it is the same with your label. Isn't it declared in the Button Click event and then a pointer to the object passed up in the Timer's Tag property?

I might be wrong, but I think passing a pointer of the btn will keep it available for you, like so:

CODE

If lbl.Text = "0" Then
   Dim btn As Button = lbl.Tag
   btn.Image = My.Resources.pvmetal2
End If


I haven't actually typed everything in to try it but seems to me it should work. (maybe not, too.)

Sorry if I am sending you down a dead end road.
I think what I have done in the past when I wanted to create a lot of buttons dynamically was to hold them in an array. Its been a while since I wrote that code, but I could look around for it...

User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Call A Dydnamicly Created Control
27 May, 2008 - 01:32 PM
Post #5

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hey Kitt!

Let me work with it a little bit more. I don't think I need to keep in array, this seems to work well. I haven't worked with dyanamic objects much. All new to me.

I'll let you know how it goes!

Thanks!

Rudy smile.gif
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Call A Dydnamicly Created Control
30 May, 2008 - 12:48 PM
Post #6

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hi Kitt!

I have been working with your code. I think I have a pretty good understanding how you are doing things.
What I did on a test app, took your code on form1 and ran it. Then I coied the code from my program, to form 2. And applied the same logic to it.

This way I can really learn what the code is doing. The problem is on my side, I'm still getting an object error at btn.backcolor = mycolor, at the click event. I went through line by line, and I can't find waht diffrent between yours and mine.

Would you mind taking a peek, and see what I'm missing? Ignore all the comment lines. I'll keep digging on my side. I hope I find you before you do. smile.gif

Thanks!

rudy

Hey Kitt!

Got it!
newbutton.Text = "btn" + btnNum.ToString
this should be newbutton.NAME

Thanks again for all your help!



This post has been edited by RudyVB.net: 30 May, 2008 - 01:20 PM
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Call A Dydnamicly Created Control
30 May, 2008 - 01:24 PM
Post #7

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
Hey Rudy,

Try changing
CODE


       newbutton.Text = "btn" + btnNum.ToString


to
CODE

        newbutton.Name = "btn" + btnNum.ToString


and see how it works. icon_up.gif

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month