I want to close all forms but this code doesnt work. Could someone give me the right code?
private Form0 _form0 = null;
public Form1(Form0 form0)
{
InitializeComponent();
form0.Hide();
_form0 = form0;
}
form0 show a demo when user press the entrance button form1 will be shown.
private void ToolStripMenuItem7_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1(form0);
form1.Dispose();
form1.Close();
}
private void ToolStripMenuItem7_Click(object sender, EventArgs e)
{
using(Form1 form1 = new Form1(form0))
{
form1.Show();
}
}
Application.Exit(); will close all forms and entire application as well.
Related
I am creating a quiz for my As level coursework on visual studio 2019(c#). In this I will be creating a help button that will have information that the user may need if they are stuck. The button to access the help form will be avaliable through a menu strip bar loacted in the top corner of every form. In the help form there will be a menu strip bar with a back button. I would like to know how to code a back button to go back to the previous form eg Question 1-10 forms or the login form. I know how to code it if i wanted to back to a specific form but it is the fact it may need to back to any form as i dont know which form the user will have previously been on.
If you want to code a back button to go back to the previous form, you can refer to the following code:
Code in Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form2 newform = new Form2();
newform.ShowDialog();
this.Show();
}
}
Code in Form2.cs :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form3 newform = new Form3();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Code in Form3.cs :
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void btnForward_Click(object sender, EventArgs e)
{
this.Hide();
Form4 newform = new Form4();
newform.ShowDialog();
this.Show();
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
If you want to open other forms, just change this code ‘Form2 newform = new Form2();’.
Commonly, one would store the questions in a list or array. If you also store the index of the current question, then you could use that to return to the correct question.
The button should navigate between different windows:
STEP 1 - add a new button on your current window.
STEP 2 - double click on that button to access cs file:
private void Button_Click(object sender, RoutedEventArgs e)
{
}
STEP 3 - create a new object window (to navigate to) and open that, then close current window
private void Button_Click(object sender, RoutedEventArgs e)
{
NewWindow page2= new NewWindow();
page2.Show();
this.Close();
}
You should be able to move between different pages back and forth
I have a problem concerning c# and Windows Forms.
I am developing an application which needs more than 10 forms.
I started to close forms by clicking buttons and start new forms. It works quite well.
But now I am in form no. 5. I can't link any button with this new form...
In earlier forms, it worked??
Is there a limit on a number of forms?
Thanks for your help!
Greetings from Germany
Code: here it works
namespace WindowsFormsApplication1
{
public partial class Form3: Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form4 f4 = new Form4();
f4.ShowDialog();
}
}
}
Code: here it isn't working
namespace WindowsFormsApplication1
{
public partial class Form4: Form
{
public Form4()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form3 f3 = new Form3();
f3.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
Form5 f5 = new Form5();
f5.ShowDialog();
}
}
}
This is due to the fact that you're using Hide() instead of Close().
If you use the Hide() function, it just sets the form's property Visibility.Hidden, which means that the Form is still running on the background.
Now, when you call the function from Form4 to "Close" and open Form3, you're just hidding Form4, if you want to call Form4 back, you're gonne create two Form4 (of the same Type), since it's still in the background.
One way I like to do that is to check if the form is open before creating a new one:
private void button1_Click(object sender, EventArgs e)
{
Close();
var app = Application.OpenForms["Form3"];
if (app == null)
{
Form3 f3 = new Form3();
f3.Show();
}
else { app.Show(); app.BringToFront(); }
}
Or you can just call a new Form, but make sure you Close the form.
private void button1_Click(object sender, EventArgs e)
{
Close();
Form3 f3 = new Form3();
f3.Show();
}
Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.
One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():
private void PlayButton_Click(object sender, EventArgs e)
{
this.Visible = false; // hide the current form
Application.DoEvents();
PongForm form = new PongForm();
form.ShowDialog(); // code stops here until PongForm is dismissed
this.Close(); // now close the form (and presumable the whole app)
}
I have 2 forms form1&form2. Form1 contains a serial port. This port is opened in form load.
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
}
I want to write to serial port through form2. For that I created a function in form1 for writing to serial port.
public void SerialPortValueUpdated()
{
byte[] head = new byte[1] { 0xAA };
byte[] trail = new byte[1] { 0x55 };
byte[] llen = new byte[1] { length };
// head = Convert.ToByte(0xAA);
//serialPort1.Open();
serialPort1.Write(head, 0, 1);
serialPort1.Write(llen, 0, 1);
serialPort1.Write(trail, 0, 1);
}
and called this function from form2 like this
private void button1_Click(object sender, EventArgs e)
{
Form1 F = new Form1();
F.SerialPortValueUpdated();
}
But when I calling this function I get an error that 'Port is closed'. How can I solve this.
Please help me.
As was stated in the other answer you are creating a new Form1 in your Buttons click event instead of using the Form that you have already opened the serial port with. In my systems I use a separate class to encapsulate the communication logic as was mentioned in the other answer. But since this seems to be a simple application and your existing Form1 as knowledge of your Form2 since it created it, just pass form1 as the Owner of Form2, that way you can access the function you are wanting.
i.e.
f2 = new Form2();
f2.Show(this); // or f2.ShowDialog(this) if you want f2 to be Modal
You would then access it in your Form2's Button Click something like this.
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).SerialPortValueUpdated();
}
Form1
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
f2 = new Form2();
f2.ShowDialog(this); // or f2.Show(this) if you want f2 to be non Modal
}
public void SerialPortValueUpdated()
{
MessageBox.Show("Test");
}
}
Form2
public partial class Form2 : Form
{
Form3 f3;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).SerialPortValueUpdated();
}
private void button2_Click(object sender, EventArgs e)
{
f3 = new Form3();
f3.ShowDialog(this.Owner);
}
}
Form3
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
((Form1)this.Owner).SerialPortValueUpdated();
}
}
the problem is that creating a new Form1 will never open your port since the Load event will be called when the form get's loaded, and just newing up a form will not load or show it.
The easiest way to change this is to move the opening into the constructor.
So instead of
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
}
do
public Form1()
{
serialPort1.Open();
}
The better approach
Having said that - using a Form just to do this kind of stuff is really not a solution. Instead use a simple class to do this and make it the responsibility of the caller to manage opening and closing (or if you really just want to write a few bytes then open and close it in your methods).
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