my global variables not working

hello

thanks in advance

i made a module declared public variables. when i login in one form and set a variable to user anem and want to display that user name on another form it never works.

i try to write the variable to the label.text = strvariable kind of thing

my module

Module subMain

'Decleration of global variables

Public strUserName As String

Public strPassNumber As String

Public strCurrentUser As String

End Module

then in another form i have this

lblCurrentUser.Text = strCurrentUser

but the lbl, label nevr show the user name

please can somebody help

thanks

ian



Answer this question

my global variables not working

  • Jason Holt

    if its class that u declare your variables,u need to import it

    for example:

    import var_class



  • arturm

    Hello

    firstly, thankyou sir for your help, it is very much appreciated in this frustrating time.

    here is my module code

    Module subMain

    'Decleration of global variables

    Public strUserName As String

    Public strPassNumber As String

    Public strCurrentUser As String

    End Module

    and here is the login from code

    Public Class frmLogin

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLog_Close.Click

    Me.Hide()

    End Sub

    Private Sub txtPassNumber_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassNumber.Click

    strPassNumber = txtPassNumber.Text

    End Sub

    Friend Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

    strUserName = txtUserName.Text

    strPassNumber = txtPassNumber.Text

    Select Case strPassNumber

    Case Is = 0 And strUserName = "Safe"

    MsgBox("level 0")

    strCurrentUser = ("Safe")

    Case Is = 1 And strUserName = "Trainee"

    MsgBox("level 1 Succesfull")

    strCurrentUser = ("Trainee")

    Case Is = 2 And strUserName = "Operator"

    MsgBox("level 2 Succesfull")

    strCurrentUser = ("Operator")

    Case Is = 3 And strUserName = "Technical"

    MsgBox("level 3 Succesfull")

    strCurrentUser = ("Technical")

    Case Is = 4 And strUserName = "Supervisor"

    MsgBox("level 4 Succesfull")

    strCurrentUser = ("Supervisor")

    Case Is = 5 And strUserName = "Maintenance"

    MsgBox("level 5 Succesfull")

    strCurrentUser = ("Maintenance")

    Case Is = 6 And strUserName = "Administrator"

    MsgBox("level 6 Succesfull")

    strCurrentUser = ("Administrator")

    Case Else

    strUserName = "Safe"

    strPassNumber = "0"

    MsgBox("Login Failed")

    Beep()

    End Select

    End Sub

    Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click

    strUserName = "Safe"

    strPassNumber = "0"

    txtUserName.Text = strUserName

    txtPassNumber.Text = strPassNumber

    MsgBox("System now secure as you have logged out succesfully")

    Beep()

    End Sub

    End Class

    and here is the main form that will not show for some reason the current user name

    thanks again, im running out of hair, but its fascianting, as I am a beginner

    many thanks

    ian


  • Fregas

    Hello

    yes you were correct . i changed it to activated from got focus and the thing worked first time. Its not easy for a beginner but i have purchased some good learning material. Again, many thanks ian


  • ASR

    Do you have Option Explicit On or Off

    Do you have any Imports statements

    My thought is that if you either arent setting this variable or that you arent fully qualify the variable name when you are using it but as you dont show the code which is setting the variable then are you sure it was actually set.

    lblCurrentUser.Text = subMain.strCurrentUser

    Please provide more details....


  • Yuval124

    hello

    thanks

    the login actually works, the problem seems to be that the current user logged in will not show on another form in a label. its about the 21 line down this page from option explicit on(inclusive) were it is not showing the user currently logged on. i appreciate your help and understanding

    I include the the text for the main form below

    Option Explicit On

    Public Class frm_Main_Menu

    Inherits System.Windows.Forms.Form

    Dim graphics As System.Drawing.Graphics

    Dim brushcolor0 As New SolidBrush(Color.Red)

    Dim brushcolor1 As New SolidBrush(Color.Green)

    Dim brushcolor2 As New SolidBrush(Color.Yellow)

    Dim Pencolor0 As New System.Drawing.Pen(System.Drawing.Color.Red)

    Dim Pencolor1 As New System.Drawing.Pen(System.Drawing.Color.Green)

    Dim Pencolor2 As New System.Drawing.Pen(System.Drawing.Color.Black)

    Public Sub frm_Main_Menu_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus

    graphics = Me.CreateGraphics

    graphics.FillEllipse(brushcolor0, 1150, 450, 40, 40) ' couch indicator

    graphics.FillEllipse(brushcolor0, 1200, 400, 40, 40) ' jet indicator

    graphics.FillEllipse(brushcolor0, 1100, 420, 40, 40) ' lead roll 1 indicator

    Me.lblCurrentUser.Text = subMain.strCurrentUser

    End Sub

    Public Sub frm_Main_Menu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Dim lblTime As DateTime

    frm_Splash.Close()

    lblCurrentUser.Text = subMain.strCurrentUser

    'Dim graphics As System.Drawing.Graphics

    'Dim Pencolor As New System.Drawing.Pen(System.Drawing.Color.Red)

    graphics = Me.CreateGraphics

    graphics.FillEllipse(brushcolor0, 1150, 450, 40, 40) ' couch indicator

    graphics.FillEllipse(brushcolor0, 1200, 400, 40, 40) ' jet indicator

    graphics.FillEllipse(brushcolor0, 1100, 420, 40, 40) ' lead roll 1 indicator

    graphics.DrawLine(Pencolor0, 900, 400, 50, 50)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    graphics.FillEllipse(brushcolor1, 1150, 450, 40, 40)

    graphics.FillEllipse(brushcolor1, 1150, 450, 40, 40)

    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click

    frmAbout.Show()

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    Application.Exit()

    End Sub

    Private Sub LoginToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoginToolStripMenuItem.Click

    frmLogin.Show()

    End Sub

    End Class


  • Breakgate

    I think you'll find that the offending code is in the Login Button.

    You are using in the case statements is = 0 but 0 is an integer and the strPassNumber is a string. This is assuming the user will only be allowed to ensure numbers in the passnumber textbox. The rest appeared to be OK.

    Module subMain

    'Decleration of global variables
    Public strUserName As
    String
    Public strPassNumber As
    String
    Public strCurrentUser As
    String

    End Module

    Public Class Form1
    '//TEST FORM TO CALL THE LOGIN FORM AND LOOK AT THE GLOBALS AFTERWARDS

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x As New frmLogin
    x.ShowDialog()

    MsgBox(strCurrentUser)
    End Sub
    End Class

    Public Class frmLogin
    Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogout.Click
    strUserName = "Safe"
    strPassNumber = "0"
    txtUserName.Text = strUserName
    txtPassNumber.Text = strPassNumber
    MsgBox("System now secure as you have logged out succesfully")
    Beep()
    End Sub


    Private Sub BtnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnLogin.Click

    strUserName = txtUserName.Text
    strPassNumber = txtPassNumber.Text

    Select Case CType(strPassNumber, Integer)
    Case Is = 0 And strUserName = "Safe"
    MsgBox("level 0")
    strCurrentUser = "Safe"
    Case Is = 1 And strUserName = "Trainee"
    MsgBox("level 1 Succesfull")
    strCurrentUser = "Trainee"
    Case Is = 2 And strUserName = "Operator"
    MsgBox("level 2 Succesfull")
    strCurrentUser = "Operator"
    Case Is = 3 And strUserName = "Technical"
    MsgBox("level 3 Succesfull")
    strCurrentUser = "Technical"
    Case Is = 4 And strUserName = "Supervisor"
    MsgBox("level 4 Succesfull")
    strCurrentUser = "Supervisor"
    Case Is = 5 And strUserName = "Maintenance"
    MsgBox("level 5 Succesfull")
    strCurrentUser = "Maintenance"
    Case Is = 6 And strUserName = "Administrator"
    MsgBox("level 6 Succesfull")
    strCurrentUser = "Administrator"
    Case Else
    strUserName = "Safe"
    strPassNumber = "0"
    MsgBox("Login Failed")
    Beep()
    End Select
    End Sub
    End Class


  • James_lh

    Imports was not required as the variable had been fully qualified in the code.


  • StuartMiller

    Your problem is that the Form.Gotfocus event is probably never occuring. I'd stick a breakpoint in your code and see if the event is ever fired. My thought is it wont be.

    If its not then try changing the code to use the Activated event instead of the gotfocus - I'm sure this will fix your issues. Either way the global variable is set.

    Private Sub frm_Main_Menu_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

    graphics = Me.CreateGraphics
    graphics.FillEllipse(brushcolor0, 1150, 450, 40, 40) ' couch indicator
    graphics.FillEllipse(brushcolor0, 1200, 400, 40, 40) ' jet indicator
    graphics.FillEllipse(brushcolor0, 1100, 420, 40, 40) ' lead roll 1 indicator
    Me.lblCurrentUser.Text = subMain.strCurrentUser

    End Sub

    To find more on these events check out help files / MSDN / Web search


  • my global variables not working