Using variable in for next loop code - vb.net

I have following code,
My.Settings.h301 = TextBox301.Text
My.Settings.h302 = TextBox302.Text
My.Settings.h303 = TextBox303.Text
My.Settings.h304 = TextBox304.Text
My.Settings.h305 = TextBox305.Text
I want to convert above code like following;
For i = 301 To 305 Step 1
My.Settings.h & i = TextBox & i.Text
Next
So, please provide me correct for next loop code. Thank you.

To pass a string as a control name, you can use: ParentControl.Controls("ControlName").
To pass a string as an application setting name, you can use: My.Settings("SettingName").
Hence, your code should look something like the following:
For i = 301 To 305 Step 1
My.Settings("h" & i.ToString) = Me.Controls("TextBox" & i.ToString).Text
Next
Please note that if the parent of your textboxes is not the form, you'll need to replace Me with the parent control name.
Hope that helps :)

If this is a WinForm project, all of the controls are included in the form's Controls collection, which is accessible by name, like this:
For x As Integer = 300 to 400
Me.Controls("My.Settings.h" & i.ToString()).height = Me.Controls("TextBox" & i.ToString()).Text
Next

Related

How do I display the result of a loop on a new line in a text box?

Basically, how do I write a new line in a text box, keeping the existing information as well.
If I have for loop,
For i As Integer = 1 To 10
Dim result = i
i = i + 1
textbox1.text = result
Next
This will display '10' in the textbox. I want it to be like:
1
2
3
4
...
First, your TextBox must allow multiple lines. This is a property of the textbox control that you can set from the designer or from the code. You may want to ensure that a scroll bar is there to scroll in case the height is not large enough.
If you want to set the properties from code, use this code in the Load event of the form.
' Set the Multiline property to true.
textBox1.Multiline = True
' Add vertical scroll bars to the TextBox control.
textBox1.ScrollBars = ScrollBars.Vertical
' Change the height of the textbox so that it could accomodate the lines
TextBox1.Height = 120
Now, your approach had a major problem in this line:
textbox1.text = result
The way you coded it, every new value of i, would overwrite the old value. What you want to do is to first construct a string, then send the entire string to the TextBox control. This is not required had you been using Console.WriteLine method.
Method 1
Dim s as string
s=""
For i As Integer = 1 To 10
s = s & Environment.Newline & i.ToString() 'we use Environment.NewLine to force new line
Next i
textbox1.text = s
Method 2
.NET offers a class to handle strings better than the way we did before. It won't matter in your case but it is the efficient way to handle concatenation when volume is large and/or performance matters
Dim s as new System.Text.StringBuilder() 'Initialize stringbuilder instance
For i As Integer = 1 To 10
s.AppendLine (i.ToString()) 'We use stringbuilder to concat. and inser line feed
Next i
textbox1.text = s.ToString()
Note: If you want double spacing then you need to add a linefeed (using & ) to both of the above methods.
Something like this should work:
For i As Integer = 1 To 10
if i = 1 then
textbox1.text = i
else
textbox1.text &= vbcrlf & i
end if
Next
For i = 1 To 10
textbox1.AppendText(vbNewLine & i)
Next

Visual Basic - DropDown text not altered to reflect value

I am working in VB and have an event that is supposed to update a number of values in a DropDown, and update the text accordingly:
For i As Integer = 0 To (prices.Items.Count - 1)
If prices.Items(i).Text.Contains("£") Then
Dim dConvertedValue = getTextAsDouble(prices.Items(i).Value) / dConversionRate
prices.Items(i).Value = dConvertedValue.ToString()
'should update displayable text here, but no change
prices.Items(i).Text = (Math.Floor(dConvertedValue).ToString("N") & "$")
End If
Next
This works fine in theory, and I have stepped-through and can see that the values are changing as expected. However, the Dropdown does not update at any point.
I'm very new to VB, so it could be something as simple as a syntax error. Does anybody know why this might be?
Mark
Try using this I have used your exact code but changed the loop to 'For each'
For each Item as ListItem in prices.items
If Item.Text.Contains("£") Then
Dim dConvertedValue = (getTextAsDouble(Item.Value) / dConversionRate)
Items.Value = dConvertedValue.ToString()
Items.Text = (Math.Floor(dConvertedValue).ToString("N") & " sq m")
End If
Next

Set parent Control to another control

