4 Replies - 2159 Views - Last Post: 30 September 2012 - 11:59 AM Rate Topic: -----

#1 dsreamincode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-September 12

Add diff. Dynamic Controls in same row but how setting AddHandler ?

Posted 29 September 2012 - 09:55 PM

Hello,
I using dynamic method to create button,textbox and picture contorls in Panel1(vb.net),
how could I click the one row of picture and can trigger the textbox event in the same row?

if i click paste(2) then it can judge I click (2) and affect txt(2) to show message

how 'show_to_txt' function write?

[img=http://www1.picturepush.com/photo/a/10406824/640/10406824.jpg]
Attached Image

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim i As Integer = 0

        For i = 1 To 3

            Dim txt As New TextBox
            txt.Name = "txt" & i.ToString
            txt.Location = New Point(10, i * 50 + 3)
            Panel1.Controls.Add(txt)

            Dim pic As New PictureBox
            pic.Name = "paste" & i.ToString
            pic.Image = Picture1.Image ' show image
            pic.Height = 15
            pic.Location = New Point(150, i * 50 + 3)
            Panel1.Controls.Add(pic)

            AddHandler pic.Click, AddressOf show_to_txt
        Next

    End Sub


    Private Sub show_to_txt(ByVal sender As Object, ByVal e As EventArgs)

        'MsgBox("you click: " & CType(sender, PictureBox).Name)

        'if i click paste(2) and it can judge I click (2) and affect txt(2) to show message
        'I wnat to implement this action:

        'txt1.text = "you click: 1"
        'txt2.text = "you click: 2"
        'txt3.text = "you click: 3"

    End Sub

End Class





How do I call the handler to implement this code?
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Add diff. Dynamic Controls in same row but how setting AddHandler ?

#2 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Add diff. Dynamic Controls in same row but how setting AddHandler ?

Posted 29 September 2012 - 10:27 PM

You can do it easily with For Each loop, where you compare sender.Name (as you already figured out by yourself), with each TextBox's Name in Controls collection of Panel1 (because there they are added on form load). I couldn't resist to do similar thing with Linq.

Spoiler

Was This Post Helpful? 1
  • +
  • -

#3 dsreamincode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 29-September 12

Re: Add diff. Dynamic Controls in same row but how setting AddHandler ?

Posted 30 September 2012 - 01:16 AM

Thanks....

But if I add more then one textbox?
How coult I get some one?


like:
For i = 1 To 3

            Dim txt As New TextBox
            txt.Name = "txt" & i.ToString
            txt.Location = New Point(10, i * 50 + 3)
            Panel1.Controls.Add(txt)

            Dim pic As New PictureBox
            pic.Name = "paste" & i.ToString
            pic.Image = Picture1.Image ' show image
            pic.Height = 15
            pic.Location = New Point(150, i * 50 + 3)
            Panel1.Controls.Add(pic)

            Dim txt2 As New TextBox
            txt2.Name = "txt2" & i.ToString
            txt2.Location = New Point(190, i * 50 + 3)
            Panel1.Controls.Add(txt2)

            AddHandler pic.Click, AddressOf show_to_txt
        Next



Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Add diff. Dynamic Controls in same row but how setting AddHandler ?

Posted 30 September 2012 - 06:40 AM

Have you thought about creating a usercontrol for this? You could use an image property and a text property so that at designtime, you specify the image for the picturebox and in the click event of the picturebox set the text that you want with a text property. This way, all your properties are combined in a single control without you having to loop through control collections every time you want to set something. What you're wanting is possible but you will have to ensure that you have a method that ensures you find the correct control. (For example, you could do something as simple as setting the tag property for each control to match) then when you loop through the collection, you find the tag of the textbox that matches the tag of the sender and set its text property that way.

Just thought a custom control would be a better fit for this.
There are a couple of Tutorials on the subject in our VB.Net Tutorials section.
Was This Post Helpful? 0
  • +
  • -

#5 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Add diff. Dynamic Controls in same row but how setting AddHandler ?

Posted 30 September 2012 - 11:59 AM

View Postdsreamincode, on 30 September 2012 - 10:16 AM, said:

But if I add more then one textbox?
How coult I get some one?


like:


The way you gave example, you need to realize first, that txt TextBoxes and txt2 TextBoxes, share same name ending on each iteration (loop) of i. Each loop you add 2 TextBoxes to the Panel1, each on different location, but with same last character in Name property value (all iterations give: txt1, txt21, txt2, txt22, txt3, txt23).

As you didn't say, and didn't put another clickable Picturebox, perhaps this is what you want:
    Private Sub show_to_txt(ByVal sender As Object, ByVal e As EventArgs)
        Dim pictureName As String = sender.Name.ToString

        Dim txtBoxes = From txtBx In Panel1.Controls
                       Where txtBx.GetType.ToString = "System.Windows.Forms.TextBox" And txtBx.Name.ToString.EndsWith(pictureName.Last)

        For Each txt In txtBoxes
            txt.Text = "you click" & txtBoxes.First.Name.ToString.Last
        Next
    End Sub


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1