From Form1 I've been opening Form2. If I then click on a button or whatever in Form1, I want Form2 to be activated. Something like
Form2.Activate();
But that just gives me errors.
This is my code right now:
private void button1_Click(object sender, EventArgs e) // first I click here
{
Form2 f2 = new Form2();
f2.Show();
}
private void button2_Click(object sender, EventArgs e) // then here, to activate it
{
Form2 f2 = new Form2();
f2.Activate();
}
You're having that error because Activate method should be called from an instance of the Form2 class not the Form2 class its self, Activate() is not a Static method, You have to instantiate the Form2 class first, this event handler is for a button click on the first Form
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
this was to show the form, to activate it and make it the main one showed to you, you can than call the Activate() method to that instance, like
form2.Show();
form2.Activate();
I believe you need to create an instance of a class in order to access instance methods. Basically, the instance is created via a constructor call, like this:
Form2 form = new Form2();
However, the method to show newly created form is this one:
form2.Show();
Move the Form2 reference out to Class level so it can be accessed from both button1 and button2.
Something like...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 f2 = null;
private void button1_Click(object sender, EventArgs e) // first I click here
{
if (f2 == null || f2.IsDisposed)
{
f2 = new Form2();
f2.Show();
}
else
{
ActivateForm2();
}
}
private void button2_Click(object sender, EventArgs e) // then here, to activate it
{
ActivateForm2();
}
private void ActivateForm2()
{
if (f2 != null && !f2.IsDisposed)
{
if (f2.WindowState == FormWindowState.Minimized)
{
f2.WindowState = FormWindowState.Normal;
}
f2.Activate();
}
}
}
Related
I am very new to C# so I am sorry if I use wrong terms. Here is my problem. I have 2 forms. and I want If I click the button in Form1, it shows up the Form2 by overlapping the Form1. And now, if I click the button in form2, I want the form1 overlap the form2. But I always get error "stackOverFlowException". Why did i get the error? How to resolve it? I am sorry if my question is not clear. I hope these picture can explain better about my question.
Here is the code for form1.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
frm2.Show();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
frm2.Left += 10;
if (frm2.Left >= 750)
{
timer1.Stop();
this.TopMost = false;
frm2.TopMost = true;
timer2.Start();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
frm2.Left -= 10;
if (frm2.Left <= 535)
{
timer2.Stop();
}
}
}
}
and here is the code for form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 frm1 = new Form1();
private void Form2_Load(object sender, EventArgs e)
{
frm1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
frm1.Left += 10;
if (frm1.Left >= 750)
{
timer1.Stop();
this.TopMost = false;
frm1.TopMost = true;
timer2.Start();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
frm1.Left -= 10;
if (frm1.Left <= 535)
{
timer2.Stop();
}
You are creating and endless chain of opened forms.
When you first create a Form1 object with new Form1(), the initialization code inside this form
Form2 frm2 = new Form2();
... is called. This in turn triggers the initialization code of Form2, which is
Form1 frm1 = new Form1();
This creates a new instance of this form (now you have two Form1 instances) and calls Form2 frm2 = new Form2(); again in this new instance (now you have two Form2 instances). This goes on until thousands of form objects have been created and the stack overflows.
What can you do about it?
In Form2 add a parameter to the constructor that allows you to pass a reference to the first form
private readonly Form1 _frm1;
public Form2 (Form1 frm1)
{
InitializeComponent();
_frm1 = frm1;
}
In Form1 do this
private Form2 _frm2;
private void Form1_Load(object sender, EventArgs e)
(
_frm2 = new Form2(this);
)
I.e. Form2 never creates a Form1. Instead, it gets a reference to the already opened form.
How do you bring a form to front?
The TopMost property controls the behavior of a form when it is opened. Instead, use
_frm1.BringToFront();
to change the z-order afterwards.
I'm having trouble trying to open a second form in Visual C# from a menu stript.
I try with the line Form2.Show(); but it doesn't work.
namespace Noggy_Shield
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buscarActualizacionesToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://ultscargot.blogspot.com.br/p/noggy-shield.html");
}
private void acercaDeToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2.Show(); // This appears underlined in red
}
private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
System.Diagnostics.Process.Start("https://sitecheck.sucuri.net/results/" + textBox1.Text);
}
else
{
System.Diagnostics.Process.Start("http://www.urlvoid.com/scan/" + textBox1.Text);
}
}
}
}
This is attempting to call .Show() as a static method:
Form2.Show();
But it's an instance method. First you create an instance of the object, then show that instance. Something like this:
var form2 = new Form2();
form2.Show();
Note that another common mistake is to attempt to interact with other separate instances later on. Each instance is distinct. So if you show one instance, then at a later time create another instance to try to get values from it, that won't work. You'll need to interact with the form2 instance created and shown.
Im late but to open another form you can use this code:
Form2 form2 = new Form2(); //you can replace the form2 (not the Form2) with anything you want
form2.ShowDialog();
I have 2 form and I want change Form1.Text when its run with a button on
Form2 !!!! I dont whant make a instance Form1.
Thanks friends**
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog();
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
Form1
}
}
In this particular case (you need a reference to the Owner form) there is a simple shorcut that doesn't require to keep a local instance of the first form
Just pass the instance of Form1 as the Owner in the ShowDialog call
private void button1_Click(object sender, EventArgs e)
{
Form2 F2 = new Form2();
F2.ShowDialog(this);
}
Now you could reference the property Owner inside the Form2 code
private void ChangeForm1Text_Click(object sender, EventArgs e)
{
this.Owner.Text = "your new caption for form1";
}
One possible way for you to do this is to pass the instance of Form1 into a constructor on Form2`. So add this constructor and field to Form2:
private readonly Form1 _parentForm1;
public Form2(Form1 parentForm1) : this()
{
_parentForm1 = parentForm1;
}
Now when you create your Form2 in the button click, create it like this:
Form2 F2 = new Form2(this);
F2.ShowDialog();
Then you can use _parentForm1 anywhere in your instance of Form2 to refer back to the other form.
At first I thought that it won't be a problem for me, but now I can't figure it out. So,
when I click Button1 in main form, form2 opens. Form2 is simple numeric keyboard, that user can enter some data. On form2 is also Save. When user clicks it, entered value should pass to main form and from that moment some event must happen in main form, which contains data from form2. Could you please give me some example or any kind of help? Thanks!
// code from main form to create form2
private void button1_Click(object sender, EventArgs e)
{
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
//button save on form2
private void button13_Click(object sender, EventArgs e)
{
this.ReturnValue1 = "Something";
this.ReturnValue2 = DateTime.Now.ToString(); //example
this.Close();
//after this, some event should happen in main form !
}
There is a lot of solutions to do what you want; but I think one of these will resolve your problem.
1- Simple and easy: use public properties in Form2, initialize them when buttonSave get clicked, and access them in Form1:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
YourDate = "something";
Close();
}
public object YourDate { get; private set; }
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
var f2 = new Form2();
f2.ShowDialog();
var data = f2.YourDate;
}
}
2- A better way, is using events which is more flexible and professional programming friendly:
Form2:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
}
// create an event of Action<T> which T is your data-type. e.g. in this example I use an object.
public event Action<object> SaveClicked;
// create an event invocator, to invoke event whenever you want
protected virtual void OnSaveClicked(object data){
var handler = SaveClicked;
if (handler != null)
handler(data);
}
private void button1_Click(object sender, EventArgs e){
// prepare your data here, -object, or string, or int, or whatever it is
var data = PrepareYourDataHere;
// invoke the event
OnSaveClicked(data);
Close();
}
}
Form1:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
// create an instance of Form2
var f2 = new Form2();
// add an event listener to SaveClicked event -which we have declared it in Form2
f2.SaveClicked += f2_SaveClicked;
f2.Show();
// or: f2.ShowDialog();
}
void f2_SaveClicked(object obj) {
// get data and use it here...
// any data which you pass in Form2.OnSaveClicked method, will be accessible here
}
}
UPDATE:
If you want to fire some events in form1, just after form2 closed, you can simply add a listener to Form2.FormClosed event:
// code from main form to create form2
private void button1_Click(object sender, EventArgs e) {
// Create a new instance of the Form2 class
Form2 settingsForm = new Form2();
settingsForm.FormClosed += SettingFormClosed;
// Show the settings form
settingsForm.Show();
string val = settingsForm.ReturnValue1;
MessageBox.Show(val);
}
void SettingFormClosed(object sender, FormClosedEventArgs e) {
// this method will be called automatically when form2 closed
}
here a sample how you can achieve this
//here I suppose that form1 is the mainform
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void UpdateMainForm(string updatedString)
{
//here you can update and invoke methods
//Once called you could raise events in your mainform
}
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2(this))
{
form2.ShowDialog();
}
}
}
Form2
public partial class Form2 : Form
{
private Form1 _mainForm1;
public Form2(Form1 mainForm1)
{
InitializeComponent();
_mainForm1 = mainForm1;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm1.UpdateMainForm( DateTime.Now.ToString());
}
}
I have a form that contains a Shockwave Flash object. I want that after playing the file or in the middle of it by pressing a key another form is shown. What should I do? Should I use an event?
Here's the code:
private void Form1_Load(object sender, EventArgs e)
{
axShockwaveFlash1.Movie = "c:/intro.swf";
axShockwaveFlash1.Forward();
axShockwaveFlash1.Play();
Form2 form2 = new Form2();
form2.Show();
}
And in Form2:
private void Form2_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Close();
}
When I debug both forms are shown but I just want Form2.
You can pass the Form1 instance to the Form2 constructor, and close it there, like:
public Form2(Form1 form1)
{
InitializeComponents();
form1.Hide();//hide the control from the user, or close it if it not the main form..
}
Or if you want it to close the form1 instance when a button click then:
private Form1 _form1 = null;
public Form2(Form1 form1)
{
InitializeComponents();
_form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
if (_form1 != null)
{
_form1.Hide();
}
}
Initialize the form2 in form1:
axShockwaveFlash1.Movie = "c:/intro.swf";
axShockwaveFlash1.Forward();
axShockwaveFlash1.Play();
Form2 form2 = new Form2(this);//"this here is the form1 instance itself"
form2.Show();
you can also use timer to check weather the movie is playing or not,if not then open new form which is form2
private void flash_Load(object sender, EventArgs e)
{
axShockwaveFlash1.Movie = #"C:\Users\Adil M\Documents\Visual Studio 2012\Projects\TProject\a.swf";
int a = axShockwaveFlash1.MovieData.Length;
timer1.Start();
}
now code on timer event
private void timer1_Tick(object sender, EventArgs e)
{
if (!axShockwaveFlash1.IsPlaying())
{
Fome2 fobj = new Form2();
this.Hide();
timer1.stop();
fobj.show();
}
else if(axShockwaveFlash1.IsPlaying())
{
}
}
this code is working for you only when your movie is complete or for any key you can also do it in timer
thanx