I need to set a parent Control to another control using the VBA code.
Actually i am looping to create differents controls dynamically and i want now to link them by child-parent.
Do someone has an idea ?
Here is the function where i create a new control and i set some values. And the last assignment is where i want to set the parent
Public Function apply_change(ihm_f, oNode, iMyName$, project$)
Dim new_elem
Dim oSubNodes As IXMLDOMNode
If oNode.Attributes.getNamedItem("Type").Text <> "Page" Then
If (oNode.Attributes.getNamedItem("Type").Text = "RefEdit") Then
Set new_elem = ihm_f.Designer.Controls.Add("RefEdit.Ctrl", oNode.nodeName, True)
Else
Set new_elem = ihm_f.Designer.Controls.Add("Forms." & oNode.Attributes.getNamedItem("Type").Text & ".1", oNode.nodeName, True)
End If
With new_elem
On Error Resume Next
.Width = oNode.Attributes.getNamedItem("Width").Text
.Top = oNode.Attributes.getNamedItem("Top").Text
.Left = oNode.Attributes.getNamedItem("Left").Text
.Height = oNode.Attributes.getNamedItem("Height").Text
Set .Parent = get_parent(oNode.ParentNode.nodeName, oNode, ihm_f)
End With
If oNode.Attributes.getNamedItem("Type").Text = "MultiPage" Then
Call new_elem.Pages.Remove(0)
Call new_elem.Pages.Remove(0)
For Each oSubNodes In oNode.ChildNodes()
Call new_elem.Pages.Add(oSubNodes.BaseName, oSubNodes.Attributes.getNamedItem("Caption").Text, oSubNodes.Attributes.getNamedItem("Index").Text)
Next oSubNodes
End If
End If
Set apply_change = ihm_f
End Function
The getparent function return a Controle which can be anything .. like textbox or combo box etc..
You provide so little information in your question that it is difficult to guess your objective.
However, I am guessing that you want to record that Control Xxxxx is the parent of control Yyyyy where the definition of “parent” has nothing to do with Excel’s definition of parent. I am further guessing you do not know how to access controls by number.
The macro below lists the name, type and top position of every control on a form by its index number within the collection Controls. Any property of a control is accessible in this way. If control Xxxxx is the parent of control Yyyyy, you can scan the collection to find their index numbers when the form loads and record this information for use when required.
Private Sub UserForm_Initialize()
Dim InxCtrl As Long
Dim LenNameMax As Long
Dim LenTypeMax As Long
LenNameMax = 0
For InxCtrl = 0 To Controls.Count - 1
If LenNameMax < Len(Controls(InxCtrl).Name) Then
LenNameMax = Len(Controls(InxCtrl).Name)
End If
If LenTypeMax < Len(TypeName(Controls(InxCtrl))) Then
LenTypeMax = Len(TypeName(Controls(InxCtrl)))
End If
Next
Debug.Print PadR("Name", LenNameMax) & "|" & PadR("Type", LenTypeMax) & "| Top"
For InxCtrl = 0 To Controls.Count - 1
Debug.Print PadR(Controls(InxCtrl).Name, LenNameMax) & "|" & _
PadR(TypeName(Controls(InxCtrl)), LenTypeMax) & "|" & _
PadL(Format(Controls(InxCtrl).Top, "#,###.00"), 8)
Next
End Sub

VBA how to use a variable string to call a variable or function?

I have form inputs called "text1, text2, text2..." and I have an array of strings to fill the inputs with...
What I want is this....
text1.Value = Invoice.input(1)
text2.Value = Invoice.input(2)
text3.Value = Invoice.input(3)
but this seems like a lot of typing for something I know can be done with a simple for loop..
for loop = 1 To loop = 50
text+i.Value = Invoice.input(i)
next loop
how can I use text+i to call my var which is actually called text1?
When doing a Google search a few people said to use the Eval() function but this didn't work for me.
Thanks.
This should do it: (or at least help)
Sub ControlName()
Dim i As Long
For i = 1 To 50
UserForm1.Controls("text" & i).Value = Invoice.input(i)
Next
End Sub

Trouble with looping in VBA (access) to put items on a form and position them correctly

I've been trying to make a form with 68 items while positioning all the items on a form via a loop, but this loop isn't working :( Can someone please help me get this to work? I've tried looking everywhere but can't see what to do :/
Dim Items(67) As String
For x = 0 To 67
Items(x) = "Ctl" & x
Next
#It goes from 1 here as I manually set up 1 otherwise (x - 1) wont work.
For x = 1 To 67
Form_Home.Items(x).Top = 0
Form_Home.Items(x).Left = (Form_MainScreen.Items(x - 1).Left) + (Form_MainScreen.Items(x - 1).Width)
Form_Home.Items(x).Height = 225
Form_Home.Items(x).Width = 500
Next
Thank you everyone :)
What do you mean with Form_Home.Items? if you already have controls added to your forms, probably there is a Controls collection, so you can iterate trough it and set its properties, assuming each control has a name of the form Ctl0, Ctl1, Ctl2... then you can proceed as follow:
Dim ct_name as String
Dim ct_name_before as String
For x = 1 To 67
ct_name_before = "Ctl" & CStr(Cint(x-1))
ct_name = "Ctl" & CStr(x)
Form_Home.Controls(ct_name).Top = 0
Form_Home.Controls(ct_name).Left = (Form_MainScreen.Items(ct_name_before).Left) + _
(Form_MainScreen.Items(ct_name_before).Width)
Form_Home.Controls(ct_name).Height = 225
Next
Some version of VBA allow you to create array of controls sharing all the same name and having different index, then you can iterate through the array to get each control.