Hey guys

I have 2 forms. One is a log in screen and another is a main menu. I need to retrieve the data from the textbox in the login screen to the textbox in the main menu.

I have sourced around the internet and have implemented this code into where the main menu loads TextBox1.Text = FrmLogIn.TxtUserName.Text This code works however everytime I log out and log back in again, the textbox which is meant to be retrieving the code from the login screen, remains empty!! ...........
I have a clearfields() function located in logout button to empty the textbox....if that helps

Do any of you guys have an idea where Im going wrong??

Thanks in advanced :)

Dani AI

Generated

Quick summary and root cause

described the common symptom: copying the username by reading another form's TextBox works initially, but after logout / relogin the value is gone. The usual causes are: the login form was closed (disposed), a different form instance is being used, or the field was cleared before you read it. pointed out the dispose-on-close issue and suggested hiding the form; showed creating a new main form and assigning a value. Those ideas are all valid, but they point to two better patterns to avoid fragile code.

Recommended patterns (safe and maintainable)

  1. Pass data into the main form via a property or constructor so the main form owns the username instead of reaching across to another form. Example property-based pattern:
Public Class frmMainMenu
    Private _username As String

    Public Property LoggedUser() As String
        Get
            Return _username
        End Get
        Set(ByVal value As String)
            _username = value
            lblWelcome.Text = "User: " & value
        End Set
    End Property
End Class

When logging in, set that property on the same instance you show.

  1. Use a small application-level session holder if many places need the username:
Module SessionState
    Public CurrentUser As String = String.Empty
End Module

Set SessionState.CurrentUser at login and read it everywhere. This avoids coupling forms together.

Troubleshooting tips and cautions

  • Do not mix VB default form instances and manually New instances; you can end up setting values on an instance that is never shown.
  • Hiding the login form (instead of closing) preserves control values, but holding hidden forms long-term wastes resources—better to store the username in a variable/property and then close the login form.
  • On logout: record the username (from your session variable or main-form property) before you clear any UI fields so your audit always has the correct value.
  • Use breakpoints or Debug.Print to confirm which instance holds the username if things still behave oddly.

These approaches keep UI controls isolated and make login/logout logging reliable.

Recommended Answers

All 4 Replies

You are probably closing the form which will set the textbox to empty.
When you bring up your logon form copy the text you saved back to the logon form.

FrmLogIn.TxtUserName.Text = TextBox1.Text

or hide login form (don't close it). so value of txtusername still exist.

Hey guys I have solved the problem through other means

What my object was to be able to register times the user logs in and register times the user logs out of the system.

Logging in was no problem, I just registered the value in the textbox along with the date at the time.

Logging out was the problem as I couldnt transfer the username from the login screen to the mein menu so then once logout was initiated it would take the value out of the textbox and register it.

However I managed this by not initially clearing the contents of the login form once logged in and once it came to logging out the form, i would register the username inputted into the the frm log in and then clear the contents of the log in form.

I hope I explained it clearly,

but thanks again for your input guys, your the ones that gave me the idea.
:)

Hey

you can also used this one

Private Sub ButtonOk_Click
Dim obj as new frm_MainMenu

obj.txtbox2.text = txt1.text

End Sub

ok?! hope you get it dude!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.