update parent window on dialogbox close WPF [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 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
}

Related

C# WPF Window creation [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 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;
}

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.

Label don't change value when I choose one CheckBox [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Hello for everyone I am new here,sorry if this post doens't match with at all with how should be,because It's my first post this.
I am wanting to change the value of the Label when I choose one CheckBox,but It's seems that the Label just changes when I click in the Label.
I was looking for see if the Label had some Events for make work it but I didn't found one.
The Label it's in another groupBox,It's seems like that :
In one GroupBox has 3 CheckBox and i want that when i choosing one or all from that CheckBox will change the value of the Label in the another GroupBox.
Please guys,help me here,I am really wanting to understand why It's not working.
You can just register an event handler for the CheckChanged event and change the label's caption in the event handler :
public partial class Form1 : Form
{
public MyForm()
{
InitializeComponent();
myCheckbox.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox1.CheckedChanged += new System.EventHandler(this.checkedChanged);
myCheckbox2.CheckedChanged += new System.EventHandler(this.checkedChanged);
}
private void checkedChanged(object sender, EventArgs e)
{
myLabel.Text = "Some text";
}
}
use this code here, but change the event to the control you're looking for
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your value here";
}
you can do this by double clicking on the radio button control and then it will generate the event for you and then set your code as per the example here.

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