passing textbox text to module from form [closed] - vb.net

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an application migrated from vb6 to vb.net.
I want to access the text of textbox1 in SearchFields of module.
I do not want to pass it as a parameter because I have so many controls to pass.
Please provide some example.
My code is frmNew page have Textbox1
Private Sub Ok_Click(.....)
call SearchFields()
Me.Close()
End Sub
Inside module I have method below
Sub SearchFields()
Dim str As string
Dim frm As New frmNew
str = frm.Textbox1.Text
End Sub
frmNew.Textbox1.Text is getting nothing

To do as little recoding as possible, you can change your two methods to the following. For this to work Textbox1 must be marked public. I have personally had to "migrate" VB6 apps to .Net, as #LarsTech pointed out, this requires rethinking of your app, not just syntax changes.
Private Sub Ok_Click(.....)
call SearchFields(Me)
Me.Close()
End Sub
Sub SearchFields(Form frm)
Dim str As string
str = frm.Textbox1.Text
End Sub

You can pass a reference to the control to use for the search text. This makes your SearchFields method more general. As an example, I created a form named frmNew and a module named Searching. On the form I placed a button named Ok, a TextBox and a ComboBox.
Public Class frmNew
Private Sub Ok_Click(sender As Object, e As EventArgs) Handles Ok.Click
Searching.SearchFields(TextBox1)
Searching.SearchFields(ComboBox1)
Me.Close()
End Sub
End Class
There are two ways you could go about handling the control passed to the module (which I named Searching). First, you can check the type of the control and take actions based on that:
Module Searching
Sub SearchFields(textSource As Control)
Dim str As String = ""
' just for invesigating, show the type of the control.
Console.WriteLine(TypeName(textSource))
If TypeOf textSource Is System.Windows.Forms.TextBox Then
str = textSource.Text
ElseIf TypeOf textSource Is System.Windows.Forms.ComboBox Then
Dim src = DirectCast(textSource, ComboBox)
If src.SelectedIndex >= 0 Then
str = src.SelectedItem.ToString()
Else
' nothing was selected. Do whatever is appropriate.
str = "NOTHING SELECTED!"
End If
End If
'TODO: the searching code.
Console.WriteLine(str)
End Sub
End Module
Alternatively, you can take advantage of method overloading, where it runs the version of the method which corresponds to the argument(s) you pass to it:
Module Searching
Sub SearchFields(src As TextBox)
DoSearch(src.Text)
End Sub
Sub SearchFields(src As ComboBox)
'TODO: check an item is selected.
Dim txt = src.SelectedItem.ToString()
DoSearch(txt)
End Sub
Private Sub DoSearch(s As String)
' do the search
Console.WriteLine(s)
End Sub
End Module

Related

