I created a program a while back, in the traditional VB.NET way by drawing a bunch of forms. I have started playing around with creating the forms at runtime using classes. I have a placeholder form called frmStartup which merely calls the CreateLogin() sub from the CreateFormClass. The CreateLogin() sub creates a new blank form, then calls the grpbxLogin(), btnSave() and btnExit() subs from the FormElementClass which adds all the buttons and textboxes to the form. Within the FormElementClass, I have associated btnSave and btnExit with event handlers. These even handlers call subs from the DataProcessingClass. The event handler for Exit works just fine.
I am having a problem with the Save event handler. Precisely, I don't know how to pass either the username or password from the Login form to the btnLogin_Click() sub in the DataProcessingForm. I have tried using Me. and frmLogin., but the program doesn't see either of those. If the Login form is an instance that is disposed of once another Sub is called, then it won't exist later on to call from. I have tried declaring variables and passing those along to btnLogin_Click, but no matter where I put it, the program doesn't see txtbxUsername.Text or txtbxPassword.Text. Not even from within FormElementsClass, which is were the boxes are created.
Any ideas on how to pass variables in this case? I am not even sure what you would call this such as passing between classes, or passing between instances.
--------------------------------------------------------------------------
frmSTARTUP.VB
Imports System
Imports System.Data
Imports System.Windows
Imports Login.CreateForm
Public Class frmStartup
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using CreateFormObject As New CreateFormClass
CreateFormObject.CreateLogin()
End Using
End Sub
End Class
--------------------------------------------------------------------------
CREATEFORMCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.FormElements
Imports Login.DataProcessing
Namespace CreateForm
Public Class CreateFormClass
Inherits ObjectDisposeClass
Public Sub CreateLogin()
Dim frmLogin As New Form
frmLogin.Text = "Login"
frmLogin.MaximizeBox = False
frmLogin.MinimizeBox = False
frmLogin.ControlBox = False
frmLogin.Height = 160
frmLogin.Width = 325
Using FormElementsObject As New FormElementsClass
FormElementsObject.grpbxLogin(frmLogin, 5, 10)
FormElementsObject.btnLogin(frmLogin, 150, 90)
FormElementsObject.btnExit(frmLogin, 230, 90)
End Using
frmLogin.Show()
End Sub
#Region " IDisposable Object ..."
Edited out the code
#End Region
End Namespace
-------------------------------------------------------------------------
FORMELEMENTSCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.CreateForm
Imports Login.DataProcessing
Namespace FormElements
Public Class FormElementsClass
Inherits ObjectDisposeClass
Public Sub grpbxLogin(ByVal frmName, ByVal x, ByVal y)
Dim grpbxLogin As New GroupBox
grpbxLogin.Location = New Point(x, y)
grpbxLogin.Width = 300
grpbxLogin.Height = 70
grpbxLogin.Visible = True
frmName.Controls.Add(grpbxLogin)
Dim lblUsername As New Label
lblUsername.Text = "USERNAME:"
lblUsername.TextAlign = ContentAlignment.MiddleRight
lblUsername.Visible = True
lblUsername.Height = 20
lblUsername.Width = 100
lblUsername.Location = New Point(5, 15)
grpbxLogin.Controls.Add(lblUsername)
Dim lblPassword As New Label
lblPassword.Text = "PASSWORD:"
lblPassword.TextAlign = ContentAlignment.MiddleRight
lblPassword.Visible = True
lblPassword.Height = 20
lblPassword.Width = 100
lblPassword.Location = New Point(5, 40)
grpbxLogin.Controls.Add(lblPassword)
Dim txtbxUsername As New TextBox
txtbxUsername.Text = ""
txtbxUsername.Visible = True
txtbxUsername.Height = 20
txtbxUsername.Width = 150
txtbxUsername.Location = New Point(105, 15)
grpbxLogin.Controls.Add(txtbxUsername)
Dim txtbxPassword As New TextBox
txtbxPassword.Text = ""
txtbxPassword.Visible = True
txtbxPassword.Height = 20
txtbxPassword.Width = 150
txtbxPassword.Location = New Point(105, 40)
grpbxLogin.Controls.Add(txtbxPassword)
End Sub
Public Sub btnLogin(ByVal frmName, ByVal x, ByVal y)
Dim btnLogin As New Button
btnLogin.Visible = True
btnLogin.Text = "LOGIN"
btnLogin.Location = New Point(x, y)
Using DataProcessingObject As New DataProcessingClass
AddHandler btnLogin.Click, AddressOf DataProcessingObject.btnLogin_Click(Username, Password)
End Using
frmName.Controls.Add(btnLogin)
End Sub
Public Sub btnExit(ByVal frmName, ByVal x, ByVal y)
Dim btnExit As New Button
btnExit.Visible = True
btnExit.Text = "EXIT"
btnExit.Location = New Point(x, y)
Using DataProcessingObject As New DataProcessingClass
AddHandler btnExit.Click, AddressOf DataProcessingObject.btnExit_Click
End Using
frmName.Controls.Add(btnExit)
End Sub
#Region " IDisposable Object ..."
Edited out code
#End Region
End Namespace
-------------------------------------------------------------------------
DATAPROCESSINGCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.CreateForm
Imports Login.FormElements
Namespace DataProcessing
Public Class DataProcessingClass
Inherits ObjectDisposeClass
Public Sub btnLogin_Click(ByVal Username, ByVal Password)
Bunch of MySQL code for verifying username and password
End Sub
Public Sub btnExit_Click()
Environment.Exit(0)
End Sub
End Class
#Region " IDisposable Object ..."
Edited code out
#End Region
End Namespace
--------------------------------------------------------------------------
I am having a problem with the Save event handler. Precisely, I don't know how to pass either the username or password from the Login form to the btnLogin_Click() sub in the DataProcessingForm. I have tried using Me. and frmLogin., but the program doesn't see either of those. If the Login form is an instance that is disposed of once another Sub is called, then it won't exist later on to call from. I have tried declaring variables and passing those along to btnLogin_Click, but no matter where I put it, the program doesn't see txtbxUsername.Text or txtbxPassword.Text. Not even from within FormElementsClass, which is were the boxes are created.
Any ideas on how to pass variables in this case? I am not even sure what you would call this such as passing between classes, or passing between instances.
--------------------------------------------------------------------------
frmSTARTUP.VB
Imports System
Imports System.Data
Imports System.Windows
Imports Login.CreateForm
Public Class frmStartup
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Using CreateFormObject As New CreateFormClass
CreateFormObject.CreateLogin()
End Using
End Sub
End Class
--------------------------------------------------------------------------
CREATEFORMCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.FormElements
Imports Login.DataProcessing
Namespace CreateForm
Public Class CreateFormClass
Inherits ObjectDisposeClass
Public Sub CreateLogin()
Dim frmLogin As New Form
frmLogin.Text = "Login"
frmLogin.MaximizeBox = False
frmLogin.MinimizeBox = False
frmLogin.ControlBox = False
frmLogin.Height = 160
frmLogin.Width = 325
Using FormElementsObject As New FormElementsClass
FormElementsObject.grpbxLogin(frmLogin, 5, 10)
FormElementsObject.btnLogin(frmLogin, 150, 90)
FormElementsObject.btnExit(frmLogin, 230, 90)
End Using
frmLogin.Show()
End Sub
#Region " IDisposable Object ..."
Edited out the code
#End Region
End Namespace
-------------------------------------------------------------------------
FORMELEMENTSCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.CreateForm
Imports Login.DataProcessing
Namespace FormElements
Public Class FormElementsClass
Inherits ObjectDisposeClass
Public Sub grpbxLogin(ByVal frmName, ByVal x, ByVal y)
Dim grpbxLogin As New GroupBox
grpbxLogin.Location = New Point(x, y)
grpbxLogin.Width = 300
grpbxLogin.Height = 70
grpbxLogin.Visible = True
frmName.Controls.Add(grpbxLogin)
Dim lblUsername As New Label
lblUsername.Text = "USERNAME:"
lblUsername.TextAlign = ContentAlignment.MiddleRight
lblUsername.Visible = True
lblUsername.Height = 20
lblUsername.Width = 100
lblUsername.Location = New Point(5, 15)
grpbxLogin.Controls.Add(lblUsername)
Dim lblPassword As New Label
lblPassword.Text = "PASSWORD:"
lblPassword.TextAlign = ContentAlignment.MiddleRight
lblPassword.Visible = True
lblPassword.Height = 20
lblPassword.Width = 100
lblPassword.Location = New Point(5, 40)
grpbxLogin.Controls.Add(lblPassword)
Dim txtbxUsername As New TextBox
txtbxUsername.Text = ""
txtbxUsername.Visible = True
txtbxUsername.Height = 20
txtbxUsername.Width = 150
txtbxUsername.Location = New Point(105, 15)
grpbxLogin.Controls.Add(txtbxUsername)
Dim txtbxPassword As New TextBox
txtbxPassword.Text = ""
txtbxPassword.Visible = True
txtbxPassword.Height = 20
txtbxPassword.Width = 150
txtbxPassword.Location = New Point(105, 40)
grpbxLogin.Controls.Add(txtbxPassword)
End Sub
Public Sub btnLogin(ByVal frmName, ByVal x, ByVal y)
Dim btnLogin As New Button
btnLogin.Visible = True
btnLogin.Text = "LOGIN"
btnLogin.Location = New Point(x, y)
Using DataProcessingObject As New DataProcessingClass
AddHandler btnLogin.Click, AddressOf DataProcessingObject.btnLogin_Click(Username, Password)
End Using
frmName.Controls.Add(btnLogin)
End Sub
Public Sub btnExit(ByVal frmName, ByVal x, ByVal y)
Dim btnExit As New Button
btnExit.Visible = True
btnExit.Text = "EXIT"
btnExit.Location = New Point(x, y)
Using DataProcessingObject As New DataProcessingClass
AddHandler btnExit.Click, AddressOf DataProcessingObject.btnExit_Click
End Using
frmName.Controls.Add(btnExit)
End Sub
#Region " IDisposable Object ..."
Edited out code
#End Region
End Namespace
-------------------------------------------------------------------------
DATAPROCESSINGCLASS.VB
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Windows
Imports Login.CreateForm
Imports Login.FormElements
Namespace DataProcessing
Public Class DataProcessingClass
Inherits ObjectDisposeClass
Public Sub btnLogin_Click(ByVal Username, ByVal Password)
Bunch of MySQL code for verifying username and password
End Sub
Public Sub btnExit_Click()
Environment.Exit(0)
End Sub
End Class
#Region " IDisposable Object ..."
Edited code out
#End Region
End Namespace
--------------------------------------------------------------------------