Quantcast
Channel: Active questions tagged return-value - Stack Overflow
Viewing all articles
Browse latest Browse all 207

how to send/get values from mainform label to Childform1 label and Childform2 label with VB.NET

$
0
0

I'm Trying to send/get values from mainform label to Childform1 label and Childform2 label with VB.NET

If you see the gif (gif result code) in mainform then there appears ADMIN (LblUsername) and A (LblLocation) so I want to appear in OPENFORM1 & OPENFORM2

Please Guide Me

Code in LoginForm

Imports System.Data.OleDbPublic Class LoginForm    Inherits BaseFixedForm    Public Sub New()        InitializeComponent()    End Sub    Private Sub Login()        Dim userModel = New UserModel().Login(txtUsername.Text, txtPassword.Text)        If userModel IsNot Nothing Then            Dim mainForm As Form            mainForm = New MainForm(userModel)            Me.Hide()            AddHandler mainForm.FormClosed, AddressOf MainForm_SessionClosed            mainForm.Show()        Else            ShowMessage("Incorrect username or password")        End If    End Sub    Private Sub ShowMessage(message As String)        lblErrorMessage.Text = "    " & message        lblErrorMessage.Visible = True    End Sub    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click        Login()    End Sub    Private Sub MainForm_SessionClosed(sender As Object, e As FormClosedEventArgs)        Logout()    End Sub    Private Sub Logout()        Me.Show()        lblErrorMessage.Visible = False        txtPassword.Clear()        txtUsername.Clear()        txtUsername.Focus()    End SubEnd ClassPublic Class UserDao    Private ReadOnly connectionString As String    Public Sub New()        connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\test07092024.accdb;Persist Security Info=False;"    End Sub    Protected Function GetConnection() As OleDbConnection        Return New OleDbConnection(connectionString)    End Function    Public Function Login(ByVal username As String, ByVal password As String) As User'Validate the username and password for login.        Using connection = GetConnection() 'Obtener la conexion.            connection.Open() 'Open the connection.            Using command = New OleDbCommand()                command.Connection = connection                command.CommandText = "select *from Users where (userName=@username and password=@pass)"                command.Parameters.AddWithValue("@username", username)                command.Parameters.AddWithValue("@pass", password)                command.CommandType = CommandType.Text                Dim reader As OleDbDataReader = command.ExecuteReader()                If reader.Read() Then                    Dim userObj = New User With {                        .Id = CInt(reader(0)),                        .Username = reader(1).ToString(),                        .Location = reader(3).ToString()                    }                    Return userObj                Else                    Return Nothing                End If            End Using        End Using    End FunctionEnd ClassPublic Class UserModel    Private _id As Integer    Private _username As String    Private _password As String    Private _Location As String    Private _userDao As UserDao    Public Sub New()        _userDao = New UserDao()    End Sub    Public Property Id As Integer        Get            Return _id        End Get        Set(ByVal value As Integer)            _id = value        End Set    End Property    Public Property Username As String        Get            Return _username        End Get        Set(ByVal value As String)            _username = value        End Set    End Property    Public Property Password As String        Get            Return _password        End Get        Set(ByVal value As String)            _password = value        End Set    End Property    Public Property Location As String        Get            Return _Location        End Get        Set(ByVal value As String)            _Location = value        End Set    End Property    Public Function Login(ByVal username As String, ByVal password As String) As UserModel'Login.        Dim result = _userDao.Login(username, password)        If result IsNot Nothing Then            Return MapUserModel(result)        Else            Return Nothing        End If    End Function'Mapping entity model to domain model.    Private Function MapUserModel(ByVal userEntity As User) As UserModel'Map a single object.        Dim userModel = New UserModel() With {            .Id = userEntity.Id,            .Username = userEntity.Username,            .Password = userEntity.Password,            .Location = userEntity.Location        }        Return userModel    End FunctionEnd ClassPublic Class User    Public Property Id As Integer    Public Property Username As String    Public Property Password As String    Public Property Location As StringEnd Class

Code in MainForm

