VB.Net Pass a variable from one form to another

1

I need to pass a variable from one form to another modal form since the variable refers to the id of the selected row in a DataGridView. Here the code.

Public Class FrmMenu

    Private _modelUsuario As New Usuario.Usuario
    Private _logicUsuario As New ControllerUsuario.LogicUsuario
    ' esta variable _rowId es la que necesito pasar'
    Private _rowId As String
    Private _cellUser As String
    Private _msjAlert As String

    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Property DaGrViUsuariosDataSourceProp()
        Get
            Return DaGrViUsuarios.DataSource
        End Get
        Set(ByVal value)
            DaGrViUsuarios.DataSource = value
        End Set
    End Property

    Public Property BtnModificarUsuarioEnabledProp()
        Get
            Return BtnModificarUsuario.Enabled
        End Get
        Set(ByVal value)
            BtnModificarUsuario.Enabled = value
        End Set
    End Property

    Public Property BtnEliminarEnabledProp()
        Get
            Return BtnEliminar.Enabled
        End Get
        Set(ByVal value)
            BtnEliminar.Enabled = value
        End Set
    End Property

    Public Sub RefreshDaGrViUsuarios()
        DaGrViUsuariosDataSourceProp = _logicUsuario.RouteGetUsuario(_msjAlert)
        If _msjAlert = False Then
            MessageBox.Show("No hay Usuarios para mostrar.", "Informacion!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            BtnModificarUsuarioEnabledProp = False
            BtnEliminarEnabledProp = False
        Else
            BtnEliminarEnabledProp = True
            BtnModificarUsuarioEnabledProp = True
        End If
    End Sub

    Private Sub FrmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RefreshDaGrViUsuarios()
    End Sub

    Private Sub BtnAgregarUsuario_Click(sender As Object, e As EventArgs) Handles BtnAgregarUsuario.Click
        Dim _frmUsuario As New FrmUsuario(Me)
        _frmUsuario.BtnModificarFrmUsuarioEnabledProp = False
        _frmUsuario.ShowDialog()
    End Sub

    Private Sub DaGrViUsuarios_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DaGrViUsuarios.CellDoubleClick
        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
            Dim _frmUsuario As New FrmUsuario
            _frmUsuario.TextUsuarioTextProp = DaGrViUsuarios.CurrentRow.Cells(1).Value.ToString
            _frmUsuario.TextNombresTextProp = DaGrViUsuarios.CurrentRow.Cells(2).Value.ToString
            _frmUsuario.TextEmailTextProp = DaGrViUsuarios.CurrentRow.Cells(3).Value.ToString
            _frmUsuario.TextPasswdTextProp = DaGrViUsuarios.CurrentRow.Cells(4).Value.ToString
            _frmUsuario.CmbPerfilTextProp = DaGrViUsuarios.CurrentRow.Cells(5).Value.ToString
            _frmUsuario.ShowDialog()
        End If
    End Sub

    Private Sub BtnRefrescar_Click(sender As Object, e As EventArgs) Handles BtnRefrescar.Click
        RefreshDaGrViUsuarios()
    End Sub

    Private Sub DaGrViUsuarios_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DaGrViUsuarios.CellClick
        If e.RowIndex >= 0 AndAlso e.ColumnIndex >= 0 Then
            _rowId = DaGrViUsuarios.CurrentRow.Cells(0).Value.ToString
            _cellUser = DaGrViUsuarios.CurrentRow.Cells(1).Value.ToString
        End If
    End Sub

    Private Sub BtnEliminar_Click(sender As Object, e As EventArgs) Handles BtnEliminar.Click
        If _rowId = Nothing Then
            MessageBox.Show("Seleccione el Usuario que desea Eliminar", "Advertencia!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            Dim confirm As Integer = MessageBox.Show("Esta Seguro de Eliminar el Usuario:" + " " + _cellUser, "Advertencia!", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If confirm = DialogResult.Yes Then
                _logicUsuario.RouteDeleteUsuario(_rowId, _msjAlert)
                If _msjAlert = True Then
                    MessageBox.Show("Usuario Eliminado Exitosamente!", "Exito!", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    RefreshDaGrViUsuarios()
                    _rowId = Nothing
                Else
                    MessageBox.Show("Error al Intentar Eliminar Usuario!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            ElseIf confirm = DialogResult.No Then
                _rowId = Nothing
            End If
        End If
    End Sub

    Private Sub BtnModificarUsuario_Click(sender As Object, e As EventArgs) Handles BtnModificarUsuario.Click
        If _rowId = Nothing Then
            MessageBox.Show("Seleccione el Usuario que desea Modificar", "Advertencia!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            Dim _frmUsuario As New FrmUsuario
            _frmUsuario.TextUsuarioTextProp = DaGrViUsuarios.CurrentRow.Cells(1).Value.ToString
            _frmUsuario.TextNombresTextProp = DaGrViUsuarios.CurrentRow.Cells(2).Value.ToString
            _frmUsuario.TextEmailTextProp = DaGrViUsuarios.CurrentRow.Cells(3).Value.ToString
            _frmUsuario.TextPasswdTextProp = DaGrViUsuarios.CurrentRow.Cells(4).Value.ToString
            _frmUsuario.CmbPerfilTextProp = DaGrViUsuarios.CurrentRow.Cells(5).Value.ToString
            _frmUsuario.TextUsuarioEnabledProp = False
            _frmUsuario.BtnEnviarFormUsuarioEnabledProp = False
            _frmUsuario.BtnLimpiarCamposUsuarioEnabledProp = False
            _frmUsuario.ShowDialog()
            _rowId = Nothing
        End If
    End Sub

End Class

This is the other form the variable that I need has stored the value of the selected row, I need to be able to use it here

Public Class FrmUsuario

    Private _modelUsuario As New Usuario.Usuario
    Private _logicUsuario As New ControllerUsuario.LogicUsuario
    Private _msjAlert As String
    Private _frmMenu As FrmMenu

    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(frmMenu As FrmMenu)

        Me.New()

        _frmMenu = frmMenu

    End Sub

    Public Property TextUsuarioTextProp()
        Get
            Return TextUsuario.Text
        End Get
        Set(ByVal value)
            TextUsuario.Text = value
        End Set
    End Property

    Public Property TextUsuarioEnabledProp()
        Get
            Return TextUsuario.Enabled
        End Get
        Set(ByVal value)
            TextUsuario.Enabled = value
        End Set
    End Property

    Public Property TextNombresTextProp()
        Get
            Return TextNombres.Text
        End Get
        Set(ByVal value)
            TextNombres.Text = value
        End Set
    End Property

    Public Property TextEmailTextProp()
        Get
            Return TextEmail.Text
        End Get
        Set(ByVal value)
            TextEmail.Text = value
        End Set
    End Property

    Public Property TextPasswdTextProp()
        Get
            Return TextPasswd.Text
        End Get
        Set(ByVal value)
            TextPasswd.Text = value
        End Set
    End Property

    Public Property CmbPerfilTextProp()
        Get
            Return CmbPerfil.Text
        End Get
        Set(ByVal value)
            CmbPerfil.Text = value
        End Set
    End Property

    Public Property BtnEnviarFormUsuarioEnabledProp()
        Get
            Return BtnEnviarFormUsuario.Enabled
        End Get
        Set(ByVal value)
            BtnEnviarFormUsuario.Enabled = value
        End Set
    End Property

    Public Property BtnModificarFrmUsuarioEnabledProp()
        Get
            Return BtnModificarFrmUsuario.Enabled
        End Get
        Set(ByVal value)
            BtnModificarFrmUsuario.Enabled = value
        End Set
    End Property

    Public Property BtnLimpiarCamposUsuarioEnabledProp()
        Get
            Return BtnLimpiarCamposUsuario.Enabled
        End Get
        Set(ByVal value)
            BtnLimpiarCamposUsuario.Enabled = value
        End Set
    End Property

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

    End Sub

    Private Sub CargarCampos()
        _modelUsuario.Usuario = TextUsuarioTextProp
        _modelUsuario.Nombres = TextNombresTextProp
        _modelUsuario.Email = TextEmailTextProp
        _modelUsuario.Passwd = TextPasswdTextProp
        _modelUsuario.Perfil = CmbPerfilTextProp
    End Sub

    Private Sub BtnEnviarFormUsuario_Click(sender As Object, e As EventArgs) Handles BtnEnviarFormUsuario.Click
        CargarCampos()
        If _logicUsuario.Validacion(_modelUsuario, _msjAlert) Then
            _modelUsuario = _logicUsuario.RouteAddUsuario(_modelUsuario.Usuario, _modelUsuario.Nombres, _modelUsuario.Email, _modelUsuario.Passwd, _modelUsuario.Perfil)
            Close()
            MessageBox.Show("Usuario Agregado Exitosamente!", "Informacion!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            _frmMenu.RefreshDaGrViUsuarios()
        Else
            MessageBox.Show(_msjAlert, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub BtnModificarFrmUsuario_Click(sender As Object, e As EventArgs) Handles BtnModificarFrmUsuario.Click
        CargarCampos()
        If _logicUsuario.Validacion(_modelUsuario, _msjAlert) Then

        End If
    End Sub

    Private Sub BtnLimpiarCamposUsuario_Click(sender As Object, e As EventArgs) Handles BtnLimpiarCamposUsuario.Click
        TextUsuarioTextProp = Nothing
        TextNombresTextProp = Nothing
        TextEmailTextProp = Nothing
        TextPasswdTextProp = Nothing
        CmbPerfilTextProp = Nothing
    End Sub

End Class

By being able to use the variable in the User Form, I will create a method to eliminate the DataGridView row by means of the obtained id

    
asked by Christian Gimenez 01.06.2016 в 20:05
source

2 answers

0

Just as you pass the instance of the form in the constructor you could pass the value, or you could expose that variable as property and take it from the other form by means of this.

In the first form you define

Private Sub BtnAgregarUsuario_Click(sender As Object, e As EventArgs) Handles BtnAgregarUsuario.Click
    Dim _frmUsuario As New FrmUsuario(Me, _rowId)
    _frmUsuario.BtnModificarFrmUsuarioEnabledProp = False
    _frmUsuario.ShowDialog()
End Sub

in the second

Public Class FrmUsuario

    Private _modelUsuario As New Usuario.Usuario
    Private _logicUsuario As New ControllerUsuario.LogicUsuario
    Private _msjAlert As String
    Private _frmMenu As FrmMenu
    Private _rowId As String

    Public Sub New()

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

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(frmMenu As FrmMenu, rowId As String)

        Me.New()

        _rowId = rowId
        _frmMenu = frmMenu

    End Sub

..

As you will see, you receive the value in the constructor

    
answered by 01.06.2016 / 20:37
source
0

Any data you want to pass can be passed through public property as you are doing with the other data.

In any case I would try to separate the logic of each form in the code itself. You could create a class in which you can establish all the information that needs to be passed between the forms and establish a public property through which to pass the information to the editing form or retrieve it from it after finalizing the edition.

You have an example of how to create a dialog to edit rows of a DataGridView in this way:

Form to edit the rows of a DataGridView

    
answered by 01.06.2016 в 20:37