visualbasicuser Posted June 17, 2003 Posted June 17, 2003 I use Visual Basic.Net and have been having a problem passing a variable between forms without using button clicks. I defined the TestVar in Module1 as "Adam". I expect the Public Variable to pass to the Form1 however I have to execute a button click to pass the contents of the variable to the form. Why?? Do I have to do this! I really don't want to!!! HELP!!!!!! Why does my variable that has been declared Public in Module1 not pass on to Form1??? Here's an example: Module Module1 Public TestVar As String Public NewForm1 As New Form1() Public NewForm2 As New Form2() Public Sub Main() TestVar = "Adam" NewForm1.ShowDialog() End Sub End Module Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call Call GetVar() End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.Button2 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(48, 32) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(104, 64) Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(40, 152) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(312, 20) Me.TextBox1.TabIndex = 1 Me.TextBox1.Text = "TextBox1" ' 'Button2 ' Me.Button2.Font = New System.Drawing.Font("Tahoma", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button2.Location = New System.Drawing.Point(32, 264) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(104, 48) Me.Button2.TabIndex = 2 Me.Button2.Text = "Exit" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(496, 341) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.TextBox1, Me.Button1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region 'GetVar() executed in form1 Public Sub GetVar() TextBox1.Text = TestVar <========== Why is a blank displayed? I expected to see "Adam" displayed End Sub End Class Quote
Administrators PlausiblyDamp Posted June 17, 2003 Administrators Posted June 17, 2003 Under the project settings what is your startup object? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hog Posted June 17, 2003 Posted June 17, 2003 why don't you try this in the forms load event Me.TextBox1.Text = TestVar Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.