Access textboxes dynamically created at runtime - vb.net

I add textboxes dynamically at runtime. How do I access them later in the program?

First name the dynamically created control
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyTextBox As New TextBox
MyTextBox.Name = "MyTextBox"
Me.Controls.Add(MyTextBox)
end sub
Somewhere else in the program:
'set text
Me.Controls("MyTextBox").Text = "Hi there"
'fetch text
Dim thetext = Me.Controls("MyTextBox").Text
'fetch textbox
Dim tb As TextBox = CType(Me.Controls("MyTextBox"), TextBox)
tb.Text = ""
tb.BackColor = Color.Red
Another way is to loop through the me.controlls-collection and find the control that way (maybe you have set .tag="mycontrol" on those or something else...

Related

How to add datetimepicker control in datagridview

I am doing my college project ,i have some problem in my project.
how to add datetimepicker control in datagridview vb.net,,i try so many option but its not working ..pls tell any solution.
coding for vb.net
Regards,
Prasanth.s
8098077937
Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
'//Adding DateTimePicker control into DataGridView
DataGridView1.Controls.Add(oDTP)
'// Setting the format (i.e. 02/07/2017 - mm/dd/yyyy)
oDTP.Format = DateTimePickerFormat.Short
'// It returns the retangular area that represents the Display area for a cell
Dim oRectangle = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, True)
'//Setting area for DateTimePicker Control
oDTP.Size = New Size(oRectangle.Width, oRectangle.Height)
'// Setting Location
oDTP.Location = New Point(oRectangle.X, oRectangle.Y)
'// Read value from DataGridView into DateTimePicker
oDTP.Value = DataGridView1.CurrentCell.Value
'// Now make it visible
oDTP.Visible = True
'// Force to change date value at oDTP_ValueChanged Event.
AddHandler oDTP.ValueChanged, AddressOf oDTP_ValueChanged
End Sub
Private Sub oDTP_ValueChanged(sender As Object, e As System.EventArgs)
DataGridView1.CurrentCell.Value = oDTP.Value
End Sub
Private Sub DataGridView1_CellLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
oDTP.Visible = False
End Sub

VB: Populate a textbox with popup from txt

I am creating a vb file, and I have a txt file with some that I want to populate a textbox.
What it is currently doing: It is choosing an option that i input in the textbox that I have created.
What I want it to do: Create a popup with every option from a textbox file, show it on screen, let me choose, and then populate another textbox with my choice.
Current code and screenshot:
Public Class Form1
Private Sub TextBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseClick
Dim f As New Form2
Try
f.Owner = Me
'
' Before showing the child form populate TextBoxes
'
f.TextBox1.Text = "1"
f.TextBox2.Text = "2"
f.TextBox3.Text = "3"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Box = (From T In f.Controls.OfType(Of TextBox)()
Where Not String.IsNullOrWhiteSpace(T.Text)
Select T Order By T.Name).FirstOrDefault
If Box IsNot Nothing Then
Me.TextBox1.Text = Box.Text
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Form 2
Public Class Form2
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim Box = (From T In Controls.OfType(Of TextBox)()
Where Not String.IsNullOrWhiteSpace(T.Text)
Select T Order By T.Name).FirstOrDefault
If Box IsNot Nothing Then
CType(Me.Owner, Form1).TextBox1.Text = Box.Text
End If
CType(Me.Owner, Form1).ActiveControl = CType(Me.Owner, Form1).cmdClose
Close()
End Sub
End Class
Image:
edit: now i only need to make the checkbox into locked textboxes, one line from the text file each.

Selecting items in a listbox with drawmode set to ownerdrawfixed

