0

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

user3180163
  • 13
  • 1
  • 3
  • 1
    Please provide some code first. – Victor Zakharov Jan 14 '14 at 14:54
  • Have added the comment – user3180163 Jan 14 '14 at 18:09
  • 2
    `Dim frm As New frmNew` is not referencing the existing form. You will have to pass a reference as a parameter. Moving to .Net will require you to rethink the structure of your program. Modules, in general, should be replaces with classes. Avoid global variables for the most part. – LarsTech Jan 14 '14 at 19:07
  • Please, what are you need? To Open another form for input value or get value from text box of current form? – potehin143 Jan 14 '14 at 19:15
  • `frm.Textbox1.Text` should not be nothing. In fact I've no idea how it got Nothing, should have been String.Empty (`""`) at least. Regardless, what do you want to do? Please explain using 10 words or less. – Victor Zakharov Jan 14 '14 at 19:34

2 Answers2

1

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
AWinkle
  • 673
  • 8
  • 18
  • Thanks AWinkle... now I am getting the right values. But one thing I am facing SearchFields() method is called from several forms so what parameter should I pass from there. Otherwise I have to make a complete new method. – user3180163 Jan 15 '14 at 03:53
  • If the TextBox isn't always named Textbox1, I would recommend changing the parameter from Form to TextBox and directly pass the correct Textbox control. If it is always going to be named Textbox1, then you can always pass Me (Me is a reference to the current object) and the SearchFields method will always have the right instance. If you want maximum re-usability and sustainability, refactor the method to take a TextBox. – AWinkle Jan 15 '14 at 14:47
0

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
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84