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 = ""
Related
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
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
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
So let's say I have 5 radio buttons on my form.
In my code, I want to check RadioButton3.
I have "RadioButton3" stored in a string variable (RadName)
I can already loop through the controls on the form. How do I go about checking (and actually having the radio button filled in), RadioButton3 when the loop gets to it?
For Each RadioButtn As Control In Me.gbWriteXML.Controls
If (TypeOf RadioButtn Is RadioButton) Then
---code here for checking the radio button---
End If
Next
The .gbWriteXML is a groupbox. Just to avoid any confusion. I was thinking something like:
If RadioButtn.Name = RadName Then
RadName.Checked = True (or .PerformClick for that button)
End If
How can I actually get the control associated with the RadName string, via the control's name?
I need this code to be able to take a radiobutton's name as string hardcoded or entered into the program at runtime, loop until it find the radiobutton with a matching name, then actually taking that radiobutton control, and checking it so it's filled in blue.
Don't loop. Just search for it with Controls.Find() like this:
Dim RadName As String = "RadioButton3"
Dim matches() As Control = Me.Controls.Find(RadName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is RadioButton Then
Dim rb As RadioButton = DirectCast(matches(0), RadioButton)
rb.Checked = True
End If
I wanted to test if I could get this to work: I have two textboxes with IDs Textbox1 and Textbox2. I enter the name of a variable I have stored in my program in Textbox1 and then I enter any value in Textbox2. After clicking on a confirm button I want to have the value of the variable name that I've written in Textbox1 changed to the value I wrote in Textbox2.
Something like this (in pseudocode)
GetVariable(Textbox1.Text) = Textbox2.Text
Is there an easy way to get this done or will I be forced to create other type of functions to get around this kind of problem?
Yes, you can do this using reflection.
dim property = this.GetType().GetProperty(Textbox1.Text)
property.SetValue(this, Textbox2.Text)
This will not work on local variable, but it will work on properties.
Of course the better way would be to just use a Dictionary(of string, string) instead of loose variable. Then you can just write myValues(Textbox1.Text) = Textbox2.Text.
Another example of using Reflection. This will work on Fields or Properties. They can be Public or Private, and you don't have to exactly match the Case:
Try
Dim FI As System.Reflection.FieldInfo = Me.GetType.GetField(TextBox1.Text, Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
If Not IsNothing(FI) Then
FI.SetValue(Me, TextBox2.Text)
Else
Dim PI As System.Reflection.PropertyInfo = Me.GetType.GetProperty(TextBox1.Text, Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic)
If Not IsNothing(PI) Then
PI.SetValue(Me, TextBox2.Text)
Else
MessageBox.Show(TextBox1.Text, "Field or Property not found!")
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Unable to Set Value")
End Try
Or you can also create a label control at runtime with the value of TextBox1 Text and assign a value according to the Text of TextBox2. This label will be invisible:
Dim MyNewObject As Control
MyNewObject = New Label
MyNewObject.Name = Textbox1.Text
MyNewObject.Text = Textbox2.Text
MyNewObject.Visible = False
Me.Controls.Add(MyNewObject)
And you can use it as a variable in any part of the form. Example to show the value should be as follows:
MsgBox(Me.Controls(Textbox1.Text).Text)