Loop over controls in another class - vb.net

I have created one form with two datagridviews, two text boxes with some random text, and two combo boxes with some random text.
I have created two classes. DataGridViewForm and DataGridViewClass
I loop over the controls on the DataGridViewForm to clear the controls
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Pressing this button will clear all text and comboboxes
Dim button As New DataGridViewClass()
Dim a As Control = Nothing
For Each a In Me.Controls
button.Wissen = a
Next
End Sub
and it the DataGridViewClass
Property Wissen() As Control
Get
Return ControlName
End Get
Set(value As Control)
ControlName = value
If TypeOf ControlName Is TextBox Then
ControlName.Text = Nothing
ElseIf TypeOf ControlName Is ComboBox
ControlName.Text = String.Empty
End If
End Set
End Property
This works fine, but I would like to move the loop from the dgvFrom to the dgvClass. How would I do that? If I would move that loop I get the error "Controls is not a member of DataGridViewClass"
Okay, on the suggestion I tried this:
ControlName = value
Dim a As Control
For Each a In ControlName.Controls
If TypeOf a Is TextBox Then
a.Text = Nothing
ElseIf TypeOf a Is ComboBox
a.Text = String.Empty
End If
Next
And in DataGridViewForm I put
Dim button As New DataGridViewClass()
button.Wissen = Me
This works! Now I can use the same Clear button for other forms. Thanks!

The controls of DataGridViewForm are not static, you need to pass an instance of DataGridViewForm to an instance of DataGridViewClass and then DataGridViewClass will have access to all controls of DataGridViewForm, here is a basic example:
Public Class DataGridViewForm
Private DgvClassObj As DataGridViewClass
Sub New()
' pass this DataGridViewForm instance (Me) to an instance of DataGridViewClass
Init()
End Sub
Private Sub Init()
DgvClassObj = New DataGridViewClass(Me)
DgvClassObj.SomeMethodToLoopOverDataGridViewFormControls()
End Sub
End Class
Class DataGridViewClass
Private DgFormObj As DataGridViewForm
Sub New(ByRef dgform As DataGridViewForm)
Me.DgFormObj = dgform
End Sub
Public Sub SomeMethodToLoopOverDataGridViewFormControls()
For Each c As Control In DgFormObj.Controls
' now you can loop over the instance of your DataGridViewForm class
Next
End Sub
End Class

Related

How to pass value of a label from one form to another form in VB.NET?

I am new in Vb.net. I'm still studying the logics in this language. I want to output data in a label.text from form 1 to form 2 with the use of a button. How can I do that while both forms are running?
PS. label.text may change value every time I click the button.
Here are two options.
Use a property setter
Use a method
Note: The code below assumes the following:
Form1: Button (name: Button1)
Form2: Label (name: Label1)
When the button is clicked on Form1, if Form2 isn't open, it opens it. Additionally, the value of the label on Form2 is set.
Option 1: (use a property setter)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set property value
f2.Username = username
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Dim _username As String = String.Empty
Public Property Username As String
Get
Return _username
End Get
Set(value As String)
_username = value
Label1.Text = value
Label1.Refresh()
End Set
End Property
End Class
Option 2: (use a method)
Form1.vb
Public Class Form1
Dim counter As Integer = 0
Dim f2 As Form2 = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If f2 Is Nothing Then
'create new instance
f2 = New Form2
'show form
f2.Show()
Else
'show window
'if window is minimized, it will "unminimize" it
'f2.WindowState = FormWindowState.Normal
'bring window into focus
'also brings the window to front
'f2.Activate()
End If
'set value
Dim username As String = String.Format("user{0}", counter)
'set value using method
f2.SetLabelText(username)
counter += 1
End Sub
End Class
Form2.vb
Public Class Form2
Public Sub SetLabelText(ByVal username As String)
Label1.Text = username
Label1.Refresh()
End Sub
End Class
Resources:
How to: Create a Property (Visual Basic)

Auto initialize controls

I've created a custom control that need to be initialized. Actually I have that function to initialize my custom control (called "UserControl_Grille") :
Private Sub Init_Grille()
Me.grilleA.init_traduction(lignesTraduction)
Me.grilleB.init_traduction(lignesTraduction)
Me.grilleC.init_traduction(lignesTraduction)
Me.grilleD.init_traduction(lignesTraduction)
Me.grilleE.init_traduction(lignesTraduction)
Me.grilleF.init_traduction(lignesTraduction)
Me.grilleG.init_traduction(lignesTraduction)
Me.grilleH.init_traduction(lignesTraduction)
End Sub
As you can see it's not very worth it as If a add a new control I have to add it in this function.
So I tried to initialize automatically but it seems that it don't detect any custom control in my form ... :
Private Sub Init_Grille()
For Each grille As UserControl_Grille In Me.Controls.OfType(Of UserControl_Grille)()
grille.init_traduction(lignesTraduction)
Next
End Sub
In debug mode, it direct pass throught the For Each loop. There is any other solution?
You can recursively scroll through all controls.
For example, this sample code will return a list of all Labels in your form:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' This list will hold all the labels that we find
Dim results As List(Of Control) = New List(Of Control)
' Start searching for labels at the Form level
FindControls(Me, results)
' See how many labels we have found
MessageBox.Show(results.Count)
End Sub
Private Sub FindControls(parent As Control, ByRef results As List(Of Control))
For Each control As Control In parent.Controls
If TypeOf control Is Label Then
' We found a label so we add it to the results
results.Add(control)
End If
If Not control.Controls Is Nothing Then
' We loop through all sub-controls
FindControls(control, results)
End If
Next
End Sub
End Class
Hope this helps :)

