VB.NET Referencing TextBox from one Form to another Form

0
    ''' <summary>
    ''' Clase VistaMenu
    ''' </summary>
    Public Class FrmGestionUsuarios

        Private _controllerUsuario As New ControllerUsuario.ControllerUsuario
        Private _modelUsuario As New ModelUsuario.ModelUsuario
        Private f As New VistaUsuarios.FrmAddUsuario
        Private msj_alert As String

        Public Sub New()
            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.
            If _controllerUsuario.RutaGetUsuario(msj_alert) IsNot Nothing AndAlso _controllerUsuario.RutaGetUsuario(msj_alert).Rows.Count > 0 Then
                DGVusuarios.DataSource = _controllerUsuario.RutaGetUsuario(msj_alert)
            Else
                MessageBox.Show(msj_alert, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        End Sub

        Private Sub FrmGestionUsuarios_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        End Sub

        Private Sub BtnAgregarUsuario_Click(sender As Object, e As EventArgs) Handles BtnAgregarUsuario.Click

            f.ShowDialog()



        End Sub

        Private Sub DGVusuarios_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVusuarios.CellDoubleClick



            End If
        End Sub

End Class
Public Class FrmAddUsuario

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Private Sub FrmAddUsuario_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

End Class

I try to reference a textbox that is in the second form in this way f.textbox1.text but apparently it is not possible to reference it that way

    
asked by Christian Gimenez 17.05.2016 в 03:32
source

1 answer

2

The path you are taking is incorrect, you should not reference any control directly from one form to another, but you should expose properties or public methods that return the data or perform some action on the form.

In the FrmAddUsuario, you could expose a property such as

Public Class FrmAddUsuario

    'resto codigo

    Public Property TextBox1Prop() As String
        Get
            Return TextBox1.Text
        End Get
        Set(ByVal value As String)
            TextBox1.Text = value
        End Set
    End Property

End Class

Then if from the FrmGestionUsuarios you will be able to access the value of the control by means of the property

Private Sub BtnAgregarUsuario_Click(sender As Object, e As EventArgs) Handles BtnAgregarUsuario.Click

    f.ShowDialog()

    Dim textbox1Usuario As String = f.TextBox1Prop

End Sub

In summary, you use the .net object-oriented capabilities and work with properties, methods and events encapsulating the functionality

    
answered by 17.05.2016 / 05:41
source