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();
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 have a Winforms app written in C#.
On one form there is a button which on the Click event opens a second form using the following code -
frmConflicts check = new frmConflicts(c);
check.Show();
frmConflicts has lots of controls on it, yet the Form which opens on the click event is a default Visual Studio form. By that, I mean the very small blank form which VS gives you when you Add New Item and select Form. There are no controls on it.
I've stepped through my code and the frmConflicts constructor is called, so I can't understand why a blank form is appearing instead.
Any clues?
Is the method initializing your Controls (like InitializeComponents) called at any time (like in your constructor) ?
Try setting the TopLevel property to True:
frmConflicts check = new frmConflicts(c);
check.TopLevel=true;
check.Show();
I want to do something like the answer here:
How can I close a login form and show the main form without my application closing?
...but I want to pass a value selected on the initial form to the next (main) form. If I call an overridden constructor on the main form, where do I store the value in the meantime (between the initial form being dismissed and the main form being called)?
OTOH, if, instead of using the program.cs file to do this, I create the "initial form" inside the main form's Load() event (is there a better place), I could do something like this, but admittedly it seems rather kludgy:
0) Set the main form's size to 0,0 to hide it
1) Show the "initial" form/modal dialog, and store the value the user selects (in a button click event) in a form-global variable
2) Once the initial form/modal dialog closes, set the main form's size back to what it should be (unless modal result <> OK, in which case I close the main form and the app)
I know there's a better way to do this...
You don't have to pass a value to the main form. Just like your link explains, open your main form first. Then your main form can open the other form. This other form can place the information in a public property that the main form can access. Since the main form controls the lifetime of this other form, the main form gets the information held in the other form's public property, then closes the other form.
string myVariable;
using (OtherForm otherForm = new OtherForm())
{
otherForm.ShowDialog();
myVariable = otherForm.OtherVariable;
}
Try using ApplicationContext (System.Windows.Forms.ApplicationContext). You can show multiple forms as shown in the example in the following MSDN thread. And regarding data,you can have a common data object which is created once and the forms are instantiated with the data object passed to them before showing them.
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext%28v=vs.100%29.aspx
Backdrop: There are two forms, the main application form and a form to edit various settings for the operations of main application.
What would be the proper way to show the frmSettings form and how to dispose it once clicked OK or Cancel in the actual frmSettings form?
Perhaps a Dialog would be better suited to your Settings "form." There are subtle differences between a dialog and a form that would make the dialog easier to handle. A return code indicating the button that was clicked makes dialogs useful.
Supposing that you used a dialog - a using statement could be used (off the top of my head):
using (DialogSettings dlgSettings = new DialogSettings)
{
if (dlgSettings.ShowDialog() == DialogResult.OK)
{
}
}
If you insist on using a form then you would have to
Instance the form
show the form
record whether ok or cancel was clicked to a form level variable (from within the forms ok/cancel button click code)
hide the form
save the recorded value from the form
dispose of the form
make use of the saved ok/cancel value
fyi, using "frm" is not a recommended part of the C# coding guidelines. Microsoft prefers you don't use hungarian notation in .NET at all.
using (frmSettings s = new frmSettings() )
{
if( s.ShowDialog() == DialogResult.OK )
{
//do work
}
}
In the main application declare an instance and show it.
using(frmSettings settingsInstance = new frmSettings())
{
settingsInstance.Show(); //or ShowDialog()
}
Then just close the form when done...