Block Form1 until Form2 closes c# - c#

In my code i from form1 launch form2 using .Show() .
Form2 f2 = new Form2();
f2.show();
block form1 until form2 close and when form2 closed continue my code.

.Show() will show the new form you are displaying but it will enable you to go back and use the controls in the Main Form and .ShowDialog() wont allow you to access your main form unless its closed.
f2.ShowDialog();

Try using
Form2 f2 = new Form2();
f2.showDialog();

I tested that ways and don't worked but only below code worked:
private void button1_Click_1(object sender, EventArgs e)
{
Form2 frm = new Form2();
this.Enabled = false;
frm.Show();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
private void frm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Enabled = true;
}

Related

How to close a Form embedded in a panel

So I have 4 Forms. Inside Form1 there are 2 Panels and a button. When I click the button1 on Form1, it opens the Form2 on the first panel. So inside the Form2 there are 2 buttons. The button1_Click method shows the Form3 on the second panel of Form1. How can I show the Form4
on the second panel by the method button2_Click from Form2?
private void button2_Click(object sender, EventArgs e)
{
Form4 myForm = new Form4();
myForm.TopLevel = false;
myForm.AutoScroll = true;
var Form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "Form1").FirstOrDefault();
Form1.panel2.Controls.Add(myForm);
myForm.Show();
}`
This is the code I used to try showing Form4 on the second panel. But it wont replace Form3 that is currently appearing on the second panel of Form1. I really don't know what to do. Please help. Thanks in advance.
So from form 2 you want to be able to load form3 OR form4 inside panel2 ?
private Form3 form3 = null;
private Form4 form4 = null;
private void button2_Click(object sender, EventArgs e)
{
if (form3 != null)
{
Form1.panel2.Controls.Remove(form3);
form3.Close();
form3 = null;
}
form4 = new Form4();
form4.TopLevel = false;
form4.AutoScroll = true;
var Form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "Form1").FirstOrDefault();
Form1.panel2.Controls.Add(form4);
form4.Show();
}`
and do the same for Form3 (remove and close form4 before creating form3)
Also consider setting your form3 and form4 properties as HansPassant suggested in his comment.

Hide Form1, show Form2 on Form1_Load

The Form1 of my App is a login page that i want to:
- show on some conditions
- hide and show Form2 on some conditions
I can hide/show a form by the button click event like so,
private void button1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Hide();
}
but the same technique does not work for Form1_Load.
I have tried the first example in this thread,
Program.cs
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run();
}
Form1
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Hide();
}
but it's not showing neither Form1 or Form2, and i don't see how it could. The second example i can't understand how i can implement, and the next google results are even more confusing.
Please help i'm stuck on this for 2 hours.
In the last line in program.cs you must type new Form1() between the parenthesis. So, your program.cs code is as follow:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
C# can not hide form in form_load evant Apparently.
To resolve Hide problem, you can use of a timer and hide the form in tick event. i.e.:
Timer timer = new Timer();
private void timerTick(object sender, EventArgs e)
{
timer.Enabled = false;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
timer.Tick += new EventHandler(timerTick);
timer.Interval = 10;
Form2 frm = new Form2();
frm.Show();
timer.Enabled = true;
}
This works. I tested it.
I hope this will be useful.
Hello You Can Use This
private void button1_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
if(condition==true)
{
this.Hide();
f2.ShowDialog();
this.Close();
}
}
Why don't you reverse the order of your forms? Start with the main form in the main method.
Application.Run(new Form2());
Now in the constructor of Form2 call the login form with ShowDialog and set the result of the login in a global variable inside the Form2
public class Form2:Form
{
private bool _isValidated = false;
public Form2()
{
InitializeComponent();
// Add here the conditions to check if you don't want to
// run the login process...
// if(loginNotRequired)
// _isValidated = true;
// else
using(Form1 fLogin = new Form1())
{
// This blocks until the user clicks cancel or ok buttons
DialogResult dr = fLogin.ShowDialog();
if(dr == DialogResult.OK)
_isValidated = true;
}
}
Now in the Form2.Load event check the status of your login and close the Form2 if the login is not successful
private void Form2_Load(object sender, EventArgs args)
{
if(!_isValidated)
this.Close();
else
.....
}

Create new form on same screen position

I want to create a new form on the same location. When I call this code a new form opens but on a different screen position.
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.Location = this.Location;
form2.Show(this);
Hide();
}
I used this.Location to get the location from my first form but this has no effect.
You need to set StartPosition property to Manual for this to work.
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Tag = this;
form2.StartPosition = FormStartPosition.Manual;
form2.Location = this.Location;
form2.Show(this);
Hide();
}
Use this. Hope helps
private void BtnAddForm_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show(this);
form2.Tag = this;
form2.Location = this.Location;
Hide();
}
Use form2.StartPosition = FormStartPosition.Manual;, You should also use form2.Closed += (s, args) => this.Close(); to close parent form after hide it Try this:
Hide();
Form2 form2 = new Form2();
form2.Tag = this;
form2.StartPosition = FormStartPosition.Manual;
form2.Location = this.Location;
form2.Closed += (s, args) => this.Close();
form2.Show();
The easiest way is by using the the StartPosition property of the form. This property should be set before the form is shown. You can set this property before you call the Show or ShowDialog method or in your form's constructor.
For example:
form2.StartPosition = FormStartPosition.CenterParent
private void button5_Click(object sender, EventArgs e)
{
Form1 NewForm = new Form1();
NewForm.Show();
NewForm.Location = this.Location;
this.Dispose(false);
}
This is a pretty simple one.

Closing three forms form button

I have the following code:
in form1
private void button6_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form5 = new Form5();
Form Form6 = new Form6();
Form4.Show();
Form5.Show();
Form6.Show();
}
in form5 i have a button that must close form4, form5 and form6. as following:
private void button2_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form6 = new Form6();
Form4.Close();
Form6.Close();
this.Close();
}
but Form4 and Form6 are still open!!!
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = (Form4) Application.OpenForms["Form4"];
Form5 form5 = (Form5) Application.OpenForms["Form5"];
form4.Close();
form5.Close();
}
To close in proper way you have to know what do you want to close. In your case you are creating new Forms on beggining, and another ones with the same names visible locally before trying to destroy them.
Form Form6 = new Form6(); are totally different in both of your clicked buttons. Then, if you want to make it more visible generate interesting forms in constructor method and put definition as class fields like.
public partial class Form1 : Form
{
Form form2; // be sure all componentes see all forms
Form form3;
Form form4;
public Form1()
{
InitializeComponent();
form2 = new Form(); // create new Forms
form3 = new Form();
var button = new Button();
button.Click += new EventHandler(button_Click); // tell the button what should be called when click
form4 = new Form();
form4.Controls.Add(button); // add button progrimicaly to form
}
void button_Click(object sender, EventArgs e)
{
form2.Hide(); // hide on click
form3.Hide();
form4.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
form2.Show(); // show on load
form3.Show();
form4.Show();
}
}

resize label text in Windows application

how to resize lable text form2 at run time in C# windows application?.. for example if click button in form1 to open form2 then how to change form2 label text at run time...normaly form2 label text "saran" but if you can click form1 button1 click automatically change to "SARAN"
Example code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
You will either have to make the modifier public of the Textbox on form2 or you will have to create a method in form2 that will change the text on the form.
Something like
Example 1: textbox modifier is public
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.myTextBox.Text = "TADA";
form2.Show();
}
Example 2: public method on form2
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.SetMyTextBox("TADA");
form2.Show();
}

Categories

Resources