Is it possible to have things defined and controlled inside a class, without being assigned in the "form" (outside the class) in VB?

My problem:
I have a checkbox I use to control if certain textboxes are enabled or not, and I need to do this around 30+ times. I've named my textboxes numerically/sequentially (TB_name_1, TB_name_2, etc) so if I know the Checkbox name I know which textboxes are affected.
My question:
Can I make a class for my checkboxes that says "if this box is checked/unchecked, then enable/disable these 3 textboxes" without the class also having to be told which textboxes (finds them itself)?
Here's the copy/paste code I'm currently using (not a class, obviously). I change the first 2 values and the rest of the code solves itself. (PS - I see you laughing)
Private Sub T1_cb_c_1_CheckedChanged(sender As Object, e As EventArgs) Handles T1_cb_c_1.CheckedChanged
'change here for current checkbox
Dim b As CheckBox = T1_cb_c_1
'change here for start value of first textbox (of 3), the next 2 will be in sequence
Dim a As Integer = 1
'How much of the below code can be moved to, and controlled from, a class?
Dim a1 As Integer = a + 1
Dim a2 As Integer = a + 2
Dim TB_PtNum As TextBox = Me.Controls.Find("T1_tb_c_" & a, True).FirstOrDefault
Dim TB_Qty As TextBox = Me.Controls.Find("T1_tb_c_" & a1, True).FirstOrDefault
Dim TB_Seq As TextBox = Me.Controls.Find("T1_tb_c_" & a2, True).FirstOrDefault
If b.Checked = True Then
TB_PtNum.Enabled = True
TB_Qty.Enabled = True
TB_Seq.Enabled = True
Else
TB_PtNum.Enabled = False
TB_Qty.Enabled = False
TB_Seq.Enabled = False
End If
End Sub
Here's a design time only class that will do this. You only have to the AssociatedCheckbox property in the designer:
Public Class TextBoxWithCheckboxProperty
Inherits TextBox
Private m_CheckBox As CheckBox
Public Property AssociatedCheckBox As CheckBox
Get
Return m_CheckBox
End Get
Set(value As CheckBox)
If Not m_CheckBox Is Nothing Then
RemoveHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
End If
m_CheckBox = value
If Not value Is Nothing Then
AddHandler m_CheckBox.CheckedChanged, AddressOf OnCheckBoxChanged
End If
OnCheckBoxChanged(m_CheckBox, Nothing)
End Set
End Property
Private Sub OnCheckBoxChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If Not sender Is Nothing Then
Me.Enabled = CType(sender, CheckBox).Checked
Else
Me.Enabled = False
End If
End Sub
End Class
Here's a sample Form1 that uses it:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.TextBoxWithCheckboxProperty1 = New WindowsApp4.TextBoxWithCheckboxProperty()
Me.SuspendLayout()
'
'CheckBox1
'
Me.CheckBox1.AutoSize = True
Me.CheckBox1.Location = New System.Drawing.Point(293, 131)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(81, 17)
Me.CheckBox1.TabIndex = 0
Me.CheckBox1.Text = "CheckBox1"
Me.CheckBox1.UseVisualStyleBackColor = True
'
'TextBoxWithCheckboxProperty1
'
Me.TextBoxWithCheckboxProperty1.AssociatedCheckBox = Me.CheckBox1
Me.TextBoxWithCheckboxProperty1.Location = New System.Drawing.Point(428, 131)
Me.TextBoxWithCheckboxProperty1.Name = "TextBoxWithCheckboxProperty1"
Me.TextBoxWithCheckboxProperty1.Size = New System.Drawing.Size(100, 20)
Me.TextBoxWithCheckboxProperty1.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.TextBoxWithCheckboxProperty1)
Me.Controls.Add(Me.CheckBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents CheckBox1 As CheckBox
Friend WithEvents TextBoxWithCheckboxProperty1 As TextBoxWithCheckboxProperty
End Class
I would use the property Tag for the related controls.
Suppose to set this property to the value "line1" for the first set of textboxes and on the checkbox that controls them.
Next row of controls (checkbox+textboxes) will have the property set to "line2" and so on until the last row. (You can do this through the Winforms Designer or through code)
At this point you could have a single event handler for all your checkboxes
Private Sub onCheckedChanged(sender As Object, e As EventArgs) _
Handles T1_cb_c_1.CheckedChanged, T2_cb_c_2.CheckedChanged, _
..... add other checkbox events here .......
' Get whatever checkbox has been clicked and extract its tag
Dim b As CheckBox = DirectCast(sender, CheckBox)
Dim tag = b.Tag.ToString()
' Find the textbox controls in this form with the same Tag
Dim ctrls = Me.Controls.OfType(Of TextBox).Where(Function(x) x.Tag.ToString() = tag)
' Enabled status matches the status of the Checked property
For Each c as TextBox in ctrls
c.Enabled = b.Checked
Next
End Sub

Cannot set focus to textbox

I am using VB and trying to select a portion of the text in a textbox of a separate form. However, I can't seem to find a good way to access the textbox from the other form, although the textbox is public (I am new to VB).
Currently, I'm trying to do this by calling a function located in the form (the form with the textbox), and then focusing on the textbox and selecting/highlighting the text. But it still doesn't work:
Public Sub GetFindLoc(ByVal lngStart As Long, ByVal intLen As Integer)
frmFind.Hide()
MessageBox.Show(ActiveForm.Name)
MessageBox.Show(txtNotes.CanFocus())
txtNotes.Focus()
txtNotes.Select(lngStart, intLen)
frmFind.Show()
End Sub
With this, I first hide the original form, and then try to select the text, and bring back the form. It shows that the active form is the one which I'm trying to select the text on, but it returns false on CanFocus().
Any help would be appreciated, thank you!
Hmm. This was more fiddly than I thought. You need to pass a reference to the other form:
Main form:
Public Class frmNotes
'This is the main form
'This form has a textbox named txtNotes and a button called btnShowFind
'txtNotes has .MultiLine=True
Private mfrmFind As frmFind
Private Sub btnShowFind_Click(sender As Object, e As EventArgs) Handles btnShowFind.Click
If mfrmFind Is Nothing OrElse mfrmFind.IsDisposed Then
mfrmFind = New frmFind(Me)
mfrmFind.Show()
Else
mfrmFind.BringToFront()
End If
End Sub
End Class
Finder form:
Public Class frmFind
'This form has a textbox called txtFind and a button called btnFind
Private mfrmParent As frmNotes
Sub New(parent As frmNotes)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
mfrmParent = parent
End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
If txtFind.Text = "" Then
MsgBox("Please enter text to find", MsgBoxStyle.Exclamation)
Exit Sub
End If
Dim intSearchBegin As Integer = mfrmParent.txtNotes.SelectionStart + 1
Dim intStart As Integer = mfrmParent.txtNotes.Text.IndexOf(txtFind.Text, intSearchBegin)
If intStart > -1 Then
mfrmParent.txtNotes.Select(intStart, txtFind.Text.Length)
mfrmParent.txtNotes.Focus()
mfrmParent.BringToFront()
Else
mfrmParent.txtNotes.Select(0, 0)
MsgBox("No more matches")
End If
End Sub
End Class
Public Class frmFind
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
Dim search As String = TextBox1.Text.Trim
Dim pos As Integer = frmNotes.txtNotes.Text.IndexOf(search)
If pos > 0 Then
frmNotes.txtNotes.Focus()
frmNotes.txtNotes.Select(pos, search.Length)
End If
End Sub
End Class
This is just a "find" form with 1 textbox and 1 button which will highlight the first occurrence of the string in TextBox1 that it finds in txtNotes on the other form. If you want it to find whitespace as well, then remove the Trim function. You can add code to find other occurrences or go forward/backward.

Calling external user control by its name

I have one panel (Panel1), two combo boxes (ComboBox1, ComboBox2) and one button (Button1) all in same form (Form1).
When the button is clicked:
Private Sub Button1_Click(sender As Object, e As EventArgs)
Dim a as String = ComboBox1.SelectedValue() & Combobox2.SelectedValue()
AddUserControl(a)
End Sub
value of a is the name of an external user control for instance p1k1.
Can I add an external user control named p1k1 to Panel1 in Form1 using following method?
Private Sub AddUserControl(ByVal a As String)
Panel1.Controls.Add(a)
End Sub
What should I do to make this work?
Usually I would use:
Panel1.Controls.Add(new p1k1)
You'll need to use reflection to do this. Something like this:
Private Sub AddUserControl(ByVal a As String)
Dim controlType As Type = Type.GetType(a)
If controlType Is Nothing Then
Throw New ArgumentException(String.Format("""{0}"" is not a valid type. Type names are case sensitive.", a))
ElseIf Not controlType.IsSubclassOf(GetType(Control)) Then
Throw New ArgumentException(String.Format("""{0}"" does not inherit from Control. Only Controls can be added to the control collection.", a))
End If
Dim newControl As Control = Activator.CreateInstance(controlType)
If newControl Is Nothing Then
Throw New ArgumentException(String.Format("Unspecified error when creating control of type ""{0}"".", a))
End If
Panel1.Controls.Add(newControl)
End Sub
i found my answer finally...
Private Sub AddUserControl(ByVal a As String)
Dim nmspace As String = "mynamespace"
Dim t As Type = Assembly.GetExecutingAssembly().GetType(nmspace & "." & a)
Dim o As Control = Activator.CreateInstance(t)
Panel1.Controls.Add(o)
End Sub