opening a window form from another form programmatically - c#

I am making a Windows Forms application. I have a form. I want to open a new form at run time from the original form on a button click. And then close this new form (after 2,3 sec) programatically but from a thread other than gui main thread.
Can anybody guide me how to do it ?
Will the new form affect or hinder the things going on in the original main form ? (if yes than how to stop it ?)

To open from with button click please add the following code in the button event handler
var m = new Form1();
m.Show();
Here Form1 is the name of the form which you want to open.
Also to close the current form, you may use
this.close();

I would do it like this:
var form2 = new Form2();
form2.Show();
and to close current form I would use
this.Hide(); instead of
this.close();
check out this Youtube channel link for easy start-up tutorials you might find it helpful if u are a beginner

This is an old question, but answering for gathering knowledge.
We have an original form with a button to show the new form.
The code for the button click is below
private void button1_Click(object sender, EventArgs e)
{
New_Form new_Form = new New_Form();
new_Form.Show();
}
Now when click is made, New Form is shown. Since, you want to hide after 2 seconds we are adding a onload event to the new form designer
this.Load += new System.EventHandler(this.OnPageLoad);
This OnPageLoad function runs when that form is loaded
In NewForm.cs ,
public partial class New_Form : Form
{
private Timer formClosingTimer;
private void OnPageLoad(object sender, EventArgs e)
{
formClosingTimer = new Timer(); // Creating a new timer
formClosingTimer.Tick += new EventHandler(CloseForm); // Defining tick event to invoke after a time period
formClosingTimer.Interval = 2000; // Time Interval in miliseconds
formClosingTimer.Start(); // Starting a timer
}
private void CloseForm(object sender, EventArgs e)
{
formClosingTimer.Stop(); // Stoping timer. If we dont stop, function will be triggered in regular intervals
this.Close(); // Closing the current form
}
}
In this new form , a timer is used to invoke a method which closes that form.
Here is the new form which automatically closes after 2 seconds, we will be able operate on both the forms where no interference between those two forms.
For your knowledge,
form.close() will free the memory and we can never interact with that form again
form.hide() will just hide the form, where the code part can still run
For more details about timer refer this link, https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netframework-4.7.2

You just need to use Dispatcher to perform graphical operation from a thread other then UI thread. I don't think that this will affect behavior of the main form. This may help you :
Accessing UI Control from BackgroundWorker Thread

This might also help:
void ButtQuitClick(object sender, EventArgs e)
{
QuitWin form = new QuitWin();
form.Show();
}
Change ButtQuit to your button name and also change QuitWin to the name of the form that you made.
When the button is clicked it will open another window, you will need to make another form and a button on your main form for it to work.

private void btnchangerate_Click(object sender, EventArgs e)
{
this.Hide(); //current form will hide
Form1 fm = new Form1(); //another form will open
fm.Show();
}
on click btn current form will hide and new form will open

Related

How to show a status dialog while parent form is closing

I have a WPF app that takes a while to close so I want to add a 'please wait' dialog to show up while the main form is closing. However, when I added 'formmessage.show' to my main form's closing event handler, the form displays but instead of the 'please wait' text, all i get is a white rectangle. This only seems to happen when calling this code from the closing handler. It works fine from other handlers (form click or maximize). Can anyone help? Here is my simplified code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Form1 f = new Form1();
f.Show();
System.Threading.Thread.Sleep(3000);
}
}
Form1 has a label on it which says 'please wait'.
Try out:
Application.Current.Dispatcher.Invoke(
DispatcherPriority.Normal,
new Action(() =>
{
Form1 f = new Form1();
f.Show();
System.Threading.Thread.Sleep(3000);
}));
BTW, you can remove Thread.Sleep() because default value of the Application Shutdown mode is OnLastWindowClose so Application will be active until an user explicitly close active window, this behaviour could be changed by switching Application.ShutdownMode to OnMainWindowClose in the App.xaml file)
That would be because of the fact your creating the form in the same thread your telling to sleep, during which it's not going to respond to draw requests, try launching the form in another thread, and closing it in MainWindow's destructor.

Closing and Opening Forms

What is the best way to close a form and get to another form.
At present I have a Main Form and then two forms:
I want to know how to close one form and open another form efficiently.The two times I did it had a slight different outcome:
The main form in my application is just the introduction which will load some Company Logo and show a progress bar which will then take it to a log-in form, where in I have a log-in button, from where the actual application will open.
I have some questions.In the Main Form I have added this code:
private void Form1_Load(object sender, EventArgs e)
{
int i;
for (i = 0; i <= 100; i++)
{
progressBar.Value = i;
label1.Text = "Please wait while Refresh loads up...";
}
}
private void progressTimer_Tick(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
Form form = new FirstForm();
form.ShowDialog(this);
}
1) This works fine, just that the new form that opens up is at the task bar and is not in the center of the screen(as I have set it's property).How do I fix this?
In the First Form I have added this code:
private void loginButton_Click(object sender, EventArgs e)
{
using( ERPQueries eq = new ERPQueries())
{
int? count = eq.CheckEmployee(userTextBox.Text,passwordTextBox.Text);
if (count == 1)
{
//testLabel.Text = "Ok";
this.Close();
Form form = new SecondForm();
form.ShowDialog(this);
}
else
{
testLabel.Text = "Invalid username or password!";
}
}
}
2) Here the next Form pops up in the center of the screen.I want to know how is it different from the first case, as I have used showDialog() in both the cases?
3) Also in the first case my Main Form Disappears, whereas in the second case the First Form is still visible in the background and disappears only after closing the SecondForm.
I'm sure I'm doing a lot of mistakes, my code is flawed.Please help.This is the first time I'm making an application with multiple forms.
Edit:
When I use Show() instead of ShowDialog() I don't see the new form.Am I missing something?
I tried this.Dispose() instead of this.Close().In the first case it works fine.In the second case, it disposes all the forms.
try:
form.Show();
not
form.ShowDialog(this);
which is what makes it modal.
ShowDialog() is blocking call, so if in one Form's deactivate event you call ShowDialog() of another, your parent form will not yet finish it deactivating.
Like a suggesion I can give you create a collection/stack of forms you want to manage and give control over it to a FormManager class, where you implement whatever logic you want and can call either ShowDialog() or Show(). In other words bring out forms Show/Hide management out forms itself.
Regards.

