I'd like to be able to set the title and the button name for a form from another form when executed and receive button response back to first form. Might wish to get other parms back etc.
SEE SOLUTION IN LAST OF THREAD.
Using VC#2005 Express,
Thanks,
RON C
Ronald ChambersPosted Dec 21, 2007, 7:16 PM
TEST CODE to DEMONSTRATE PASSING INFO BETWEEN FORMS IN VS C#2005.
RON C
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace test_form
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main ( )
{
Application.EnableVisualStyles ( );
Application.SetCompatibleTextRenderingDefault ( false );
Application.Run ( new Form1 ( ) );
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_form
{
public partial class Form1 : Form
{
public Form1 ( )
{
InitializeComponent ( );
}
}
}
Form1.Designer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_form
{
partial class Form1 : System.Windows.Forms.Form
{
public static Form1 staticVar = null;
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose ( bool disposing )
{
if (disposing && ( components != null ))
{
components.Dispose ( );
}
base.Dispose ( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
///
private void InitializeComponent ( )
{
this.form1button1 = new System.Windows.Forms.Button ( );
this.SuspendLayout ( );
//
// form1button1
//
this.form1button1.AccessibleRole = System.Windows.Forms.AccessibleRole.Dialog;
this.form1button1.Anchor = ( ( System.Windows.Forms.AnchorStyles )( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
| System.Windows.Forms.AnchorStyles.Left )
| System.Windows.Forms.AnchorStyles.Right ) ) );
this.form1button1.DialogResult = System.Windows.Forms.DialogResult.Yes;
this.form1button1.Location = new System.Drawing.Point ( 105, 217 );
this.form1button1.Name = "form1button1";
this.form1button1.Size = new System.Drawing.Size ( 95, 23 );
this.form1button1.TabIndex = 1;
this.form1button1.Text = "form1button1";
this.form1button1.UseVisualStyleBackColor = true;
this.form1button1.Click += new System.EventHandler ( this.form1button1_Click );
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF ( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size ( 292, 271 );
this.Controls.Add ( this.form1button1 );
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "From1";
this.ResumeLayout ( false );
}
#endregion
//
// form1button1
//
private void form1button1_Click ( object sender, System.EventArgs e )
{
Form2 form2 = new Form2 ( );
form2.Text = "TEXT CHANGED";
form2.form2button1.Text = "New button text";
//form2.form2button1.Click += form2button1_Click;
//DialogResult dr =
form2.ShowDialog ( );
}
public System.Windows.Forms.Button form1button1;
}
}
Form2.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_form
{
public partial class Form2 : Form
{
public Form2 ( )
{
InitializeComponent ( );
}
}
}
Form2.Designer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace test_form
{
partial class Form2 : System.Windows.Forms.Form
{
public static Form2 staticVar = null;
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose ( bool disposing )
{
if (disposing && ( components != null ))
{
components.Dispose ( );
}
base.Dispose ( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
///
private void InitializeComponent ( )
{
this.form2button1 = new System.Windows.Forms.Button ( );
this.SuspendLayout ( );
//
// form2button1
//
this.form2button1.AccessibleRole = System.Windows.Forms.AccessibleRole.Dialog;
this.form2button1.Anchor = ( ( System.Windows.Forms.AnchorStyles )( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
| System.Windows.Forms.AnchorStyles.Left )
| System.Windows.Forms.AnchorStyles.Right ) ) );
this.form2button1.DialogResult = System.Windows.Forms.DialogResult.Yes;
this.form2button1.Location = new System.Drawing.Point ( 105, 217 );
this.form2button1.Name = "form2button1";
this.form2button1.Size = new System.Drawing.Size ( 95, 23 );
this.form2button1.TabIndex = 1;
this.form2button1.Text = "form2button1";
this.form2button1.UseVisualStyleBackColor = true;
this.form2button1.Click += new System.EventHandler ( this.form2button1_Click );
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF ( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size ( 292, 271 );
this.Controls.Add ( this.form2button1 );
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Form2";
this.ResumeLayout ( false );
}
#endregion
//
// form2button1
//
private void form2button1_Click ( object sender, System.EventArgs e )
{
// Display a message box.
MessageBox.Show ( "Test of buttons and forms. ", "test_form" );
}
public System.Windows.Forms.Button form2button1;
}
}
Ronald ChambersPosted Dec 21, 2007, 3:56 PM
I'm going to upload a small test program I did in a bit that shows it working.
You got me through it. Thanks,
RON C
AlanPosted Dec 21, 2007, 2:51 PM
Yes, it's all making sense now, Ron.
I was assuming with my 'Application.OpenForms' code that Form3 had already been created and you were trying to get a reference it. However, it hadn't in fact been created and so when you constructed it, the code worked fine :)
Ronald ChambersPosted Dec 21, 2007, 12:39 PM
Form3
form3 = new Form3 ( );Thanks for help.
RON C
Ronald ChambersPosted Dec 20, 2007, 7:50 PM
Thanks,
RON C
AlanPosted Dec 20, 2007, 7:19 PM
You don't need the following line because Text is an existing public property which all Forms have which contains the Form's caption:
public System.Windows.Forms.Form Text; //delete this
There may be other problems but I'd see what happens when you rebuild after removing that line.
Ronald ChambersPosted Dec 20, 2007, 5:42 PM
This doesn't compile now with the public System.Windows.Forms.Form Text; I inserted at bottom trying to fix.
I really appreciate your help.
RON C
FORM1:
private void aboutToolStripMenuItem_Click ( object sender, System.EventArgs e )
{
Form3 form3 = ( Form3 )Application.OpenForms [ "Form3" ];
form3.Text = "A new title";
form3.form3label1.Text = "New label text";
form3.form3button1.Text = "New button text";
//form3.form3button1.Click += form3_button1_Click;
//DialogResult dr =
form3.ShowDialog ( );
}
//
// add eventhandler to Form1
//
private void form3_button1_Click ( object sender, System.EventArgs e )
{
// do something when form3button1 on Form3 is clicked
}
FORM3:
using System;
using System.Collections;
using System.Windows.Forms;
namespace text1
{
partial class Form3 : System.Windows.Forms.Form
{
public static Form3 staticVar = null;
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose ( bool disposing )
{
if (disposing && ( components != null ))
{
components.Dispose ( );
}
base.Dispose ( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
///
private void InitializeComponent ( )
{
this.form3label1 = new System.Windows.Forms.Label ( );
this.form3button1 = new System.Windows.Forms.Button ( );
this.SuspendLayout ( );
//
// form3label1
//
this.form3label1.Anchor = ( ( System.Windows.Forms.AnchorStyles )( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
| System.Windows.Forms.AnchorStyles.Left )
| System.Windows.Forms.AnchorStyles.Right ) ) );
this.form3label1.AutoSize = true;
this.form3label1.Location = new System.Drawing.Point ( 130, 117 );
this.form3label1.MinimumSize = new System.Drawing.Size ( 13, 13 );
this.form3label1.Name = "form3label1";
this.form3label1.Size = new System.Drawing.Size ( 13, 13 );
this.form3label1.TabIndex = 0;
//
// form3button1
//
this.form3button1.AccessibleRole = System.Windows.Forms.AccessibleRole.Dialog;
this.form3button1.Anchor = ( ( System.Windows.Forms.AnchorStyles )( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
| System.Windows.Forms.AnchorStyles.Left )
| System.Windows.Forms.AnchorStyles.Right ) ) );
this.form3button1.DialogResult = System.Windows.Forms.DialogResult.Yes;
this.form3button1.Location = new System.Drawing.Point ( 105, 217 );
this.form3button1.Name = "form3button1";
this.form3button1.Size = new System.Drawing.Size ( 75, 23 );
this.form3button1.TabIndex = 1;
this.form3button1.UseVisualStyleBackColor = true;
this.form3button1.Click += new System.EventHandler ( this.form3button1_Click );
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF ( 6F, 13F );
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size ( 292, 271 );
this.Controls.Add ( this.form3button1 );
this.Controls.Add ( this.form3label1 );
this.Name = "Form3";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.ResumeLayout ( false );
this.PerformLayout ( );
}
#endregion
//
// form3button1
//
private void form3button1_Click ( object sender, System.EventArgs e )
{
}
public System.Windows.Forms.Form Text;
public System.Windows.Forms.Label form3label1;
public System.Windows.Forms.Button form3button1;
}
}
AlanPosted Dec 19, 2007, 6:04 PM
Ron,
If you click on button1 whilst in the VS form designer, the Properties Box will open up and you'll see a Modifiers property. Just change that to 'public' and the code will be changed for you.
If the Application class still can't be found, just post the code on here and I'll take a look.
Ronald ChambersPosted Dec 17, 2007, 10:54 PM
I don't know where to look on your second comment.
I have the using statement in all routines.
Sent you an e-mial to see if we can make this easier.
Thanks,
RON C
AlanPosted Dec 17, 2007, 7:00 PM
Hi Ron,
I don't understand the first error as the compiler should be able to find the Application class as long as you've got: using System.Windows.Forms; in your source file.
The second error is probably due to the button1 field being declared as private - you need to change it to public.
Incidentally, you shouldn't need InvokeRequired here as everything should be taking place on the UI thread.
Ronald ChambersPosted Dec 17, 2007, 2:22 PM
Seemed easy but I have no experience with C#.
Error 1 The name 'Application' does not exist in the current context
Error 2 'text1.Form3.button1' is inaccessible due to its protection level
How I included:
private
void aboutToolStripMenuItem_Click ( object sender, System.EventArgs e ){
Form3 form3 = ( Form3 )Application.OpenForms[ "Form3" ];
form3.Text = "A new title";
form3.label1.Text.InvokeRequired = "New label text";
form3.button1.Text.InvokeRequired = "New button text";
form3.button1.Click.InvokeRequired += form3_Button1_Click;
form3.ShowDialog ( );
}
Thanks,
RON C
Ronald ChambersPosted Dec 16, 2007, 7:17 PM
Looks rather easy.
Thanks,
RON C
AlanPosted Dec 16, 2007, 7:12 PM
If Form1 and Form2 are the names of your forms and button1 is the name of a Button, declared as a public field, on Form2 then I'd try this code :
// within a method on Form1
Form2 frm2 = (Form2)Application.OpenForms["Form2"];
frm2.Text = "A new title";
frm2.button1.Text = "New button text";
frm2.button1.Click += frm2_Button1_Click;
// add eventhandler to Form1
private void frm2_Button1_Click(object sender, EventArgs e)
{
// do something when button1 on Form2 is clicked
}