I have a listbox which I wanted some items to be a different color, and I understand to do this I have to set the drawmode to ownerdrawfixed. This works fine, but, now I can't retrieve a selected item. With the drawmode set to normal, when I click on an item in the listbox, I have it put that text into a textbox. With the drawmode set to ownerdrawfixed, when I click on an item, I get an error that "conversion from type 'item' to type 'string' is not valid. Also, the listbox is no longer sorted, even tho the sorted property is set to true (when in ownerdrawfixed mode).
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim ac As Integer
LstAll.DrawMode = DrawMode.OwnerDrawFixed
MaxRec = 708
ChkShow = True
FileOpen(1, "C:\MyMov3\MovData.mdt", OpenMode.Random, , , Len(Mv3Rec))
For x = 1 To MaxRec
FileGet(1, Mv3Rec, x + 1)
'This If loop for the colored text
If Mv3Rec.Rc3Mlti = True And ChkShwMlti.Checked = True Then
ac = Asc(Trim(Mv3Rec.Rc3MTitle))
If ac > 0 Then
Dim i As New Item()
i.ItmColor = Color.Red
i.Txt = Trim(Mv3Rec.Rc3MTitle)
LstAll.Items.Add(i)
End If
End If
If ChkShow = True Then
Dim i As New Item() 'Needed for the black text when in ownerdrawfixed mode
i.ItmColor = Color.Black 'Needed for the black text when in ownerdrawfixed mode
i.Txt = Trim(Mv3Rec.Rc3Title) 'Needed for the black text when in ownerdrawfixed mode
LstAll.Items.Add(i) 'Needed for the black text when in ownerdrawfixed mode
'LstAll.Items.Add(Trim(Mv3Rec.Rc3Title)) 'This line adds the text when in normal mode
End If
Next
FileClose(1)
End Sub
Private Sub LstAll_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles LstAll.DrawItem
If e.Index < 0 Then Return
Dim i As Item
i = TryCast(LstAll.Items(e.Index), Item)
If i IsNot Nothing Then
e.Graphics.DrawString(i.Txt, e.Font, New SolidBrush(i.ItmColor), e.Bounds)
End If
End Sub
Private Sub LstAll_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles LstAll.SelectedIndexChanged
TextBox1.Text = LstAll.SelectedItem
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
End
End Sub
End Class
Public Class Item
Public Txt As String
Public ItmColor As Color
End Class
This code is only the essential parts.... and was tested in a new project on a blank form. With a Listbox (renamed to LstAll) A textbox, a checkbox (renamed to ChkShwMlti). This gives the same error as in the program that I need it to work in. It does however use a file that I did not include the Structure for... but I think you can get the idea.
I have figured out this problem. In the LstAll.SelectedIndexChanged
Private Sub LstAll_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles LstAll.SelectedIndexChanged
Dim i As New Item
i = LstAll.SelectedItem
TextBox1.Text = i.Txt
End Sub
this worked as needed.

How to change selected item text in list box at run time?

I tried with code like this:
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles MyBase.Leave
' This way is not working
ListBox1.SelectedItem = TextBox1.Text
' This is not working too
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End Sub
The form is looked like this:
I need to change that list text while user typing in the text box. Is it possible to do that at run time?
You are using the form's leave event MyBase.Leave, so when it fires, it is useless to you.
Try using the TextChanged event of the TextBox instead.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) _
Handles TextBox1.TextChanged
Make sure to check if an item is actually selected in the ListBox:
If ListBox1.SelectedIndex > -1 Then
ListBox1.Items(ListBox1.SelectedIndex) = TextBox1.Text
End If
Use Double click to select line (item) inside list box and change or modify.
Instead of using text box use ListBox1_MouseDoubleClick event
Private Sub ListBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDoubleClick
then add this code inside this event
Dim intIndex As Integer = ListBox1.Items.IndexOf(ListBox1.SelectedItem)
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items.Remove(ListBox1.SelectedItem)
ListBox1.Items.Insert(intIndex, objInputBox)
End If
OR
Dim objInputBox As Object = InputBox("Change Item :","Edit", ListBox1.SelectedItem)
If Not objInputBox = Nothing Then
ListBox1.Items(ListBox1.SelectedIndex) = objInputBox
End If

How to programmatically add controls to a form in VB.NET

