I'm implementing a window form in C#. I want to close current window and open a new window. (Just like File->New function in applications)
This is my code
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Image_Editor IE = new Image_Editor(); //Image_Editor is the name of window
IE.Show();
this.Close();
}
Images of what I want to achieve:
(source: infosight.com)
I want to implement the "new" function as shown in the above link. When executing given code, new window just appeared and both windows close at the same time.
What should I do to solve this?
if you mean that you want to close this windows and open a windows like your current windows use this
Myform frm; //thats name of the class of your form like Form1
frm = new Myform();
frm.Show(); // show the secend one
this.Close(); // and close the first one
Related
I have a form and through this I press a button and a new form opens. If I click minimize this new form, the form I call this form from is minimized as well. How could I make sure the form from which I call the new form is not minimized?
Maybe I have to enable some property in the form or something.
Of course, I tried with the following code and with the form propety singleFixed but the two forms are minimized:
private void bminimize_Click(object sender, EventArgs e)
{
WindowState = FormWindowState.Minimized;
}
Maybe have I to create this new form as subform or something like this?
EDIT: How I call this new form:
private void Button_Click(object sender, EventArgs e)
{
DateTime rnow = DateTime.Now;
Chronometer chrono = new chronometer();
var resultchrono = chrono.ShowDialog();
if (resultchrono == DialogResult.OK)
{
...
}
You're using ShowDialog(), which is documented thus, emphasis mine:
This version of the ShowDialog method does not specify a form or control as its owner. When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.
If you need the dialog result, you'll need to use the ShowDialog(owner) form of the call with say, the desktop window handle.
Its because of: chrono.ShowDialog();. Show the dialog with chrono.Show(); . But you will have to handle returned values differently.
If you need the DialogResult in the same method in which you open the window, then use AKX´s answer.
I have been searching everywhere for the answer to my problem but i cant seem to locate anything.
I am trying to open 2 different windows, that i have already created, after a button press. On the button press, i go to open the new window, but it just opens a blank window, not the window i want.
This is my function that i am trying to open my new window with, and it is inside my MainWindow, and i want to open my window i have created in Jobs (see image below).
void Jobs_Clicked(object sender, RoutedEventArgs e)
{
Window Jobs_Window = new Window();
App.Current.Jobs = Jobs_Window;
Jobs_Window.Show();
this.Close();
}
Solution Explorer
You are creating an instance of the standard Window class rather than your custom Window that you have made.
If you have added a Window, called JobsWindow for example, then create an instance of that rather than just the .NET built-in Window:
void Jobs_Clicked(object sender, RoutedEventArgs e)
{
Window Jobs_Window = new JobsWindow();
App.Current.Jobs = Jobs_Window;
Jobs_Window.Show();
this.Close();
}
I'm currently trying to create a Properties Window which is opened after a Button on the Outlook Toolbar is pressed, i now have:
1) the Button on the Toolbar (currently if pressed nothing occurs)
2) i know how to create the method which would hold the action after the Button is Pressed
-but, I am a beginner and i don't know how to create a window which would open after the button is pressed, the Window should be fairly big, and for now have nothing but a checkbox(which i later would like to apply some method to.
if you ever created a window which opens after a button is pressed, i would be really pleased to get your help.
All help is appreciated, thank you
Here's the recommended way of opening a dialog window when the user clicks a button:
Add a new form to your project (e.g. MyForm) and then you can use the following code in your button's click event handler:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
if (myForm.ShowDialog() == DialogResult.OK)
{
// The code that should be executed when the dialog was closed
// with an OK dialog result
}
}
In case you do not want the new window to be modal (i.e. you want to allow the user use other parts of the application while the window is opened), the code gets even more simple:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
myForm.Show();
}
You can also create your form on the fly without adding one to your project, which is a bit more complicated, but advanced developers prefer this approach instead of messing with the designer ;)
private void OnMyButtonClicked(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Text = "My Form Title";
// Add a checkbox
CheckBox checkBox = new CheckBox();
checkBox.Text = "Check me";
checkBox.Location = new Point(10, 10);
myForm.Controls.Add(checkBox);
// Show the form
myForm.Show();
}
Here is a small tutorial for you to follow..
http://msdn.microsoft.com/en-us/library/ws1btzy8%28v=vs.90%29.aspx
EDIT: I would also recommend you remember the msdn website because it will prove invaluable for other programming issues you come across..
you have to add a new form to your project. Then you call the constructor where you want to pop up the window.
like this
Form2 form2 = new Form2();
form2.showDialog();
Edit:
where form2 is not the "main" Form of you program.
This'll set your main window to the background as long as the newly popped up window is closed.
When you create a new Windows Forms application, what's the easiest way from dropping a button on the page, creating a click event which will open a new form.
My method requires clicking a button which will open up 9 new forms, all together, but I want to be able to position them where I want, I know the code for this, I just can't seem to open up multiple new forms at the same time?
Button -> Click -> Open 9 new forms which must be open at the same time.
To open a new form
MyForm myForm = new MyForm()
myForm.Show();
Where MyForm is a class that inherits from Form (i.e. your designed form)
How you would do this would depend on whether the forms are all the same or not but
For i as integer = 0 to 8
dim frm as new Form1
frm.Show
Next
Using the static method ie: form1.show is not generally a good idea.
Cheers
Solution in C# using System.Windows.Forms
private void Button_Click(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Show();
}
To open more than 1 form, a loop with each form would be required.
foreach (Form form in formArray)
{
form.Show();
}
Scenario
I have a C# WinForms application with a main form. I also have a button on this main form, that, when clicked, creates and displays a new form.
The problem....
...Is that I cannot click on anything on the main form when the new form is open.
The Question
How do I solve this? Is it possible to use both forms simultaneously?
Code To Launch New Form
private void barBtnStatsMonitor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//XtraMessageBox.Show("This Feature Has Not Been Fully Implemented Yet!");
using (StatsMonitorForm frm = new StatsMonitorForm())
{
if (frm.ShowDialog() == DialogResult.OK)
{
}
}
}
ShowDialog() opens a modal dialog.
Show() opens non-modal.
Try frm.Show instead of ShowDialog. ShowDialog opens the new form as a modal dialog so you cannot access the base form until you close this one.
when closing the main form its like closing the app...
so a suggestion would be, disable the main form close button when there are child forms open... and just enable it again when there are no child forms open...
or create a global variable(a bool perhaps), that when a child form is open.. its set to true... so when pressing the close button on the main form... it checks this variable if its true it prompts to save.. else it just closes...
ShowDialog() displays the form in MODAL mode which means you must close new form opened.
private void barBtnStatsMonitor_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//XtraMessageBox.Show("This Feature Has Not Been Fully Implemented Yet!");
using (StatsMonitorForm frm = new StatsMonitorForm())
{
frm.Show();
//do some work here to get the dialog result some other way..
}
}