15

i have been created buttons and textboxs by coding in next loop, the result

'T(x).Name = "text_1"
'T(x).Name = "text_2"
'T(x).Name = "text_3"
'....

'B(x).Name = "button_1"
'B(x).Name = "button_2"
'B(x).Name = "button_3"
'...

and i want to get textbox property whene i click the button, i can get button property when click like button_1.Name.ToString but i cant get the text_1,2,3 .... property.

i do some trick by split function button_1.Name.ToString and get the last number and add it to the textbox name like "text_" & button_1.Name.ToString but i can't convert this string to object.

Update

Here's the code I'm using to load the controls in the loop:

C_A_TEXT(x) = New TextBox() 
C_A_TEXT(x).Dock = System.Windows.Forms.DockStyle.Fill 
C_A_TEXT(x).Location = New System.Drawing.Point(270, 5) 
C_A_TEXT(x).Margin = New System.Windows.Forms.Padding(0) 
C_A_TEXT(x).Size = New System.Drawing.Size(70, 27) 
C_A_TEXT(x).TabIndex = 5 
C_A_TEXT(x).Name = "NEW_RECHARGE_COUNT_TEXT_" & x

Update 2

Here's some more code:

AddHandler C_A_BUTTONS(x).Click, AddressOf C_A_BUTTON 

Private Sub C_A_BUTTON(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim thisButton As Button = sender Dim A = CType(Me.Controls("NEW_RECHARGE_COUNT_TEXT_1"), TextBox) 
    MsgBox(A.Text.ToString)  'Error!
End Sub
0

6 Answers 6

34

You can access the controls by name via the Form.Controls property, for instance:

Dim text1 As TextBox = CType(Me.Controls("text_1"), TextBox)
Sign up to request clarification or add additional context in comments.

13 Comments

i want to select controls by sting name ("NAME")
That is precisely what I demonstrated. In my example, the string name is "text_1".
i know but when i try MsgBox(text1.Name.ToString) result Error :(, i can't get any property .
You'll need to show how you are creating/loading the controls in the loop.
Do the controls actually show on the form? I don't see where you are ever adding them to the form?
|
10

As a quick useful tip to note, you don't seem to have to specify the type of control within the CType statement for purposes of accessing a control on your form. I came across this when trying to access multiple types of form controls, such as buttons and textboxes, all with the same line of code.

CType(Controls("NAME_OF_CONTROL"), Control)

Note that, rather than specifying exactly what type of control, such as 'TextBox' or 'Button', you simply state 'Control'. This allows you to universally change any type of control, without needing to specify its type.

I couldn't find this anywhere else, so I thought I'd share it!

Comments

9

Below is the code.

Dim oObj As Object = Me.Controls.Find("control name", True).FirstOrDefault()
Obj.Property = Value

I hope it helps.

1 Comment

This is the only solution that worked for me. Thank you @thewaywewere. In addition,one can then use CType to onvert the object to the desired control type. Dim oObj As Object = Me.Controls.Find("control name", True).FirstOrDefault() txtbox = CType(oobj,TextBox)
1
Dim sometext As TextBox = CType(Me.Controls("sometext "), TextBox)

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
0

The title of the thread and your description of the problem at hand seem a little different from each other.

To answer your title (to find a control by its name) use the following:

Dim myControlToFind = LayoutRoot.FindName("NAMEOFCONTROL")

More information on this method can be found here .

To answer the description of your issue as (to access a code generated control after it is clicked) do the following:

In the loop where you are creating the control(s) add the following handler

Addhandler YOURCONTROL.Clicked, AddressOf Textbox_Clicked

...and then this will handle the click event

Private Sub Textbox_Clicked(sender as object, e as RoutedEventArgs)

Dim tbClicked = Ctype(sender, TextBox)
'You can now access any of the properties of the textbox, for example

Dim txt as String = tbClicked.Text
Dim name as String = tbClicked.Name
Dim height as Double = tbClicked.Height

End Sub

3 Comments

Your answer assumes WPF. Since Hesham never specified, you may be correct in that assumption, but I rather doubt it.
Actually, given the comment he just added to my answer, I'd say it's pretty much certain that he is using WinForms.
thanks for your time but here Dim tbClicked = Ctype(sender, TextBox) i want replace sender to some string name like "TEXT_BOX_1" because i want click a button not a textbox .
0

None of the above worked for me. This does:

Dim selVal As String = CType(Form.FindControl(myListName), DropDownList).SelectedValue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.