I am working on an inventory in Visual Basic 2010 Express Edition. I don't know the number of fields that will be necessary for the inventory. My hope was that I could add textboxes/checkboxes/buttons using for loops in the program. Is there a way to add controls to a form without using the toolbox?
Can I add controls by instantiating them in the program?
Yes.
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyTextbox as New Textbox
With MyTextbox
.Size = New Size(100,20)
.Location = New Point(20,20)
End With
AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_Changed
Me.Controls.Add(MyTextbox)
'Without a help environment for an intelli sense substitution
'the address name and the methods name
'cannot be wrote in exchange for each other.
'Until an equality operation is prior for an exchange i have to work
'on an as is base substituted.
End Sub
Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs)
'Write code here.
End Sub
Dim numberOfButtons As Integer
Dim buttons() as Button
Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Redim buttons(numberOfbuttons)
for counter as integer = 0 to numberOfbuttons
With buttons(counter)
.Size = (10, 10)
.Visible = False
.Location = (55, 33 + counter*13)
.Text = "Button "+(counter+1).ToString ' or some name from an array you pass from main
'any other property
End With
'
next
End Sub
If you want to check which of the textboxes have information, or which radio button was clicked, you can iterate through a loop in an OK button.
If you want to be able to click individual array items and have them respond to events, add in the Form_load loop the following:
AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked
then create
Private Sub All_Buttons_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
'some code here, can check to see which checkbox was changed, which button was clicked, by number or text
End Sub
when you call: objectYouCall.numberOfButtons = initial_value_from_main_program
response_yes_or_no_or_other = objectYouCall.ShowDialog()
For radio buttons, textboxes, same story, different ending.
Public Class Form1
Private boxes(5) As TextBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim newbox As TextBox
For i As Integer = 1 To 5 'Create a new textbox and set its properties26.27.
newbox = New TextBox
newbox.Size = New Drawing.Size(100, 20)
newbox.Location = New Point(10, 10 + 25 * (i - 1))
newbox.Name = "TextBox" & i
newbox.Text = newbox.Name 'Connect it to a handler, save a reference to the array & add it to the form control.
AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
boxes(i) = newbox
Me.Controls.Add(newbox)
Next
End Sub
Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs)
'When you modify the contents of any textbox, the name of that textbox
'and its current contents will be displayed in the title bar
Dim box As TextBox = DirectCast(sender, TextBox)
Me.Text = box.Name & ": " & box.Text
End Sub
End Class
To add controls dynamically to the form, do the following code. Here we are creating textbox controls to add dynamically.
Public Class Form1
Private m_TextBoxes() As TextBox = {}
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' Get the index for the new control.
Dim i As Integer = m_TextBoxes.Length
' Make room.
ReDim Preserve m_TextBoxes(i)
' Create and initialize the control.
m_TextBoxes(i) = New TextBox
With m_TextBoxes(i)
.Name = "TextBox" & i.ToString()
If m_TextBoxes.Length < 2 Then
' Position the first one.
.SetBounds(8, 8, 100, 20)
Else
' Position subsequent controls.
.Left = m_TextBoxes(i - 1).Left
.Top = m_TextBoxes(i - 1).Top + m_TextBoxes(i - _
1).Height + 4
.Size = m_TextBoxes(i - 1).Size
End If
' Save the control's index in the Tag property.
' (Or you can get this from the Name.)
.Tag = i
End With
' Give the control an event handler.
AddHandler m_TextBoxes(i).TextChanged, AddressOf TextBox_TextChanged
' Add the control to the form.
Me.Controls.Add(m_TextBoxes(i))
End Sub
'When you enter text in one of the TextBoxes, the TextBox_TextChanged event
'handler displays the control's name and its current text.
Private Sub TextBox_TextChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
' Display the current text.
Dim txt As TextBox = DirectCast(sender, TextBox)
Debug.WriteLine(txt.Name & ": [" & txt.Text & "]")
End Sub
End Class
You can get your Button1 location and than increase the Y value every time you click on it.
Public Class Form1
Dim BtnCoordinate As Point
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As Button = New Button
BtnCoordinate.Y += Button1.Location.Y + 4
With btn
.Location = New Point(BtnCoordinate)
.Text = TextBox1.Text
.ForeColor = Color.Black
End With
Me.Controls.Add(btn)
Me.StartPosition = FormStartPosition.CenterScreen
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1Coordinate = Button1.Location
End Sub
End Class