How to populate a textbox in vb.net - 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

Related

Get textbox from Button Tag

I try to get a TextBox from a Button.Tag.
I have X buttons and for every Button has a own TextBox.
Not I want one Click-Method for all buttons and get the correct TextBox from the Button.Tag, but this does not work.
Can someone help me with this?
Dim Textbox As TextBox = CType(DirectCast(sender, Button).Tag, TextBox)
Textbox.Text = ""
With this code I get the following exception:
System.InvalidCastException: "Das Objekt des Typs "System.String" kann nicht in Typ "System.Windows.Forms.TextBox" umgewandelt werden."
Your code tries to convert a String (name of the TextBox) into a TextBox, which won't work.
So you either have to assign the TextBox'es by code like this:
Button1.Tag = TextBox1
Button2.Tag = TextBox2
...
or find the TextBox by its name like this:
Dim Textboxname As String = DirectCast(sender, Button).Tag.ToString()
Dim Textbox As TextBox = DirectCast(Me.Controls.Find(Textboxname, True).First(), TextBox)
Textbox.Text = ""

How to access cells from programmatically created datagridview in VB.net?

When creating a datagridview in VB.net I cannot get at the cells from anywhere except the Sub where it was made.
I tried placing the code in Form_Load and tried make the Sub Public.
I use Controls.Find, which finds the datagridview, but I cannot access any rows or cells. I can create textbox and label controls and access their text content from anywhere, but not so with the datagridview. This is odd to me.
Using and learning Visual Basic in Visual Studio 2017, for use in my own home business utility.
Dim dgv As New DataGridView
dgv.Location = New Point(2, 200)
dgv.Size = New Size(300, 50)
dgv.Name = "NewDGV"
Dim ColumnTitles() As String = {"ProdId", "Price"}
Dim ColumnWidths() As Integer = {50, 50)
For i = 0 To UBound(ColumnTitles)
Dim col As New DataGridViewTextBoxColumn
col.DataPropertyName = ColumnTitles(i)
col.HeaderText = ColumnTitles(i)
col.Name = ColumnTitles(i)
col.Width = ColumnWidths(i)
dgv.Columns.Add(col)
Next
Controls.Add(dgv)
‘the following sees the datagridview just fine
‘within the same sub
Dim x = dgv.Rows(0).Cells(0).Value
This is how I am creating this control. Perhaps I am Adding to Controls in the wrong way? I am open to better ways to do this.
Dim dgv As DataGridView = CType(Controls("NewDGV"), DataGridView)
dgv.Rows(0).Cells(0).Value = "I found you!"
What this does is find the first Control in the Controls collection, coerces it into a DataGridView type, and assigns it to the variable dgv. You can than manipulate dgv. In this case I put some text in the first cell on the first row.
You could ask the control collection for the instance of your datagridview
Dim MyDgv As DataGridView = Controls.Cast(Of DataGridView).First(Function(dgv) dgv.Name = "NewDGV")
Then work with "MyDGV" properties, methods, and/or events

How do I fill the first empty TextBox in a GroupBox?

I have a GroupBox with multiple TextBox controls and I can check to see if any are empty just fine but I'm trying to make my program fill the first empty textbox it finds in the GroupBox.
Code:
Dim empty = From txt In grpbill.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
If empty.Any Then
End If
Any ideas?
You're almost there. I'm unsure on how you define first but I've gone off the TabIndex property:
Dim firstEmptyTextBox As TextBox = (From txt In GroupBox1.Controls.OfType(Of TextBox)()
Where txt.Text.Length = 0
Order By txt.TabIndex Ascending).FirstOrDefault()
If firstEmptyTextBox IsNot Nothing Then
firstEmptyTextBox.Text = "Text"
End If
You can use the .FirstOrDefault() method:
Returns the first element of a sequence, or a default value if the sequence contains no elements.
In my example I have three TextBox controls. The first one has the text Not empty whilst the other two have nothing. When I run the code, this is my output:
Here another idea which will also work in cases where all TextBox controls in GroupBox are not empty.
Private Sub SetTextJForFirstEmptyTextBoxIfExists(text As String)
Dim emptyTextBoxes As IEnumerable(Of TextBox)
= grpbill.Controls.
OfType(Of TextBox)().
Where(Function(txtbox) txtbox.Text.Length = 0)
For Each emptyTextBox In emptyTextBoxes
emptyTextBox.Text = text
Exit Sub
Next
End Sub

Access a dynamically created textbox text from another sub. And I also want to be user-configurable and access the user-configured text

Textbox.text I want to access. And I want it user-configurable before I want to access the altered text.
Dim qbox As New TextBox
qbox.Size = New Size(20, 20)
qbox.Location = New Point(90, 10)
qbox.Parent = addtocart
qbox.Name = "quarts"
qbox.Text = "ss"**
how I dynamically add it inside a series of other dynamic controls:
tile.Controls.Add(addtocart)
flpp.Controls.Add(tile)
tile.Controls.Add(plabel)
tile.Controls.Add(nlabel)
addtocart.Controls.Add(qbox)
How I tried to access it:
qb.Text = CType(Me.Controls("flpp").Controls("tile").Controls("addtocart").Controls("qbox"), TextBox).Text
I generated to textbox at runtime. Of course it's dynamic. I'm new to VB and I'm just experimenting a school project. I wanted the textbox text to be configurable and then access that configured value. I've been brain-cracking for days about this. when I run the thing, I "getObject reference not set to an instance of an object." under NullReferenceException was unhandled" something like this. I don't get it.
WinForms? If yes, and you want to find that control "by name", then use the Controls.Find() function:
Dim ctlName As String = "quarts"
Dim matches() As Control = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim tb As TextBox = DirectCast(matches(0), TextBox)
' ... use "tb" somehow ...
Dim value As String = tb.Text
MessageBox.Show(value)
End If

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