0

I need to set text of some controls.

I have a Form with some CheckBoxes an some TextBoxes.

In VBA (If I have 5 TextBoxes named "TextBox1", "TextBox2", ... "TextBox5") I can use something like this:

For n = 1 To 5
    Me("TextBox" & n).Text = NeededValue 
Next n

I know that something like this is also possible in VB.Net but I wasn't able to find the right syntax (and I didn't find similar codes on SO).

I've tryed using

Me.Controls() 

But I can't insert control name this way

5
  • What if someone decides to use better(more meaningful) control names? That would break your code silently. Don't let your logic depend on control-names. Commented Jun 7, 2016 at 8:52
  • @TimSchmelter I used "meaningful" control names but I need to show and get valued only some of them (I had no better ideas) suggests are apprecciated Commented Jun 7, 2016 at 8:59
  • 1
    You could group them in a container control like Panel or GroupBox. Then it's simple: Dim myTextboxes=textBoxPanel.Controls.OfType(Of TextBox).ToArray() Commented Jun 7, 2016 at 9:01
  • @TimSchmelter They are already into a panel. I have something like 4 rows. Each one with a ComboBox and five TextBoxes. I need to insert values from a DataTable depending on Datatable.Rows.Count (1 to 4) Commented Jun 7, 2016 at 9:05
  • sounds like a DataGridView would be much more appropriate and much easier if the data is in a DataTable. Commented Jun 7, 2016 at 19:06

2 Answers 2

4
Me.Controls.Find("TextBox" & n, True)

would be the similar approach to your VBA Style.

Sign up to request clarification or add additional context in comments.

2 Comments

I get error when i try to set Text. It's not possible to convert Control() to ComboBox (I used CType(.Find("CBox_Phase" & r, True), ComboBox)
The returnvalue is an array of controls. You have to take the first one and cast it as textbox. I would recommend you to think about your way of handling controls. I don't think it is a good way to iterate over controls by name in general. As I can see you come from VBA I can only suggest you to relearn VB.NET from scratch and forget the VBA things. And alternative way would be to store all Controls you want to change the text in an List(of Textbox) in your form so you can just do it with for each and the list.
0

Use For Each and then test with TypeOf to find all TextBoxes in your form like :

For Each myObject1 As [Object] In Me.Controls
     If TypeOf myObject1 Is TextBox Then
         TryCast(myObject1, TextBox).Text = "NeededValue"
     End If
Next

Also :

 Dim myText = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
        For Each btn In myText
            btn.Text = "NeededValue"
        Next

For i As Int32 = 1 To 5
    Dim Txt = Me.Controls.Find("TextBox" & i, True)
    If Txt.Length > 0 Then
        Txt(0).Text = "blah"
    End If
Next

Or :

  For i As Int32 = 1 To 5
       Dim Txt = Me.Controls.Find("TextBox" & i, True)
         If Txt.Length > 0 Then
            Txt(0).Text = "NeededValue"
         End If
   Next

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.