Accessing varables

Dimension

Member
Joined
Jul 12, 2008
Messages
11
Programming Experience
1-3
I'm having some trouble with accessing variables on a button click (txtUsername and txtPassword). I'm not the greatest with vb, but here is my code if anyone can spot what i'm doing wrong...

VB.NET:
Public Class limelight
    Public ds As Data.DataSet
    Private Sub limelight_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mainPanel As New Panel
        mainPanel.Parent = Me
        mainPanel.Size = New Size(1024, 768)
        mainPanel.Location = New Point(((Me.Width / 2) - (mainPanel.Width / 2)), ((Me.Height / 2) - (mainPanel.Height / 2)))
        mainPanel.BorderStyle = BorderStyle.FixedSingle
        mainPanel.BackColor = Color.Gainsboro

        Dim loginpanel As New Panel
        loginpanel.Parent = mainPanel
        loginpanel.Size = New Size(500, 350)
        loginpanel.Location = New Point(((mainPanel.Width / 2) - (loginpanel.Width / 2)), ((mainPanel.Height / 2) - (loginpanel.Height / 2)))
        loginpanel.BorderStyle = BorderStyle.FixedSingle


        Dim lblLoginTitle As New Label
        lblLoginTitle.Parent = loginpanel
        lblLoginTitle.Size = New Size(loginpanel.Width - 25, 50)
        lblLoginTitle.Location = New Point(((loginpanel.Width / 2) - (lblLoginTitle.Width / 2)), 10)
        lblLoginTitle.Text = "Login Required"
        lblLoginTitle.Font = New Font("Arial", 20, FontStyle.Bold)
        lblLoginTitle.TextAlign = ContentAlignment.MiddleCenter
        lblLoginTitle.BackColor = Color.FromArgb(192, 255, 192)

        Dim lblUsername As New Label
        lblUsername.Parent = loginpanel
        lblUsername.Size = New Size(150, 35)
        lblUsername.Location = New Point(100, 125)
        lblUsername.Text = "Username:"
        lblUsername.Font = New Font("Arial", 16, FontStyle.Bold)
        lblUsername.TextAlign = ContentAlignment.MiddleCenter

        Dim txtUsername As New TextBox
        txtUsername.Parent = loginpanel
        txtUsername.Size = New Size(150, 50)
        txtUsername.Location = New Point(250, 125)
        txtUsername.Font = New Font("Arial", 16, FontStyle.Bold)
        txtUsername.MaxLength = 8

        Dim lblPassword As New Label
        lblPassword.Parent = loginpanel
        lblPassword.Size = New Size(150, 35)
        lblPassword.Location = New Point(100, 175)
        lblPassword.Text = "Password:"
        lblPassword.Font = New Font("Arial", 16, FontStyle.Bold)
        lblPassword.TextAlign = ContentAlignment.MiddleCenter

        Dim txtPassword As New TextBox
        txtPassword.Parent = loginpanel
        txtPassword.PasswordChar = "*"
        txtPassword.Size = New Size(150, 50)
        txtPassword.Location = New Point(250, 175)
        txtPassword.Font = New Font("Arial", 16, FontStyle.Bold)
        txtPassword.MaxLength = 8

        Dim btnLogin As New Button
        btnLogin.Parent = loginpanel
        btnLogin.Size = New Size(100, 50)
        btnLogin.Location = New Point(((loginpanel.Width / 2) - (btnLogin.Width / 2)), 250)
        btnLogin.Font = New Font("Arial", 16, FontStyle.Bold)
        btnLogin.BackColor = Color.LightGray
        btnLogin.FlatStyle = FlatStyle.Flat
        btnLogin.Text = "Login"



    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim checkUser As New DatabaseDataSetTableAdapters.usersTableAdapter
        Dim dt As Data.DataTable
        dt = checkUser.GetUser(txtUsername.text, txtPassword.text)


    End Sub

End Class
 
Use the Designer to add and configure your controls and problem is fixed. Don't waste hours coding something when you can have the Visual tools generate it for you correctly with a few mouse clicks. It's what Visual Basic and any visual design development is about.

As a learning experience you should also have a look at how the Designer generates code when you interact with the visual elements. In Solution Explorer click the "Show all" icon, expand one of the project nodes, for example for limelight.vb you will find limelight.Designer.vb.

When in need of coding to add dynamic controls you have to decide if you can, as the Designer does, define class level variables that can be used from other places inside or outside the class, or if you must resolve the references at runtime by other means, for example by searching/indexing a Controls collection.
 
ok so I see how it would be easier to drag and drop controls but...

the idea was to have a login screen, once logged in, hide that panel and have a new panel display etc. would it not start to get confusing as to what controls im trying to select if they are all laying over the top of each other on the form?
 
I wouldn't put everything in one form, create a login form displayed as a dialog from application Startup event and use it to determine if your application should start or not.

In the case of totally redefining a form UI create UserControls that can be swapped with minimum of code. You design UserControls the same way as Forms, and they can be treated the same way as any other class as a single object.
 
Possible Solution

If you using a database to store your username/password combinations,
I would do exactly what JohnH said, but I would make the Login form the Startup Form in the Project Designer, and make the 'Login' button check against the database/dataset and, if it finds a match, open the "main" form.
 
Back
Top