13 Replies - 1211 Views - Last Post: 25 July 2012 - 12:48 PM Rate Topic: -----

#1 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Problem getting values after adding controls with variable names

Posted 24 July 2012 - 08:20 AM

Hello,

I'm having a problem in vb.net:

1. Adding a button and a textbox with a variable name and tag [I've got that].
2. By clicking on the button --> the tag value of the button (just created with variable tag) will be displayed in the textbox (which also has just been created)(everything with variable name and tags)[Problem].

How can I get the tag value of the button (which is clicked) displayed in the textbox?


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

        Dim DeleteBT As New Button
        Dim NumberTB As New TextBox


        With DeleteBT
            .Name = "Number" & My.Settings.Number
            .Tag = My.Settings.Number
        End With

        With DeleteBT
            .Name = "NumberDelete" & My.Settings.Number
            AddHandler .Click, AddressOf Number
        End With


        Me.Controls.Add(DeleteBT)
        Me.Controls.Add(NumberTB)

    End Sub


Private Sub Number()

    End Sub


If something is not clear, please ask (I'm not native English).

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem getting values after adding controls with variable names

#2 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 444
  • View blog
  • Posts: 3,075
  • Joined: 12-January 10

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 08:41 AM

not really sure what you are askign but to share infor between subs you will have to make a class variable and pass info to that which can be transfered to the other sub

class somehting
dim btn_name as string


sub button click(your above sub)


btn_name= the buttons name

sub number()

yourtextbox.text = btn_name



something liek that should work
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 09:39 AM

Private Sub Number()

End Sub



Should be:


Private Sub Number(sender As Object, e As EventArgs)
   'now we can eval the sender object which 
   'is the button wrapped up in this object
  Dim tag As String = DirectCast(sender, Button).Tag.ToString
   'or whatever property you need to use
End Sub

Was This Post Helpful? 1
  • +
  • -

#4 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 11:47 AM

Two other possible ways to get the button and textbox:

1. You add them to Me.Controls. So you can find the appropriate textbox by name in that collection.

2. Create your own user control. It would contain one button and one textbox. Now the botton_click event in your custom control knows which textbox to fill (the only one on the control!). Then you can do the following
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myControl As New MyCustomControl

        With myControl
            .Name = "MyControl" & My.Settings.Number
            .SettingsNumber = My.Settings.Number
        End With

        Me.Controls.Add(myControl)

    End Sub

Was This Post Helpful? 0
  • +
  • -

#5 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 12:00 PM

View Post_HAWK_, on 24 July 2012 - 09:39 AM, said:

Private Sub Number()

End Sub



Should be:


Private Sub Number(sender As Object, e As EventArgs)
   'now we can eval the sender object which 
   'is the button wrapped up in this object
  Dim tag As String = DirectCast(sender, Button).Tag.ToString
   'or whatever property you need to use
End Sub


Thank you very much! It works with non-variable controls, but it doesn't work with variable controls.

Public Class Form1

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

        Dim DeleteBT As New Button
        Dim NumberTB As New TextBox


        With NumberTB
            .Name = "Number" & My.Settings.Number
            .Tag = My.Settings.Number
        End With

        With DeleteBT
            .Name = "NumberDelete" & My.Settings.Number
            .Location = New System.Drawing.Point(50, 50)
            .Tag = My.Settings.Number
            AddHandler .Click, AddressOf Number
        End With


        Me.Controls.Add(DeleteBT)
        Me.Controls.Add(NumberTB)

    End Sub
    Private Sub Number(ByVal sender As Object, ByVal e As EventArgs)
        'now we can eval the sender object which
        'is the button wrapped up in this object
        Dim tag As String = DirectCast(sender, Button).Tag.ToString
        'or whatever property you need to use

        Dim tbnum As String
        tbnum = "Number" & tag
        tbnum.Text = tag
'

    End Sub
End Class


I'm getting an error: 'Text' is not a member of 'String'.

I have no idea how to fix that :sadlike:
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 01:33 PM

What do you mean it does not work for variable controls, it sure does! I can't see your entire code section on my phone to help with your other problem.
Was This Post Helpful? 0
  • +
  • -

#7 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 01:37 PM

    Private Sub Number(ByVal sender As Object, ByVal e As EventArgs)
        'now we can eval the sender object which
        'is the button wrapped up in this object
        Dim tag As String = DirectCast(sender, Button).Tag.ToString
        'or whatever property you need to use

        Dim tbnum As String
        tbnum = "Number" & tag
        tbnum.Text = tag

    End Sub


The second part (not visible if you can't scroll)...
Was This Post Helpful? 0
  • +
  • -

#8 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 01:43 PM

View Post_HAWK_, on 24 July 2012 - 01:33 PM, said:

What do you mean it does not work for variable controls, it sure does! I can't see your entire code section on my phone to help with your other problem.


I mean: I've got the tag value so I know the name of the textbox (which shows the value). The name of the textbox is: "Number" & tag.

So tbnum.Text should be (for example .tag = 5) Number5.text = tag.

But I'm getting a error: 'Text' is not a member of 'String'...
Was This Post Helpful? 0
  • +
  • -

#9 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem getting values after adding controls with variable names

Posted 24 July 2012 - 01:46 PM

Try to debugging your work, set a breakpoint and hover the mouse above variables to see what they hold. And no the String class does not have a text member. Check your variables you answer is in there.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem getting values after adding controls with variable names

Posted 25 July 2012 - 11:27 AM

Lets simplify:


Private Sub Number(sender As Object, e As EventArgs)
  Dim btn As Button = DirectCast(sender, Button)
  'btn is the referenced Button - the one you created
  'btn.Tag is now the tag object you set earlier
End Sub

Was This Post Helpful? 0
  • +
  • -

#11 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Re: Problem getting values after adding controls with variable names

Posted 25 July 2012 - 11:37 AM

View Post_HAWK_, on 25 July 2012 - 11:27 AM, said:

Lets simplify:


Private Sub Number(sender As Object, e As EventArgs)
  Dim btn As Button = DirectCast(sender, Button)
  'btn is the referenced Button - the one you created
  'btn.Tag is now the tag object you set earlier
End Sub


Thanks, but that wasn't what I'm looking for. I need to display the tag in the new textbox (created the same time as the button) with the variable name.
Was This Post Helpful? 0
  • +
  • -

#12 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Problem getting values after adding controls with variable names

Posted 25 July 2012 - 12:12 PM

View PostRobin19, on 24 July 2012 - 01:47 PM, said:

Two other possible ways to get the button and textbox:

1. You add them to Me.Controls. So you can find the appropriate textbox by name in that collection.

2. Create your own user control. It would contain one button and one textbox. Now the botton_click event in your custom control knows which textbox to fill (the only one on the control!). Then you can do the following
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myControl As New MyCustomControl

        With myControl
            .Name = "MyControl" & My.Settings.Number
            .SettingsNumber = My.Settings.Number
        End With

        Me.Controls.Add(myControl)

    End Sub

Since you know what the name of the textbox is, search Me.Controls for a control with that name. Warning, a C# coder is about to manually type VB code so some syntax will be wrong.
Dim textboxName As String
textboxName = "Number" & tag

Dim txtBox As TextBox
For Each control As Control In Me.Controls)
{
   If (control.Name = textboxName) Then
   {
      txtBox = CType(control, TextBox)
      Exit For
   }
}

// txtBox is now the text box you are looking for
// txtBox will be Null if that name was not found


I still think making your own User Control is the best and easiest solution.

This post has been edited by Robin19: 25 July 2012 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Problem getting values after adding controls with variable names

Posted 25 July 2012 - 12:39 PM

I am trying to teach a concept of knowing which control you clicked on and how to get it's properties. Good luck!

Did you notice where you used the With block - it looks like you may have wanted to use the textbox variable with the first With block instead of the button variable. Then if your looking fir the textbox with the tag that has that number you use a loop like Robin19 suggested where you check each control for it's tag property and compare.


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

        Dim DeleteBT As New Button
        Dim NumberTB As New TextBox


        With DeleteBT ' was this supposed to be the textbox?
            .Name = "Number" & My.Settings.Number
            .Tag = My.Settings.Number
        End With

        With DeleteBT
            .Name = "NumberDelete" & My.Settings.Number
            AddHandler .Click, AddressOf Number
        End With


        Me.Controls.Add(DeleteBT)
        Me.Controls.Add(NumberTB)

    End Sub

Was This Post Helpful? 0
  • +
  • -

#14 Whazabazaa  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 24-July 12

Re: Problem getting values after adding controls with variable names

Posted 25 July 2012 - 12:48 PM

View Post_HAWK_, on 25 July 2012 - 12:39 PM, said:

' was this supposed to be the textbox?



Damm, I had changed it in my project but not here...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1