HDokes Posted June 30, 2003 Posted June 30, 2003 Hello, I'd like to speak to Mr. Obvious!? I have a series of button controls on the main form. I am trying to alter their text and tag properties while in another form. My biggist problem... and I shutter to think that it's staring me betwixt the eyes, how do I reference the main form and subsequently its controls while in the other form? The main form is called..... well.... main of course. Thought I could reference it's controls from the 2nd form by using the "Main.button" notation.... however.... no dice. Any takers? Quote Thanks for any assistance provided and take care, Hdokes
*Experts* mutant Posted June 30, 2003 *Experts* Posted June 30, 2003 Refer to my post in that thread :) : http://www.xtremedotnettalk.com/showthread.php?s=&threadid=73157 Quote
HDokes Posted June 30, 2003 Author Posted June 30, 2003 Hi Mutant... Went to your link.... and implemented your solution.... I am getting the following error: C:\Sound Effects System\SoundFX\Main.vb(2698): Argument not specified for parameter 'mainform' of 'Public Sub New(mainform As Main)'. This comes as a result of the following code being called which creates the instance of the 2nd form: Dim frmButtonManagement1 As Form frmButtonManagement1 = New frmButtonManagement frmButtonManagement1.Show() I tried entering the argument to the New frmButtonManagement line however that didn't work... again... the above is being called from the first (the primary) main form. I entered your fix into the 2nd form as your link implied... Quote Thanks for any assistance provided and take care, Hdokes
HDokes Posted June 30, 2003 Author Posted June 30, 2003 Ok... nearly time to go find a clock tower to climb. I am an old school programmer... pretty darn proficient... and efficient if I do say.... Assembler, C, VB6 and older (all the way to BASIC interpretive), PLC Ladder Logic,... you get the jist.... Why is this so dern difficult to absorb regarding .NET? Ok... so I learned that .NET no longer allows you to do ANYTHING you once did in every ealier version of vb.... including the fact that instances are not automatically created upon the execution of a class... and that public variables REALLY aren't public.... so here's what I have tried to do to get around this confusion.... I created a public shared subroutine in the Main form, called "main", (again... this is the primary form initiated upon startup). in the arguments of the share sub I provide the ability to pass a variable by value called from form2 and then assign the variable to a control located on form1 (Main). Once I used the 'shared' directive I was promplty told I could no longer refer to the local control in the form of "me.localcontrol"..... the name of the main form..... is main!... you'd think I could use main.localcontrol... but NOOOOOOOOO (<---- he says in his best John Belushi imitatoin) What's a guy to do? ...... I figure it out.... climb the clock tower and start shootin!!! :) Ok... jest aside, Here is the sub within main being called... and then the call from form2... Public Shared Sub Button1tag(ByVal strPathName As String) Main.Button1.Tag = strPathName End Sub error returned: Expression does not produce a value. call from form 2 Private Sub btnAssignTemplate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssignTemplate.Click Main.Button1tag(grdButton_Assignment_Table.Item(grdButton_Assignment_Table.CurrentCell.RowNumber, 2)) End Sub Now.... I'm not saying that I'm heading up the clock tower right this minute..... nope... not saying it.... but I am dang sure thinking it!!! (not to worry... the only clock tower within walking distance around here is a 6 foot grandfather clock in my livingroom... hope the kids don't come around) Quote Thanks for any assistance provided and take care, Hdokes
Leaders dynamic_sysop Posted June 30, 2003 Leaders Posted June 30, 2003 here's a quick example : in form2 ( the form you want to send information from, to form1 ) Public Class Form2 Inherits System.Windows.Forms.Form Public frm As Form1 '/// first make a public reference to a new form #Region " Windows Form Designer generated code " Public Sub New(ByVal myForm As Form) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() frm = myForm '/// initialize the form 'Add any initialization after the InitializeComponent() call 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 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(136, 104) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Form2 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1}) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm.TextBox1.Text = "i am some text from form2 to form1's textbox!" End Sub End Class in Form1 ( the form you want to start up with ) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frmTwo As New Form2(Me) frmTwo.Show() End Sub Quote
HDokes Posted June 30, 2003 Author Posted June 30, 2003 Ok... I've doen the above... and the instance gets passed, however, the controls do not show up in the instances i.e. frm properties or listing. I can see the reference to the instance (frm) as the main form but no button controls are any other controls are showing up in the sub procedure in form 2 when making the call... Form1 (main form) entries: Dim frmButtonManagement1 As Form frmButtonManagement1 = New frmButtonManagement(Me) frmButtonManagement1.Show() Form2 entries: Public Class frmButtonManagement Inherits System.Windows.Forms.Form Public formmain As Form #Region " Windows Form Designer generated code " Public Sub New(ByVal mainform As Main) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() formmain = mainform 'Add any initialization after the InitializeComponent() call End Sub Private Sub btnAssignTemplate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAssignTemplate.Click formmain.button1.tag = (grdButton_Assignment_Table.Item(grdButton_Assignment_Table.CurrentCell.RowNumber, 2)) End Sub I get an error that indicates "button1" is not a member of the "System.Windows.Forms.Form" Quote Thanks for any assistance provided and take care, Hdokes
*Experts* mutant Posted June 30, 2003 *Experts* Posted June 30, 2003 Change that: Public formmain As Form To: Public formmain As Main 'or whatever your form is called You only declared it as a form, so it wont have what your form class has. Quote
HDokes Posted June 30, 2003 Author Posted June 30, 2003 My sincerest appreciations..... That took care of it.... and it was something that I should have seen. Sometimes one can't see the characters for the paragraphs! Thanks a bunch! Quote Thanks for any assistance provided and take care, Hdokes
yan19454 Posted July 2, 2003 Posted July 2, 2003 I tried that. It gave me error in this sentence Dim frmTwo As New Form2(Me)-Too many argument to public sub New. While I took off me in the above sentence, while I click the button1 on frm2, it gave me the error msg about "Object reference not set to an instance of object. Here is the code: Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public frm As Form1 '/// first make a public reference to a new form Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call 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 Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(72, 80) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 56) Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Form2 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1}) Me.Name = "Form2" Me.Text = "Form2" Me.ResumeLayout(False) End Sub #End Region Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub 'Friend WithEvents Button1 As System.Windows.Forms.Button Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm.TextBox1.Text = "i am some text from form2 to form1's textbox!" End Sub Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm.TextBox1.Text = "i am some text from form2 to form1's textbox!" End Sub End Class 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 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 TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(104, 56) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(136, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "TextBox1" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(72, 144) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(144, 88) Me.Button1.TabIndex = 1 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Thanks.:( Quote
Leaders dynamic_sysop Posted July 2, 2003 Leaders Posted July 2, 2003 try this : accessing controls on another form in vb.net and btw that code needs changing to run it that way : Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public frm As Form1 '/// first make a public reference to a new form Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() needs to be something like this : Public Class Form2 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public frm As Form1 '/// first make a public reference to a new form Public Sub New(ByVal newFrm As Form)'/// <<<<<< this MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() frm = newFrm'/// <<<< and this line. they should be in form2's code , then in form1 Dim MyForm As New Form2(Me) MyForm.Show() Quote
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.