How to disable all the TextBox in GroupBox in vb.net form - vb.net

I have a vb.net form in which i added a GroupBox including 10 TextBox. I want to disable 9 TextBox at the load time and a single TextBox should be enable.

Normally you would be required to show what you have tried. Next time please do that.
Create an array of the text boxes you want to disable. Then loop to the array setting each box's enabled property to False.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim boxes = {TextBox1, TextBox2}
For Each tb In boxes
tb.Enabled = False
Next
End Sub

Related

How to add a form2 control on top of a form1.control

I have two forms. Form1 with richtextbox and form2 with listbox.
I'd like to put listbox on top of richtextbox, docking it.
I want to do exactly like this:
form2.Controls.Remove(ListBox1)
form2.hide
form1.richtextbox1.controls.add(form2.listbox1)
form1.richtextbox1.hide 'I can't hide, also listbox will hide...
form2.listbox1.location= richtextbox1 or
form2.listbox1.bounds=richtextbox1.bounds
Everything works until setting form2.listbox1.location as If richtextbox1 is hidden also listbox will be. Also, setting location and/or bounds of listbox the same as richtextbox is not completely covering the richtextbox. I also tried using the same size for both.
Gif Example
This works for me:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim LB As ListBox = Form2.ListBox1
If IsNothing(LB.Tab)
LB.Tag = LB.Size ' <-- store it for later use
End If
Me.Controls.Add(LB)
LB.Bounds = RichTextBox1.Bounds
RichTextBox1.Hide()
End Sub
Later, when you move the ListBox back, retrieve the size stored in the Tag:
If Not IsNothing(LB.Tab)
LB.Size = LB.Tag
End If
Output:
Form2.Listbox1.Parent = Form1
Form1.RichTextBox1.Visible = false
Form2.Listbox1.dock = DockStyle.Fill
The cool thing here is any code written in Form2 for Listbox1 will still function while it's sitting in Form1.
P.S. Rename your controls.

Textbox all properties not working when textbox added in other control

I have added a windows form on control panel using following code
Dim frmopen As New WinForm1
panel1.Controls.Add(frmopen)
Form opens normally work also but my concern is,
I have one textbox on that WinForm1, when i am typing text in that textbox it takes input but when i am clicking in between that typed text the cursor goes to either last character or first character.
So if i have to enter characters in between, then first i have to erase the typed characters and then i have to type again.
Plz help me to work textbox normally??
Thank you
Try changing the child form's FormBorderStyle to None.
See: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim frm2 As New Form2
frm2.TopLevel = False
frm2.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.Panel1.Controls.Add(frm2)
frm2.Show()
End Sub
End Class

add event on controls - how can i refer to the control itself

I want to add the same event on my multiple textboxes. Let's say for example I want all my textboxes to trim the text value of itself when it has lost focus
my idea is to loop through all the textboxes and to add an event handler to all of it, but how will I refer to the textbox itself, I think it is the same as using the "this" keyword, but it is not available in vb.net - any other recommendations?
In order to get the element which triggered the event you can use the sender parameter of the event and cast it to the required type. It is not clear from the question which platform you are using, but below is the sample code for Windows Forms:
Private Sub txt1_TextChanged(sender As Object, e As EventArgs) Handles txt1.TextChanged
Dim currentTextbox as TextBox = CType(sender, TextBox)
' Do what you want with the textbox
End Sub
Similar principles should apply to Web forms or WPF as well.
Through all the textboxes Use handles for all textboxes
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) _
Handles TextBox1.LostFocus, TextBox2.LostFocus
Dim txtBox As TextBox = sender
txtBox.Text = Strings.Trim(txtBox.Text)
End Sub

combobox not being populated

I have a windows form project with a main form. There is a textbox leave event that opens a new form. In that new forms load event i have a combobox item loop that populates the combobox items. It works perfectly fine if run on the main form but doesnt work on the second form. Why doesn't the comboboxes on the secondary form populate when that form is opened via a textbox_leave event from the main form?
this is the leave event
Private Sub tbChartTitle_Leave(sender As Object, e As System.EventArgs) Handles tbChartTitle.Leave
If Not tbChartTitle.Text = Nothing Then
frmTitleAttributes.Show()
End If
End Sub
This is the code that populates one of the comboboxes on the second form (it works if run on a combobox on the main form)
Private Sub frmTitleAttributes_Load(sender As Object, e As System.EventArgs) Handles Me.Load
InitializeComponent()
AddFonts()
End Sub
Private Sub AddFonts()
' Get the installed fonts collection.
Dim allFonts As New Drawing.Text.InstalledFontCollection
' Get an array of the system's font familiies.
Dim fontFamilies() As FontFamily = allFonts.Families
' Display the font families.
For i As Integer = 0 To fontFamilies.Length - 1
cbxTitleFonts.Items.Add(fontFamilies(i).Name)
Next
End Sub
make sure that the Load handler is hit after you show your form (use break point)
also you can try to call it in the Shown event
Private Sub frmTitleAttributes_Shown(sender as Object, e as EventArgs) _
Handles frmTitleAttributes.Shown
AddFonts()
End Sub

How to Control Click Event Of A Textbox Array?

I am using VB 2010. I have 20 TextBox controls in my form. I turned them to TextBox array.
Here is the code:
Dim TbArray(19) As TextBox
Private Sub Form7_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
TbArray(0) = TextBox1
TbArray(1) = TextBox2
...
TbArray(19) = TextBox20
It works properly. I want my program to select all the text on the TextBox control which got focused.
How can i know which TextBox control has been selected? I mean there is no Private Sub TbArray(i)_GotFocus in the dropdown menu of vb designer.
To expound on what Akram said,
For x = 0 to 19
AddHandler tbarray(x).GotFocus, AddressOf TextBox_GotFocus
Next x
Private Sub TextBox_GotFocus(sender As Object, e As System.EventArgs)
Dim tb As TextBox = CType(sender, TextBox)
tb.SelectAll()
End Sub
Handle the TextBox.GotFocus event of all the TextBox controls using one event handler method. Use the following:
Dim focusedTextBox as TextBox = CType(sender, TextBox)
So you want the text inside the TextBox to get highlighted when it gets focus? Sounds like a job for JavaScript to me. Should be fairly simple using jQuery or something similar