C# WPF Window creation [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm creating a WPF project based on C#.
While working on my project, I have ran into some troubles in how to create a single instance window (Like if i want to open the window X, it cannot be opened again unless it is closed). I found many articles online but they seemed lika lot of work, that it seemed easier to just disable the button when the window is open and enable it when the window is closed.
On the other hand I thought of an alternative solution which worked, but I want to see if it will be heavy on my system when a lot of windows are created.
The solution is shown below.
public partial class MainWindow : Window
{
Window1 wndw = new Window1();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!wndw.IsActive)
wndw.Show();
}
}
}
As you can see, i created the window at the start, and when the button to open it is clicked, it checks if it is active or not to show.
If I follow the latter method, will all the windows be loaded on MainWindow creation, or will the components only load when the event window1.show() is launched?
Thank you for your support

This is how I would have done it:
MainWindow code:
Window1 window1 = null; // global reference var
private void btnOpenWin1_Click(object sender, RoutedEventArgs e)
{
if (window1 == null)
{
window1 = new Window1();
window1.Closed += Window1_Closed; // register event to detect when Window1 closed
window1.Show();
}
else
{
window1.Focus();
}
}
// register event to remove Window1 ref to be able to open it again
private void Window1_Closed(object sender, EventArgs e)
{
window1 = null;
}

Related

Avoiding boilerplate code in WPF dialogs

I am new to WPF, and my application uses several dialogs. Each of the dialogs currently looks like this:
public partial class EditTeamDialog : Window {
public EditTeamDialog() {
InitializeComponent();
Loaded += (sender, args) => MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
}
private void OkButton_Click(object sender, RoutedEventArgs e) {
DialogResult = true;
}
}
The Loaded += line and the Click method both seem to me so common behavior that I expect them to be provided by WPF. It shouldn’t be necessary for every programmer of a dialog window to write this code.
How can I achieve the same behavior (dialog focuses first element on startup, closes on Enter) with writing less code?

update parent window on dialogbox close WPF [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to update my Main window when I close child window. I'm updating listview record and then on ok button I'm closing my child window and I'm trying to update my parent window without MVVM technique
private void btnDialogOk_Click(object sender, RoutedEventArgs e)
{
MainWindow mn = new MainWindow();
mn.InitializeComponent();
List<Scheme> items = new List<Scheme>();
List<Scheme> objScheme = oSchemeBLL.getAllSchemeListBLL();
mn = new MainWindow(objScheme);
this.Close();
}
You could make use of Closed event, that is raised when the Window is about to close. Here's an example that you can implement in your parent window in the declaration of you child window:
Window TestWindow = new Window();
TestWindow.Closed += (s, eventarg) =>
{
// Your method to update parent window
};
You can try to suscribe to Closed event of child window from parent window.
Window child_window;
child_window.Closed += child_Closed;
In that event handler, you can get the viewmodel from your child window, and access to data.
private void child_Closed(object sender, EventArgs e)
{
Window child;
ViewModel vm;
//Cast sender to windows type
child = (Window)sender;
//Cast window datacontext to viewmodel
vm = (ViewModel)child.DataContext;
//Access to properties or whatever you need
vm.Foo
}

How could I close form? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
When I click button from Form 1, the data of form 2 will display. So, I click button from form 1 again. The next data will add in new form 2, but form before still display. Therefore I have 2 form display. How could I close form before and display next form?
In order to make this, you need to store the reference to your form and manipulate with it.
What you want - close Form2 every time and open again:
public class Form1
{
private Form2 _form2;
public Button1_Click(object sender, EventArgs e)
{
if (_form2 != null)
{
_form2.Close();
}
_form2 = new Form2();
_form2.Label1.Text = DateTime.Now.ToString(); // or any other actions with form
_form2.Show();
}
}
It will close the previously used form and create a new one every time.
What you can also do - reuse this form and add some kind of "close form" button:
public class Form1
{
private Form2 _form2;
public Button1_Click(object sender, EventArgs e)
{
if (_form2 == null)
{
_form2 = new Form2();
_form2.Show();
}
_form2.Label1.Text = DateTime.Now.ToString(); // or any other actions with form
}
public Button2_Click(object sender, EventArgs e) // close button
{
if (_form2 != null) {
_form2.Close();
_form2 = null;
}
}
}
It will simply create a form at first time, and then just change its text when you click your button.

calling a method from another window form closing event. c# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
How can i call method of a window form from another form closing event ?
Suppose second for is getting closed and i want to call method of first form when second get closed to update some changes on first window form.
You can add an event handler to the form_closing event in the first form and handle it accordingly.
Somewhere in form1
form2.Form_Closing += yourhandler;
This assumes that form 2 has a a control called TextBox1, when form 2 closes the lambda expression will be called and transfer data to form 1.
public partial class Form1 : Form
{
private Form2 openedForm2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Not sure if you would want more than 1 form2 open at a time.
if (this.openedForm2 == null)
{
this.openedForm2 = new Form2();
//Here is your Event handler which accepts a Lambda Expression, code inside is performed when the form2 is closed.
this.openedForm2.FormClosing += (o, form) =>
{
// this is Executed when form2 closes.
// Gets text from Textbox1 on form2 and assigns its value to textbox1 on form 1
this.textBox1.Text = ((Form2)o).Controls["TextBox1"].Text;
// Set it null so you can open a new form2 if wanted.
this.openedForm2 = null;
};
this.openedForm2.Show();
}
else
{
// Tells user form2 is already open and focus's it for them.
MessageBox.Show("Form 2 is already open");
this.openedForm2.Focus();
}
}
}
Pass a reference from your first form to your second. Say you create your second form this way (From your first form):
Form2 frm2 = new Form2();
frm2.referenceToFirstForm = this
In your second form you should have this:
public Form1 referenceToFirstForm
Then in your OnClosing event you can reference referenceToFirstForm

Why doesn't my second form show up? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I made a timer, but when I press a button nothing happens.
My code is:
private void button3_Click(object sender, EventArgs e)
{
Instellingen form = new Instellingen();
form.Show();
}
Instellingen.cs code:
public partial class Instellingen : Form
{
public Instellingen()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
MessageBox.Show("text", "text");
}
}
}
The button3 event doesn't even fire (confirmed by adding a breakpoint, it doesn't get there)
Check if the button's event realy connected to the method button3_Click.
Put a breakpoint in the first line of button3_Click and check if the code get there.
Make sure your button's click event is button3_Click. Probably your button's click event is empty.

Categories

Resources