Dynamically assign text to text box in the loop - vb.net

I want to assign Text to the textBox in the look, I tried
Dim textBoxHB As TextBox = FindName("txt_HB_" + iRecCnt.ToString())
Me.Controls(String.Format("txt_HB_" + iRecCnt.ToString()).Text = .HouseBill
My Text box name change form txt_HB_1 ,txt_HB_2 and so on, and i want to where iRecCnt has 1,2.. values and Text is coming form .HouseBill
Is there any other way i can try?

Replace the loop with this:
Dim boxes = Me.Controls.OfType(Of TextBox).Where(Function(b) b.Name.StartsWith("txt_HB_"))
For Each box As TextBox in boxes
box.Text = .HouseBill
Next

Related

How do you display multiple combo box items in one text box?

I have a combo with multiple selections enabled. I've made a new form, and want to display the combo box selections in a text box. I use the following to display the actual data instead of the key: =[MyComboBox].[Column](2) but it only displays the data where there is exactly 1 item selected in the record. For records where there are multiple items selected in the combo box it displays nothing.
How can I display all my selections in the text box?
Use ListBox and create a function like:
Public Function GetSelectedItems(list As ListBox, Optional index as int = 0) As String
Dim result As String
For Each varItem In list.ItemsSelected
result = result & "," & combo.Column(index)
Next
GetSelectedItems = Mid(result, 2)
End Function
Assign the result of this function to text box:
Me.MyTextBox = GetSelectedItems(Me.MyComboBox, 2)

How to populate a textbox in vb.net

I have a gridview that contais a textbox in row 0.
I am able to read the textbox with this two instructions:
Dim control As Control = gridview.Rows(0).FindControl("textbox")
Dim valueInTextbox As TextBox = CType(cntrol, TextBox)
My question is, how can I send data to textbox, for example sent the letter "A" to the textbox?. Any suggestion?
I am using this line to try to populate the textbox but my textbox disappear:
gridview.Rows(0).Cells(0).Text = "A"
Thanks.
You can set the text using the following lines:
Dim control As Control = gridview.Rows(0).FindControl("textbox")
If control IsNot Nothing Then
Dim valueInTextbox As TextBox = CType(cntrol, TextBox)
valueInTextbox.Text = "Some Text"
End If

Access VBA - Hide Labels, text box, buttons If = " "

I'm trying to hide make some labels, text boxes and buttons hidden:
If rst![RI] = "" Or IsNull(rst![RI]) Then
I have:
A Label named "Label83"
A Text box named "C1"
A Text box named "Tex4"
A Text box named "Text8"
A button named "Command18"
So whenever I'm on PM200 and If rst![RI] = "" Or IsNull(rst![RI])
Then the listed label, text boxes and button should be hidden.
Thank you in advance.
You can use as a minimum:
Me!Label83.Visible = Len(Nz(rst![RI].Value))
or, to play nice:
Me!Label83.Visible = CBool(Len(Nz(rst![RI].Value)))
For more controls, set a variable:
Dim Visible As Boolean
Visible = CBool(Len(Nz(rst![RI].Value)))
Me!Label83.Visible = Visible
Me!text8.Visible = Visible
' etc.
And do rename your controls to something meaningful, like: lblCtransfer

Accessing Textbox programmatically created

I need to read a datagrid numbers of items, and programmatically add tabs to one tabControl. No problem on reading the datagrid, no problem in creating the model in the tabcontrol. So, I read the number of items, create the tabs accordingly, with all textboxes already with the correct values and so on.
At this point, the user will update some information on the tabs created, and need to click a Update button. At this point, I need to read all tabs, one by one, accessing all textboxes created, and send this to my database.
The only thing I got no result till now is “How to access these programmatically created Textboxes?
This is how I create the textboxes inside the TabControl
Dim TXT As New TextBox
TXT = New TextBox
TXT.Location = New System.Drawing.Point(213, 25)
TXT.Width = 303
TXT.TextAlign = HorizontalAlignment.Center
TXT.Name = "TXT_02_" & tab_counter
TXT.Text = MAT_DTCP(1) 'ABERTURA
TXT.BackColor = ColorTranslator.FromOle(RGB(128, 255, 255))
FORM_01.TBC_DTCP.SelectedTab.Controls.Add(TXT)
You could use LINQ:
Dim allTextBoxes = From tab In FORM_01.TBC_DTCP.TabPages.Cast(Of TabPage)()
From txt In tab.Controls.OfType(Of TextBox)()
Where txt.Name.StartsWith("TXT_02_")
Select txt
For Each txt As TextBox In allTextBoxes
' ... '
Next

String to Object as Expression

I have multiple TextBox controls Monday that will hold a value. I want to be able to add up all of the Monday textboxes and store that value as monTotal. I am getting an error message that says string cannot be converted to integer.
For i As Integer = 1 To rowCount Step 1
Dim var As Object
var = "txtMonday" & i & ".Text"
monTotal = monTotal + CInt(var)
Next
The way you are attempting to obtain a reference to the text boxes is not idiomatic of VisualBasic .NET.
var = "txtMonday" & i & ".Text" ' this is not a way to obtain a reference to the text box's text
While it would be possible to accomplish something like that using reflection, you'd be much better off refactoring your code to use an array of text boxes.
Since you are probably using Windows Forms you could perhaps implement logic to find the text box control you are interested in on the form using something like this:
' assuming container is the control that contains the text boxes
For Each ctrl In container.Controls
If (ctrl.GetType() Is GetType(TextBox)) Then
If ctrl.Name.StartsWith("txtMonday") Then
Dim txt As TextBox = CType(ctrl, TextBox)
monTotal = monTotal + CInt(txt.Text)
End If
End If
Next
The example above assumes that all the txtMonday.. text boxes are placed inside a control named container. That could be the form itself, or some other panel or table.
If all the textboxes live on the form and there are none being used for other text work you could use this. You could place all the textboxes that contain values your looking for in a separate container and get them like below, but use that control collection.
Dim amount As Double = 0
For Each tb As Textbox In Me.Controls.OfType(Of Textbox)()
amount += Convert.ToDouble(tb.Text)
Next
Dim monTotal as double=0
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox AndAlso ctrl.Name.StartsWith("txtMonday") Then
monTotal = monTotal + val(ctrl.Text)
End If
Next