How to clear every TextBox inside a GroupBox [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am having problem with emptying every TextBoxes inside a GroupBox, because my loop only clears all TextBoxes if textbox1 has value but if I try to bypass textbox1 and jump to input data to textbox2, my ClearCtrlText method doesn't work.
Please see my loop code if there's a need for change:
Public Sub ClearCtrlText(ByVal root As Control)
For Each ctrl As Control In root.Controls
If TypeOf ctrl Is TextBox Then ' textbox set to empty string
If ctrl.Text <> "" Then
ctrl.Text = Nothing
End If
End If
Next
End Sub
I would be tempted to write this as an extension method:
Imports System.Runtime.CompilerServices
Public Module ControlExtensions
<Extension>
Public Sub ClearTextBoxes(source As Control)
For Each child As Control In source.Controls
Dim tb = TryCast(child, TextBox)
If tb Is Nothing Then
child.ClearTextBoxes()
Else
tb.Clear()
End If
Next
End Sub
End Module
You can then call it on a control as though it was a member, e.g.
GroupBox1.ClearTextBoxes()
This method also includes the recursion required to access child controls inside child containers, e.g. a Panel inside the GroupBox.
You need to RECURSE into containers within the form like this:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearCtrlText(Me)
End Sub
Public Sub ClearCtrlText(ByVal root As Control)
For Each ctrl As Control In root.Controls
If TypeOf ctrl Is TextBox Then ' textbox set to empty string
If ctrl.Text <> "" Then
ctrl.Text = Nothing
End If
ElseIf ctrl.HasChildren Then
ClearCtrlText(ctrl)
End If
Next
End Sub
End Class

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.

Most efficient way to refer to multiple controls. IE: the multi-WITH

I have had a little look around and cannot seem to find an easy way to refer to multiple controls in stuff like an IF statement, or to set a property to multiple controls etc. (IF exists = true!)
The 'shortcut'if you like; would best be described in illegal code such as:
Public Sub BreakCompiler()
if string.IsNullOrEmpty(Textbox1.text, textbox2.text, textbox3.text) Then .....
'As opposed to
If String.IsNullOrEmpty(PartNumTextBox.Text) Or _
String.IsNullOrEmpty(PartNameTextBox.Text) Or _
String.IsNullOrEmpty(PartGRNTextBox.Text) Or_
String.IsNullOrEmpty(SerialNumTextBox.Text) Then
'Warn user
Else
'do nofin.
End If
Or even more outlandish:
WITH Textbox1.text, textbox2.text, textbox3.text
.ReadOnly = true
END WITH
End sub
The idea is to prevent having to run 3 if statements, or whatever, that basicaly do the same thing to 3 different objects...etc.. etc. Similar to a handler for multiple events that can be separated by a ','.
Im aware of looping through controls (IE for each control in groupbox.controls for eg) but that wouldn't quite achieve what I'm after here. (Say you wanted to skip a couple?)
Just thought id check the collective wisdom.
As far as I know, there isn't anything similar to a handler for control properties in if statements.
You either have to do them separately (or add them all into an array), or, as you mentioned, go through a subset within another control.
However, one thing you can do (say you only wanted to change a certain type of control), is this:
For Each tb As TextBox In Me.Controls.OfType(Of TextBox)
'do stuff here
Next
Another option is to create a Sub that will make the changes for you, and then pass each into the Sub.
Private Sub changeTextBox(tBox as TextBox)
'make changes here
End Sub
You want to process a variable length collection of some type of object. One way to do this is to write helper methods that have an argument decorated with ParamArray keyword.
Public Shared Function AnyIsNullOrEmpty(ParamArray controls As Control()) As Boolean
Dim ret As Boolean
For i As Int32 = 0 to controls.GetUpperBound(0)
ret = String.IsNullOrEmpty(controls(i).Text)
If ret then Exit for
Next
Return ret
End Function
Public Shared Sub SetTBReadOnlyProperty(value As Boolean,ParamArray textboxes As TextBox())
For i As Int32 = 0 to textboxes.GetUpperBound(0)
textboxes(i).ReadOnly=value
Next
End Sub
Possible usage:
Private Sub DemoUsage
If AnyIsNullOrEmpty(TextBox1,TextBox3,TextBox4) then
' at least one is empty
Else
' all have value
End If
SetTBReadOnlyProperty(True,TextBox1,TextBox3,TextBox4)
End Sub
The newer versions of .NET allow you to use the .ForEach Linq extension on a List object. If you build a e.g. a List(Of TextBox) you can use .ForEach with an anonymous method to quickly iterate the controls in the List and manipulate properties and so forth. It's still a loop - but in a much more compact form. You can be selective about which controls are in a List, and have multiple Lists etc.
Here's an example:
Public Class Form1
Private _BoxList As New List(Of TextBox)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_BoxList.ForEach(Sub(tb As TextBox) tb.Enabled = Not (tb.Text = String.Empty))
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
_BoxList.Add(Me.TextBox1)
_BoxList.Add(Me.TextBox2)
_BoxList.Add(Me.TextBox3)
End Sub
End Class
That's just 3 TextBoxs and a Button on a vanilla form:
For the particular example I was working on when I thought "There must be a better way" Something like this works perfectly.
It might be easier if trying to do multiple non type specific operations on different things. (Thanks #Sastreen and #the_lotus)
Private Sub Validate_PartDetails() Handles PartNumTextBox.TextChanged, PartNameTextBox.TextChanged, PartGRNTextBox.TextChanged, SerialNumTextBox.TextChanged
For Each tb As TextBox In BasicDetailsCustomGroupBox.Controls.OfType(Of TextBox)
If tb.Tag = "notnull" Then
If String.IsNullOrEmpty(tb.Text) Then
tb.BackColor = Color.MistyRose
Else
tb.BackColor = Control.DefaultBackColor
End If
End If
If tb.Tag = "notnumeric" Then
If not(isnumeric(tb.Text)) Then
tb.BackColor = Color.MistyRose
Else
tb.BackColor = Control.DefaultBackColor
End If
End If
Next
End Sub
Thanks guys.

VB.Net forms.show() issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
first i work on visual studio 2008.
i have a form(frmOrdreEnCours) that call an another form(frmListe) on a button.click event .
frmListe has a ListBox where you can choose a thing and validate it,
But i was ask to add a button frmOrdreEnCours that do the exact same thing, but it had to choose a specific element and don't show the frmListe.
so i tried to simulate a click but it doesn't works without the .show() Method
and obviously my client don't want a to see a opened window even for a second.
Private Sub BtnVracSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnVracSup.Click
Dim ArlTraduction As ArrayList = New ArrayList
ArlTraduction.Add(30163) 'Non-conformite operationnel - materiel
ArlTraduction.Add(30168) 'Non-conformite opérationnel - autres
Classes.Langues.Traduire(ArlTraduction)
Dim FrmListe As New FrmListe(FrmListe.TypeListe.OperationnelMaterielBacSupp, ArlTraduction(0).ToString, ArlTraduction(1).ToString, NumOrdre)
FrmListe.MdiParent = FrmParent
Dim HideIt As Boolean = False
FrmListe.Show() // i don't want this
FrmListe.Visible = HideIt // i even try this
FrmListe.BtnValider.PerformClick() // there's no .Click available
End Sub
PerformClick doesn't do anything, if target control's CanSelect is false.
And if control is not visible, CanSelect return false.
http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Button.cs,845bec419ca23629
You need call event handler (like BtnValider_Click) directry, but I think that looks bad code.
If you implemented like below:
Public Class FrmListe
Private Sub BtnValider_Click(sender As Object, e As EventArgs) Handles BtnValider.Click
' Validation codes '
End Sub
End Class
It's time to extract validation codes to another method.
Public Class FrmListe
Private Sub BtnValider_Click(sender As Object, e As EventArgs) Handles BtnValider.Click
IsValid()
End Sub
Public Function IsValid() As Boolean
' Validation codes '
Return True ' or Return False it is not valid '
End Function
End Class
and you can call this like following:
FrmListe.IsValid()
If you can, I recommend that you consider implement IsValid method in another class, like following:
Public Class ListeValidator
Public Function IsValid(type As FrmListe.TypeListe, traduction1 As String, traduction2 As String, numOrdre As Object)
' I dont know type of NumOrdre '
' validation code here '
End Function
End Class
I hope this helps you.

Initialize not properly make with classes vb.net

So, I need to do an program for a client and he wants a search bar in it. So I made it and everything worked perfectly but I put it in my main form. Now, I want to put it in a class but when I initialize the program, it gives me the following error
An error occurred while creating the form. For more information,
see Exception.InnerException. The error is: The form is self-reference during
construction from a default instance, which led to infinite recursion. In the
constructor of the form, refer to the form using 'Me'.
I tried to put Me.Rbtn_X... but it doesn't recognize it.
Initialization
' Main form
Public Sub New()
InitializeComponent()
Initialize_search()
End Sub
Initialize_search()
' Main form
' search is initialize like this :
' Dim search as New Research
Private Sub Initialize_search()
search.generate_autocomplete()
End Sub
generate_autocomplete()
' Research class
Sub generate_autocomplete()
' Main_form = Main form
Dim field = ""
' This is the place where the program fail
If Main_form.RbtnR_avancee_contact.Checked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Is there something I didn't understand or It's not possible to do it that way?
Edit: added Form_shown event
Public Sub New()
InitializeComponent()
' Initialize_search()
End Sub
Private Sub Form_personne_Shown(sender As Object, e As EventArgs) Handles Me.Shown
MessageBox.Show("You are in the Form.Shown event.")
End Sub
The form is not created (fully) until New completes. By adding your Initialize_search to it, it eventually leads to the statement `Main_form.RbtnR_avancee_contact.Checked'. This is wrong on two counts:
1) the form doesnt exist yet, so you cant refer to it. (this is what the error meant with 'form is self-reference during construction')
2) the ref should be Me.RbtnR (which is what it meant by 'refer to the form using 'Me'')
Move your Initialize_search to the Form_shown event. Your code should look like this (including Lar's suggestion)
' Main form
Public Sub New()
' REQUIRED
InitializeComponent()
End Sub
If there is really something that needs to be setup for this, add it to the form_shown event:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Shown
' NOTE: even .NET refers to ME not MainForm etc
InitializePanel
InitializeSeach
End Sub
Then:
Private Sub Initialize_search()
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
End Sub
Then:
Sub generate_autocomplete(AdvContact as Boolean)
Dim field AS STRING = ""
If AdvContact Then
field = "personneressource"
Else
field = "beneficiaire"
End if
' ....
End Sub
Your search class doesn't have a reference to the instance of the form's controls.
Try passing the value instead:
Sub generate_autocomplete(advancedChecked As Boolean)
Dim field As String = ""
If advancedChecked Then
field = "personneressource"
Else
field = "beneficiaire"
End if
End Sub
Then when you call it:
search.generate_autocomplete(Me.RbtnR_avancee_contact.Checked)
Even if did work like you want it to, according to your code, it would always result in field containing the same value (whichever was set in designer).
Instead, try putting this code inside RbtnR_avancee_contact.Checked event. Or even TextChanged for the autocomplete box (and initialize it for the first time user enters anything), it would examine the checked state and populate autocomplete items.
With this approach, if your user never uses the search box, you don't need to initialize it.