Hide Form1, show Form2 on Form1_Load - c#

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
.....
}

Related

Opening new form over another form

I have main form which has several buttons, each button opens new form. When i click button1, form1 opens and when i click button2, form2 opens but form1 goes back to the main form. I want functionality such that each new form opens over the parent form and the most recent form on the top.
This is my code
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
}
Each form has a topmost property, just set them to true
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
form.TopMost = true;
form.Activate();
}

How can I refresh One form when the other form is closed?

I have been developing project in c#.
It has 2 form and these are connected with between each other.
I want to do that when second form is closed, first form refresh.
If I use Thread's Sleep program will be tired. I want to do this with closing events. How can I do ?(Like java's repaint)
Codes are below:
Form1
public static Form1 form;
public Form1()
{
InitializeComponent();
form = this;
}
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.Show();
}
Form2(Close Button)
private void button1_Click(object sender, EventArgs e)
{
Form1.form.Invalidate();
Form1.form.Refresh();
this.Close();
}
Bind Form_Closing event in your first form.
//Form1
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.FormClosing += new FormClosingEventHandler(this.Form2_FormClosing);
yeniform.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//Do your stuff here.
}
' button in the second form which is to be closed
' form1 is the form which is to be re loaded when form 2 is closed
private void btn_close_Click(object sender, EventArgs e)
form1.close() 'unload form 1 before closing form2
form1.show() ' form 1 reloading
unload(me) 'closing form2
end sub
This is a working sample. In parent form
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
VendorsAddForm f = new VendorsAddForm();
f.StartPosition = FormStartPosition.CenterScreen;
f.FormClosed += new FormClosedEventHandler(child_FormClosed);
f.Show();
}
void child_FormClosed(object sender, FormClosedEventArgs e)
{
this.Refresh();
var query = dbContext.AccountObjects.Where(p => p.IsVendor == true).ToList();
accountObjectsBindingSource.DataSource = new BindingList<AccountObject>(query);
}
Note: child form is VendorsAddForm
Thank to https://www.daniweb.com/posts/jump/1302760 , I learned from there.

ShowDialog issue while opening form

I have 2 forms
Form1
Form2
I have one button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.ShowDialog();
f2.Dispose();
}
but issue is while opening form it's bliking and diasparing
i have tried to use show() also but not solved the problem
If i have not used Disposed method then first time when run the form it appering and disappered but sencond time onward by clicking on button it's working fine...
In Form2_Load event i am using this two property
private void Form2_Load(object sender, EventArgs e)
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
Don't change the form layout while its loading. Change it before you launch. Remove the code from Form2_Load and put it in button1_Click:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.RightToLeft = RightToLeft.Yes;
f2.RightToLeftLayout = true;
f2.ShowDialog();
}
I would guess you want to show and close the form2 using the same button. And I doubt your initial problem description
"issue is while opening form it's bliking and diasparing"
I think form2 is not 'blinking' while opening, but is 'blinking' while you try to click the button again in form1
ShowDialog() will exit your execution after u called it. Mean, it will exit the execution after you click the button.
Thus, you should try Show() with conditional statement within the button click event
In form1.cs
bool flag = false;
Form2 frm2;
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
frm2 = new Form2();
frm2.Show();
frm2.Load += new EventHandler(frm2_Load);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
flag = true;
}
else
{
frm2.Close();
flag = false;
}
}
void frm2_Load(object sender, EventArgs e)
{
//set what ever properties you like
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
flag = false;
}
See also: A dialog disables all of the windows that your program displays
Remove this Property
this.RightToLeft = RightToLeft.Yes;
and run your form...
Try This :
private void button1_Click(object sender, EventArgs e)
{
using(Form2 f2 =new Form2())
{
f2.ShowDialog();
}
}

Automatically hide one form in C# after many second and show another form

I need to hide current form after many second and then show any form
I'm writing this code but it doesn't work.
namespace tempprj
{
public partial class ProfileFrm : Telerik.WinControls.UI.RadForm
{
public ProfileFrm()
{
InitializeComponent();
}
private void ProfileFrm_Load(object sender, EventArgs e)
{
Frm2 child = new Frm2();
Thread.Sleep(3000);
this.Hide();
child.ShowDialog();
}
}
}
Thread.Sleep(3000);
is going to prevent your project from doing anything at all for 3 seconds (not counting other threads) and freeze the UI. I suggest using the standard .NET timer.
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx
This is a solution to my question:
private void ProfileFrm_Load(object sender, EventArgs e)
{
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Enabled = true;
timer1.Interval = 4000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
this.Hide();
Frm2 f = new Frm2();
f.ShowDialog();
}

how to show login form on front & Mainform Behind Login Form?

In my winform application; I have login form and main form.
When I run program I want the login form on top and main form behind it.
One more thing is until I do not login properly with username and password, the main form should not be accessible and only the login form should accessible.
My Language is C#.Net.
Please provide idea on how to achieve this?
Use Form.ShowDialog (Shows the form as a modal dialog box) on the your Form.OnShown event (Occurs whenever the form is first displayed):
private void Form1_Load(object sender, EventArgs e)
{
this.Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
LoginForm loginForm = new LoginForm ();
if (loginForm.ShowDialog() == DialogResult.Ok)
{
....
}
}
your Program and LoginForm like this:
//Progrmm.cs
Application.Run(new Form1());
//LoginForm.cs
public partial class LoginForm : Form
{
public LoginForm ()
{
InitializeComponent();
}
private void buttonLogin_Click(object sender, EventArgs e)
{
//check username password
if(texboxUser == "user" && texboxPassword == "password")
{
DialogResult = DialogResult.OK;
Close();
}
else
{
MessageBox.Show("Wrong user pass");
}
}
}
I for one do not like the design you proposed, would want to first show the Login form, then the mainform. But if you absolutely need it, then you can do the below..
In Main class:
Application.Run(new frmMain());
And then in Form class:
private void frmMain_Load(object sender, EventArgs e)
{
//---------------------------------------------
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Tick +=new EventHandler(t_Tick);
t.Interval = 1000;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
frmLogin l = new frmLogin();
if (l.ShowDialog(this) == DialogResult.Ok)
((System.Windows.Forms.Timer)sender.Dispose();
}
Though you have to further ensure that the login form is not exited without proper username and password (that should be upto you)
Use System.Windows.Forms.Timer because it runs in the same thread and hence would block the calls to main form (unlike System.Timers.Timer)

Categories

Resources