I think my question is simple but i can not handle with it.
If i have window and a button on it and i want to open other window control on that window how can i do that?
I try do that through the VSM but I jam and still don't know how to do that. Maybe there is a simplier way?
Yes. Thank all of you for answers, but I think i didn't specify clearly what i wanted to get.
Methods: Show() and ShowDialog() opens new windows and i wanted to open controls in current window.
I think about something like that:
newWindowControl nwc = new newWindowControl();
LayoutRoot.Children.Add(nwc);
but in VSM.
I don't know if it is possible to do that.
If you want to open another window on top of existing window, just like a popup.. You can simply Initialize a new window and call its show method.
//Second window is of type Window
SecondWindow window = new SecondWindow();
window.Show();
private void button1_Click(object sender, EventArgs e)
{
using (Form2 form2 = new Form2())
{
form2.ShowDialog();
}
}
Related
I'm interested in the way to make some sub-forms, like child forms (for example, helper form that shows when you click on a find button and want to search through the suppliers).
But I don't want it to create another "window" in the taskbar, but to be integrated in the main form.
I know about Show() and ShowDialog(), but it opens another window in the taskbar...
I tried with MDI and was able to make it, but I don't want to use MDI.
So, can someone provide some knowledge about some alternative?
I've seen examples of this in some programs, but I don't know how is this achieved. I'm pretty new to visual C#.
For your other form use the ShowInTaskbar property and set it to false
this.ShowInTaskbar = false;
Then you can either use Show() or ShowDialog()
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
I have another issue in converting my Winforms program to a WPF program. In my first program, I had a smaller window open to allow the user to adjust some data, and then when it closed, the other form was activated again with the new data.
I used form2.ShowDialog(); to open the form, which automatically makes the parent form deactivated in Winforms. This way when I closed form2, the parent form was activated, and I was able to use an event handler form1_Activated to reload and re-initialize some of the settings successfully.
However, now when I attempt to do the same thing with WPF, I am still able to open form2 using form2.ShowDialog();, but then when I close the form, it does not register the form1_Activated event handler. Instead, in order to reload the settings, I must click on another window, and then come back into my program to register the form1_Activated event handler.
Am I just doing something wrong, or is there another event handler that I should be using in WPF to achieve the same thing I was able to do in Winforms?
Calling ShowDialog() causes the dialog box top appear in modal mode so I don't understand why you would need an event handler to process the results after the dialog box is closed. Keep in mind that you can access public variables in the DialogBox, as well. If I understand your question, this should do what you are asking:
MainWindow:
My_DialogBox dlg = new My_DialogBox();
dlg.Owner = this;
dlg.MyPublicVariable = ''; //some value that you might need to pass to the dialog
dlg.ShowDialog(); //exection of MainWindow is suspended until dialog box is closed
if (dlg.DialogResult == true)
{
//dlg.MyPublicVariable is still accessible
//call whatever routines you need in order to refresh the main form's data
}
DialogBox:
private void OK_Button_Click(object sender, RoutedEventArgs e)
{
MyPublic variable = something; //accessible after the dialog has closed.
this.DialogResult = true;
}
private void Cancel_Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
The MSDN write-up on dialog boxes is pretty good. There may be some tips that might help you even more:
http://msdn.microsoft.com/en-us/library/aa969773.aspx
Good luck!
My application is of such a nature that I need to jump around many windows consecutively. From my menu window I need to open another window (from the selection) and the disable or close the menu window.
What I'm currently using is window.show and then this.close() method to close the menu window.
Ex:
private void MainMenuControl_Link1MouseDown(object sender, RoutedEventArgs e)
{
// Utilities
UtilitiesMenyWindow UtilitiesMenyWindow = new UtilitiesMenyWindow(); // Assign Variable to window
UtilitiesMenyWindow.Show(); // Open window
this.Close(); // close current window
}
Then from within the new windows I just reopen the MainMenu window using the same method.
private void Utilities_Link3MouseDown(object sender, RoutedEventArgs e)
{
// Return to Main
MainMenuWindow MainMenu = new MainMenuWindow(); // Assign Variable to window
MainMenu.Show(); // Open Main window
this.Close(); // close login window
}
I then also keep a public variable class with static variables to store all the variables that are generic to all the windows.
All this is working fine for me except for one snag. If I were to call the UtilitiesMenyWindow from another window (not MainWindow) it's going to return to the MainMenu instead of the window I opened it from.
Is there a easier more generic way to return to the window that opened the secondary window without having to tell it to close itself and open a specific window (in this case it's "hardcoded" to MainMenu) (Obviously I first open and then close)
I was looking at the Unloaded event but how do I get the original window to stay hidden until this event occurs without it having to sit there and wait in a loop which is not a good idea.
Maybe somebody can guide me in a way to set it up as an automatic event that "fires" the event-handler, which in turn then activates the previous window?
Rather than closing windows, you could consider hiding them. That way, you could pass a reference to the calling window whenever you create a new window.
i.e.
UtilitiesMenyWindow UtilitiesMenyWindow = new UtilitiesMenyWindow();
UtilitiesMenyWindow.CallingWindow = this;
UtilitiesMenyWindow.Show();
this.Hide();
then, when you want to close the new window and return:
this.Hide();
this.CallingWindow.Show();
You could just close the original window - since all your windows are probably inheriting from Window class you just need to store the current window in a 'Window' typed variable on the utilities menu
This way you can continue doing what you are doing:
e.g.
private void MainMenuControl_Link1MouseDown(object sender, RoutedEventArgs e)
{
// Utilities
UtilitiesMenyWindow UtilitiesMenyWindow = new UtilitiesMenyWindow(); // Assign Variable to window
UtilitiesMenyWindow.ReturnWindow = this;
UtilitiesMenyWindow.Show(); // Open window
this.Hide(); // hide current window
}
And add
Window _returnWindow;
on your utility window class
Then in that class on the close method you can call the original window type:
private void Utilities_Link3MouseDown(object sender, RoutedEventArgs e)
{
// Return to original
_returnWindow.Show();
this.Close(); // close login window
}
This is of course assuming you don't kill the original window
What I need is such an event handler in my window class.
void someEventHandler(object sender, RoutedEventArgs e)
{
MyNewWindow mnw = new MyNewWindow();
mnw.Owner = Window.GetWindow(this);
mnw.ShowDialog();
this.Close();
}
Window.GetWindow(this) returns the parent window of the current window.
I had thought when the owner of the new window is the parent window of the current one, it would wait for the parent; and not the current one. But it did not work that way. Current window waits for the execution of the new and closes only after.
If I use Show() instead of ShowDialog() for some reason the window is not shown at all.
Probably I need some delegate methods but I am not sure where to start.
Edit: I guess I need to improve the question for future references:
The new window should be a dialog to the parent window. If I use Show() the parent window becomes accesible and I dont want that. If I use ShowDialog() it becomes a dialog to the current window, meaning that the current window does not close until the new window is closed, and I dont want that either.
Closing a window causes any windows that it owns to be closed.
If you just want the owner window to not be visible, try this;
void someEventHandler(object sender, RoutedEventArgs e)
{
MyNewWindow mnw = new MyNewWindow();
mnw.Owner = this;
this.Hide(); // not required if using the child events below
mnw.ShowDialog();
}
You'll likely want to hook up some event in the parent window that acts accordingly when you close the child window depending on your requirements.
EDIT
You could perhaps control the hiding of the (multiple parents) from the child;
void OnLoad(object sender, RoutedEventArgs e)
{
this.Owner.Hide();
}
void Closed(object sender, RoutedEventArgs e)
{
this.Owner.Show();
}
If I understand what you're trying to do you want to close the current window and replace it with a MyNewWindow that is a child of the window that was the original window's parent (probably the app's main window).
To do that you should be using:
mnw.Owner = this.Owner;
instead of calling GetWindow, which gives you back the current Window instance. The purpose of GetWindow is to obtain the Window which contains some other UIElement, like a Button farther down the tree. When passing in a Window instance, you just get back what you passed in.
Calling ShowDialog() blocks. That means, the method returns only when mnw is closed and only then is the original window closed.
If you change that to Show(), mnw is closed as soon as you call Close() on the original window. because Window.GetWindow(this) returns this. Thus, you set the owner to this and when you close this, mnw gets closed too. That leaves us with:
MyNewWindow mnw = new MyNewWindow();
mnw.Owner = this.Owner; // may not be necessary
mnw.Show();
this.Close();
This code seems to work for me.
I have a windows form that pops up a dialog box if certian conditions are met when the form loads. The problem is the window does not stay on top and I can still click thing on the parent. However, there is a button on the form that when pressed opens the same window, when I do this it works as expected (like a dialog window).
Is there an issue with showing a dialog when a form is first loading?
Are you calling ShowDialog from the Form class? Because it will only set the parent window if called from another Form. Alternatively you can use the overload that has the IWin32Window parameter to specifically set the owner.
can you explain the issue further as this is my code which do not show the form it self until the dialog has been closed either you set the parent or not
private void Form1_Load(object sender, EventArgs e)
{
//your functionality goes here
AboutBox1 box = new AboutBox1();
box.ShowDialog();
}
}
on the other side you can also check with TopMost property
The ShowDialog method needs to be called from the form that you want to be it's parent/owner in order for it to be modal to that form. Alternatively I believe you can set the owner of a dialog directly but I have never needed to do that.
DaBomb,
To do what you want, you will have to call your modal dialog from the constructor of your main form, NOT from the Form_Load event.
Something like this:
public Form1()
{
InitializeComponent();
this.Show();
Form2 popupForm = new Form2();
popupForm.ShowDialog();
}