This is probably a fairly simple question, but I have had no luck researching it thus far. I have a child window that has a yes and no button on it. When I click no I would like a check box to become unchecked in the parent window (which is the main window of my program).
Is there anyway that I could do something like?:
//No Button
private void No_Click(object sender, RoutedEventArgs e)
{
NameOfParent.checkBox.Checked = false;
}
I've seen this question but do not think that it exactly addresses my problem.
What is the correct way to go about this?
I've been using this to open my other windows:
Parent Window - Current code:
//Open new window
private void checkBox5_Checked(object sender, RoutedEventArgs e)
{
var newWindow = new ChildWindow();
newWindow.button_no.Click += buttonNo_click;
newWindow.Show();
}
//Unchecks Checkbox 5
private void buttonNo_click(object sender, RoutedEventArgs e)
{
checkBox5.IsChecked = false;
}
Opening the child window from the parent window:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.buttonNo.Click += buttonNo_Click;
window.Show();
}
In the code of the parent window, include this click event for the child window's no button:
private void buttonNo_Click(object sender, RoutedEventArgs e)
{
//code to uncheck the checkbox goes here
}
This turned out even simpler than the solution for Windows Forms that I've been using...and it appears to work in Windows Forms as well.
On construction of the child window, simply add a parameter that is of the type of the parent window.
Then when you are constructing your child window in the parent window code do the following (In The Parent Window Class):
ChildWindow child = new ChildWindow(this, other_param, other_param2.....);
ChildWindow.Show();
The keyword this will pass the parent window into the ChildWindow
Then you can store it into a class variable and access properties of it.
Child class constructor and parentWindow variable:
private ParentWindowType m_parentWindow = new ParentWindowType();
public ChildWindow(ParentWindowType parent, int other_param, string other_param2....)
{
m_parentWindow = parent;
}
Then in other child class methods, such as your button click handler you can access properties from the parent window:
public void ButtonClickHandler(....)
{
m_parentWindow.checkBox1.Enabled = true;
m_parentWindow.checkBox1.Checked = true;
}
This way you can store the parent window within the child window, and always have access to it.
Its nice and clean and simple this way. Ensure to make the parent window variable private in the child window class.
If made public, it would allow you to do funny things like
parentWindow.childWindow.m_parentWindow.childWindow etc......
Related
I am pretty new in WPF C#. I am held up with the following Issue:
Lets say I have a MainWindow.xaml, and have the main program logic in it.
I have a second window, called Second.xaml
I am calling Second.xaml in MainWindow.xaml.cs,
currently I am doing:
MainWindow.xaml.cs:
var wind = new Second();
wind.Show();
This successfully opens the second window, where I have a few buttons.
My motive is to trigger events in MainWindow using Second.xaml.cs
(i.e.)
in Second.xaml.cs:
....
..
MainWindow mainwindowID = new MainWindow();
....
..
.
private void nextButton_Click(object sender, RoutedEventArgs e)
{
mainwindowID.textBox.Content = "Displaying In Mainwindow";
}
When I click the Next button in Second.xaml, I want the text Box inside Mainwindow to be updated.
Though the program in running, nothing changes inside MainWindow.
Is it possible to control it like that?
I have the MainWindow displayed using a projector, and the second window on the monitor. So I trigger events inside the second window and want them to be displayed in the MainWindow.
Is there any other solution for this kind?
Update:
If the textbox is inside SecondPage.xaml and displayed inside MainWindow.xaml using a Frame, how do I call it from Second.xaml?
In the first window (MainWindow) you can invoke the second window in this way:
var wind = new Second();
wind.FirstWindow = this;
wind.Show();
while the second window can look like this:
public MainWindow FirstWindow { get; set; }
private void nextButton_Click(object sender, RoutedEventArgs e)
{
FirstWindow.textBox.Content = "Displaying In Mainwindow";
}
I would suggest using Delegates. Have a look at the link here;
Delegates
This way you can create a method in the first Window like so;
private void WaitForResponse(object sender, RoutedEventArgs e)
{
var second = new SecondWindow();
second.ReturnTextBoxText += LoadTextBoxText;
SecondWindow.ShowDialog();
}
Then in your second Window;
internal Action<string, int> ReturnTextBoxText;
private void nextButton_Click(object sender, RoutedEventArgs e)
{
ReturnTextBoxText("Displaying In Mainwindow");
}
Finally load that response in your first Window;
private void LoadSelectedCompany(string text, int passedCompanyID)
{
contactCompanyTextBox.Text = companyName;
}
I'm a beginner and having some dicciculties with XAML.
I have a main view A, which has a button to open a pop-up-window B. When this happens, Window A should still be visible and openened, but disabled. I've done this in code behind (maybe not the cleanest way, but the only way I know how). The code i used for this is the following:
//Code behind from view A
private void X-Button_Click(object sender, RoutedEventArgs e)
{
var BWindow = new BView();
BWindow.Show();
this.IsEnabled = false;
}
I would like to get window A enabled again once i close window B, but i can t seem to get this work. Any help would be very much appreciated.
You could do it in the following way.
You register yourself on the Closed event of the window, and when it gets closed, you unregister the event, and re-enable the this form.
private void Button_Click(object sender, RoutedEventArgs e)
{
Window BWindow = new BWindow();
BWindow.Show();
BWindow.Closed += BWindow_Closed;
this.IsEnabled = false;
}
void BWindow_Closed(object sender, EventArgs e)
{
Window win = sender as Window;
if (win != null)
{
win.Closed -= BWindow_Closed;
}
this.IsEnabled = true;
}
I assume you are looking for modal window. See similar question asked here: How do make modal dialog in WPF?
The solution is in using ShowDialog method from Window class. See here for reference:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Modal window is the concept when you open new window B from the existing window A. While the B is open, the A is disabled and cannot be used. Window A become active only when the B is closed.
Being a very first user in Windows Form Development I want to ask a simple question ...
I created a form(MainWindow.cs) within the solution which opens at the time of running that solution.
Latter I created a second form(SecondWindow.cs) and created a event so that it can be called from the first window by clicking a button.When the second window loded up the first window(MainWindow.cs) will be disabled.
Now I want to enable the MainWindow.cs when the second window is closed.
How to do That...
A simple solution I already have is to hide the MainWindow.cs and latter on closing the second window make a new object of first window and show it.But it is not a good way i think because there is already a object created by .net framework for first window automatically, so why we should create a new object of Mainwindow.cs .
Code Of First Window ( MainWindow.cs ) :
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow();
this.Enabled = false;
price.Show();
}
Code Of Second Window ( On closing SecondWindow.cs )
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
// what will goes here to make the MainWindow.cs to enable state
}
Use price.ShowDialog() to show second form as modal dialog. Main form will be disabled until you close second form:
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
using(SecondWindow price = new SecondWindow())
price.ShowDialog();
}
You can pass the main form as an owner to the second form
private void priceControllToolStripMenuItem_Click(object sender, EventArgs e)
{
SecondWindow price = new SecondWindow() { Owner = this };
this.Enabled = false;
price.Show();
}
Then you can reference it from the second form.
private void price_controll_FormClosed(object sender, FormClosedEventArgs e)
{
Owner.Enabled = true;
}
I have a control in my parent mdi form which I wish to be given the focus if all mdi children have closed. I've tried hooking into the child form's FormClosed event and setting the focus from there but when I test it, my control is not left with focus when I close the mdi child.
Can anyone tell me what I'm missing?
In the sample below, "first focus" gets hit and does its job correctly (if I comment out the first focus line, my tree does not get focus on startup, so it must be doing its job, right?)
Unfortunately, even though "second focus" gets hit, my tree does not end up with the focus when I close the child window.
Has focus on startup.
Has focus http://www.programmingforpeople.com/images/stackoverflow/focus1.PNG
Not focussed after closing child window.
No focus http://www.programmingforpeople.com/images/stackoverflow/focus2.PNG
Sample
using System;
using System.Windows.Forms;
namespace mdiFocus
{
class ParentForm : Form
{
public ParentForm()
{
IsMdiContainer = true;
tree = new TreeView();
tree.Nodes.Add("SomeNode");
tree.Dock = DockStyle.Left;
Controls.Add(tree);
}
protected override void OnShown(EventArgs e)
{
Form child = new Form();
child.MdiParent = this;
child.Show();
child.FormClosed += new FormClosedEventHandler(child_FormClosed);
tree.Focus(); // first focus works ok
}
void child_FormClosed(object sender, FormClosedEventArgs e)
{
tree.Focus(); // second focus doesn't seem to work, even though it is hit :(
}
TreeView tree;
}
static class Program
{
[STAThread]
static void Main()
{
Application.Run(new ParentForm());
}
}
}
I repro, smells like the WF logic that hunts for a focusable item is running after the event and messes up the focus. These kind of problems can be elegantly solved by running code after the event is processed by using Control.BeginInvoke(). This worked well:
private void toolStripButton1_Click(object sender, EventArgs e) {
Form child = new Form2();
child.MdiParent = this;
child.FormClosed += new FormClosedEventHandler(child_FormClosed);
child.Show();
}
void child_FormClosed(object sender, FormClosedEventArgs e) {
if (this.MdiChildren.Length == 1) {
this.BeginInvoke(new MethodInvoker(() => treeView1.Focus()));
}
}
Can anyone here do me a favor?
I have a MainWindow with a HideButton and RetrieveButton. When I click on either one button, it will goes to another ChildWindow. My ChildWindow have a OKButton.
The question here,
How to set if else statement in C# for pseudocode below?
private void OKButton_Click(object sender, EventArgs e)
{
// pseudocode
if (HideButton in MainWindow is clicked)
{
// Perform the works
}
if(RetrieveButton in MainWindow is clicked)
{
// Perform other works
}
Thanks in advance.
By, Aeris
If you are creating the child window on Retrieve or Hide you can easily pass parameters then either via the constructor or by setting a custom property:
ChildWindow child = new ChildWindow();
child.Retrieving = true;
child.Show();