Windows Forms C#

I am learning windows forms and can create a one form with textboxes and stuff, but I was wondering how can I change the form upon let's say clicking a button?, so for instance my initial form has a textbox and a button, if the button is clicked I want to show a form with a dropdown and a button. SO the question should be:
1) How do I change the form upon clicking a button but without creating a new instance of the form.
2) If I wanted, how can I add a form when the button is clicked showing the same drop down and button as a pop up form?
In reality I would like to know both cases, changing the form via using the same form and via popping a new form on top.
Should the questions not be clear, I am willing to further explain
Thank you
I'm assuming you already know how to add controls in the form designer and how to implement event handlers.
Question 1
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Visible)
{
comboBox1.Visible = false;
textBox1.Visible = true;
}
else
{
comboBox1.Visible = true;
textBox1.Visible = false;
}
}
The button click handler simply toggles the visibility of the two controls.
Question 2
private void button2_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.ShowDialog();
}
This time the button handler instantiates a new form an then shows it as a modal dialog. Call Show() if you don't want to show modally.

In C# how to show a GUI from another GUI, based on the event from a class in another thread?

I am working on a DirectX based simulator.
On which I have to check for a device whether device has been plugged-in or removed from the PC.
I've managed to make classes for device arrival and removal on another thread, which raises an event from the thread itself on device arrival or removal.
The corresponding event method is being called in the main form and there:
Assume Form1 is main window and Form2 is the secondary.
Form2 form2Instance = new Form2();
I want to show another Form (Form2) keeping main Window (Form1) in behind (same as it behaves as form2Instance.ShowDialog(); in general cases.)
After a few tries I have done it by
Applicatin.Run(new Form2());, but the Form2 doesn't behave as'form2Instance.ShowDialog(); in any way.
Just giving the code if it can help in answering:
iARMdetectionThreadClass detection;
InProgram_iARMdetection iARMStatusGUI;
private void Form2_Load(object sender, EventArgs e)
{
iARMStatusGUI = new InProgram_iARMdetection();
detection = new iARMdetectionThreadClass();
detection.IniARM_device_Arrive += new iARMdetectionThreadClass.iARM_device_ArrivedEventHandler(detection_IniARM_device_Arrive);
detection.IniARM_device_Remove += new iARMdetectionThreadClass.iARM_device_RemovedEventHandler(detection_IniARM_device_Remove);
detection.startThread();
}
void detection_IniARM_device_Remove(iARM_deviceInfo senderInfo)
{
detection.StopCheckBeingRemoved();
MethodInvoker act = delegate
{
this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
};
this.label_iARMStatus.BeginInvoke(act);
Application.Run(new InProgram_iARMdetection()); //Blocking code
detection.StartCheckBeingRemoved();
}
void detection_IniARM_device_Arrive(iARM_deviceInfo senderInfo)
{
MethodInvoker act = delegate
{
this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
};
this.label_iARMStatus.BeginInvoke(act);
//detection.StopCheckArriving();
//detection.StartCheckArriving();
}
I need the code to be Blocking Code. In here:
Application.Run(new InProgram_iARMdetection()); //Blocking code
Perhaps mainform.AddOwnedForm(form2) will do what you want. It will make form2 display in front of mainform and when either one is minimized, the other is also.
You should handle the remove event on form2 rather than form1 and use the ShowDialog() method.
So when on form1 the arrival event will fire it will open up form2 like a dialog. Now when the device will be unplugged, on form2 removal event will fire where you can close the form.

how to hide the previous form when the present form is being loaded

In C sharp win forms,
not using MDI, m creating multiple forms
i wish to hide the previous form while loading a new form on a button's click
i write the following code to achieve the purpose but the previous form still remains visible, kindly help!!
here's the code...
private void btnEmployee_Click(object sender, EventArgs e)
{
Form f3 = new EmployeeLogIn();
f3.Show();
Form id = new Login();
id.Hide();
}
You're hiding a newly-created form. You need to get the reference to the previous form by either passing it into the current form, or using a static property.
EDIT: actually I think this is what you wanted to do:
private void btnEmployee_Click(object sender, EventArgs e)
{
Form f3 = new EmployeeLogIn();
f3.Show();
this.Hide();
}
Your code will only continue when the form is loaded, so when the f3.Show() statement is finished.
Consider showing the f3 form in a new thread.

Categories

Resources