I want to open a POP up on a context menu button click in C#.Please help.
And thanks in advance
You need to create a new Window class. You can design that then any way you want. You can create and show a window modally like this:
MyWindow popup = new MyWindow();
popup.ShowDialog();
You can add a custom property for your result value, or if you only have two possible results ( + possibly undeterminate, which would be null), you can set the window's DialogResult property before closing it and then check for it (it is the value returned by ShowDialog()).
Related
In my program I have two windows, the first one being my main window with a text box and the second one having an entry field with a button to update the text box in the first window. I'm a beginner in terms of using WPF and coding in C# in general, but is there a way to pass a pointer or reference of my main window to the second window so the second window can edit the text box of my first window? Is that even the right way to think about solving this issue?
WPF assumes you are binding your forms to a ViewModel object. This object can be bound to more than one form to give you different views and capabilities, so in this case you'd bind the same ViewModel to both forms, and what is changed in your edit form will appear automatically in your main form.
Your question is a bit vague and there are many approaches to accomplishing this. MVVM as Steve Todd mentions, is one.
However, it sounds like you simply want to open the window as a dialog. In your second window's code behind, be sure your textbox has a name in XAML and then access it create and easily accessible property that gets and sets your textbox value.
public MyTextContent
{
get => this.MyTextBox.Text;
set => this.MyTextBox.Text = value;
}
You can control the return value based on conditions (such as OK or Cancel buttons) if you like by using click events. The window contains a DialogResult property. The default is false, so you will need to set this somewhere.
this.DialogResult = true; // OK
Then in your main window's code behind, create a new instance of the window, assign it's property and show it. This will need to be done during a click event of a button or some similar trigger
var myDialog = new MyDialogWindow()
{
MyTextContent = "Textbox Starting Value";
}
bool? result = myDialog.ShowDialog(); // Returns when the dialog window is closed.
if(result != null && result)
{
this.LocalTextBox.Text = myDialog.MyTextContent; // Copy the text to the main textbox.
}
Typically you do this in data context of your main window. You use IoC to pass an instance of popup notification service in the constructor and create a private reference. You call that service method that displays the popup notification where user can enter async (and await) for its response or use reactive extensions to subscribe to submit action of that button. A thing to look out for is that you can update ui only in dispatcher thread and do not forget to dispose the subscription after you have finished using the window.
My program has a MainWindow and a SecondWindow, which is called by the first one like this:
SecondWindow config = new SecondWindow();
config.Owner = this;
config.Show();
Those lines are contained on a Button.Click method. And I want to check if it is already open, close it or do not open it.
Thanks!
Do not create a new instance. Just add it to top of your MainWindow class, and when you click the button, use secondWindow.Hide();. You must hide, because if you close it, you can't show it again. If you want to do not open it, activate the window and take it to top of desktop with secondWindow.Activate();.
Try this:
if(Application.Current.Windows.OfType<SecondWindow>().FirstOrDefault() == null)
{
//second window not exist
}
I am using MDIParent window form which contains menus, when I click on same menu again it open a new window. so how to stop this from reopening the window if it is already open? It should not display window form every time on click.
Use Application.OpenForms property.
Boolean found =
Application.OpenForms.Cast<Form>().Any(form => form.ID == "TargetFormID"
if (!found)
{
// Open a new instance of the form //
}
2 ways:
Way 1, flags:
Keep a flag (or list of flags) for the open forms.
Each time you open the form (create a new() one) set the flag to "true".
When the form closes, set the flag to false.
In the button's click event, check the flag to see if the form is open before creating a new one.
Way 2, keep a reference:
Keep a reference in the main form to all the forms you're using.
Initialize them as null when the forms aren't open.
When you open a new form set the reference to it.
On the button's click event check if the form's reference is null before you create a new one.
I prefer the second way. It's easier to control your resources when you have references to all your sub-forms.
You could maintain a list of open forms (and check the list in the onClick event), or disable/enable the menu item when the form opened ot closed.
Another why would be to create a Property in the Form which keeps the default instance you use.
private static Form _defaultInstance;
public static Form DefaultInstance()
{
get {
if(_defaultInstance == null || _defaultInstance.IsDisposed)
{
_defaultInstance = new yourTypeHere();
}
return _defaultInstance;
}
}
And now you always access your window through this property:
yourTypeHere.DefaultInstance.Show();
I'm trying to create a custom MessageBox by using a WPF Window that is called with ShowDialog().
So far, I've managed to implement everything, except for one thing.
As you know, when you use MessageBox.Show("text"); you cannot set the focus or click the parent window (the one that called the MessageBox). If you do try to click the parent window, the MessageBox will blink briefly in order to alert you that you must close if first.
Windows created with Window.ShowDialog();, however, do not show that behavior. In fact, while you cannot set the focus to the parent window, the child (called with ShowDialog()) will never blink briefly.
My question is, is there any way to implement that in WPF? I've been searching for an answer but I must admit, I am stumped.
Thanks everyone!
You need to set the Owner of the modal window correctly, e.g. using the following code from within the owning window:
Window win = new SomeModalWindow();
win.Owner = this;
win.ShowDialog();
You would have to set Owner property of the child Window to the parent Window. See the MSDN Documentation here.
In the program I am trying to build, I have a menu button that opens a second window. The user puts information into the second window, presses the "Done" button, and the information is transfered into the main window. The problem I am having is opening the second window. I have both windows build in xaml files in Visual Studio but I can't find a way to show the second window. Using "Window window = new Window" does not fit my needs because 1) I already have the second window built and 2) I have tried this and I cannot figure out how to add children to the window; there is no window.children nor any grid to put the children into. Thank you in advance!
Moments after I pressed post, I thought of something I hadnt tried:
"WindowAdd add = new WindowAdd; //WindowAdd being the second window
add.Show();"
This does exactly what I want it to do. The next problem I have is sending the information the TextBoxes into the MainWindow. I am thinking cookies might work but am unsure. Anyone have any thoughts? Thanks in advance!
You need to create the Window in code, but instead of doing:
Window window = new Window();
You should use:
Window2 window = new Window2(); // Assuming the window's class name is Window2
This will construct and initialize an instance of your new window class, defined in XAML. Once you've done this, you can open the window and you'll see all of your controls.