Public Class MainForm    Inherits BaseMainForm    Private listChildForms As List(Of Form)    Private activeChildForm As Form 'Obtains or sets the currently displayed child form.    Public Sub New(connectedUser As UserModel)'Use this constructor at login and submit a user view model        InitializeComponent()'Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None        listChildForms = New List(Of Form)()        LoadUserData(connectedUser)    End Sub    Public Sub LoadUserData(connectedUser As UserModel)'Upload the logged-in user's data in the side menu.        LblUsername.Text = connectedUser.Username        LblLocation.Text = connectedUser.Location    End Sub    Private Sub OpenChildForm(Of ChildForm As {Form, New})(ByVal senderMenuButton As Object)        Dim menuButton As Button = CType(senderMenuButton, Button) ' Where the form opens, you can submit a null value, if you're not trying to open a form from a different control than the side menu buttons.        Dim form As Form = listChildForms.OfType(Of ChildForm)().FirstOrDefault() 'Find if the form is already instantiated or has been previously displayed.        If activeChildForm IsNot Nothing AndAlso form Is activeChildForm Then Return 'If the form is the same as the current active form, return and do nothing.        If form Is Nothing Then 'If the form doesn't exist, then create the instance and display it in the desktop panel.            form = New ChildForm() 'Instantiation form.              form.FormBorderStyle = FormBorderStyle.None 'Remove the Edge.            form.TopLevel = False 'Indicate that the form is not top-level             form.Dock = DockStyle.Fill 'Set the Dock Style to Full (Fill the Desktop Panel)                      listChildForms.Add(form) 'Add Child Form to Form List.            If menuButton IsNot Nothing Then 'If the menu button is different from null:                ActivateButton(menuButton) 'Activate/Highlight the button.                AddHandler form.FormClosed, Sub(s, e) DeactivateButton(menuButton) 'When the form closes, disable the button.            End If            btnChildFormClose.Visible = True 'Show the Close Secondary Form Button.        End If        CleanDesktop() 'Delete the current child form from the desktop panel        panelDesktop.Controls.Add(form) 'Add Secondary Form to the Desktop Dashboard        panelDesktop.Tag = form ' Store the form        form.Show() 'Show the form        form.BringToFront() ' Bring to the front        form.Focus() 'Focus the form'lblCaption.Text = form.Text 'Set the title of the form.        activeChildForm = form 'Set as active form.    End Sub    Private Sub ActivateButton(menuButton As Button)        menuButton.ForeColor = Color.RoyalBlue'menuButton.BackColor = panelMenuHeader.BackColor;    End Sub    Private Sub DeactivateButton(menuButton As Button)        menuButton.ForeColor = Color.SlateGray'menuButton.BackColor = panelSideMenu.BackColor;    End Sub    Private Sub CleanDesktop()        If activeChildForm IsNot Nothing Then            activeChildForm.Hide()            panelDesktop.Controls.Remove(activeChildForm)        End If    End Sub#Region "- Open Secondary Forms"    Private Sub btnOPENFORM1_Click(sender As Object, e As EventArgs) Handles btnOPENFORM1.Click        OpenChildForm(Of OPENFORM1)(sender)    End Sub    Private Sub btnOPENFORM2_Click(sender As Object, e As EventArgs) Handles btnOPENFORM2.Click        OpenChildForm(Of OPENFORM2)(sender)    End Sub#End RegionEnd Class

Result code in file gif.

Result code in file gif

Desired result

OPENFORM1ADMINAOPENFORM2ADMINA

Update code

So in the OPENFORM1 form and OPENFORM2 Have each label, namely : LblUsername & LblLocation

code in OPENFORM1

Public Class OPENFORM1    Private Sub OPENFORM1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        MainForm.LblUsername.Text = LblUsername.Text        MainForm.LblLocation.Text = LblLocation.Text    End SubEnd Class

code in OPENFORM2

Public Class OPENFORM2    Private Sub OPENFORM2_Load(sender As Object, e As EventArgs) Handles MyBase.Load        MainForm.LblUsername.Text = LblUsername.Text        MainForm.LblLocation.Text = LblLocation.Text    End SubEnd Class

but I have an error Reference to a non-shared member requires an object reference


Viewing all articles
Browse latest Browse all 207

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>