how to pass button_click to child window in C#? - c#

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();

Related

Two windows display and control WPF C#

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;
}

How to flash a WPF Window? [duplicate]

If I have a Xaml Window, how does one open it as a child window, and then have the parent window wait for the child to close before the parent window continues executing?
Did you try showing your window using the ShowDialog method?
Don't forget to set the Owner property on the dialog window to the main window. This will avoid weird behavior when Alt+Tabbing, etc.
A lot of these answers are simplistic, and if someone is beginning WPF, they may not know all of the "ins-and-outs", as it is more complicated than just telling someone "Use .ShowDialog()!". But that is the method (not .Show()) that you want to use in order to block use of the underlying window and to keep the code from continuing until the modal window is closed.
First, you need 2 WPF windows. (One will be calling the other.)
From the first window, let's say that was called MainWindow.xaml, in its code-behind will be:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
Then add your button to your XAML:
<Button Name="btnOpenModal" Click="btnOpenModal_Click" Content="Open Modal" />
And right-click the Click routine, select "Go to definition". It will create it for you in MainWindow.xaml.cs:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
}
Within that function, you have to specify the other page using its page class. Say you named that other page "ModalWindow", so that becomes its page class and is how you would instantiate (call) it:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
}
Say you have a value you need set on your modal dialog. Create a textbox and a button in the ModalWindow XAML:
<StackPanel Orientation="Horizontal">
<TextBox Name="txtSomeBox" />
<Button Name="btnSaveData" Click="btnSaveData_Click" Content="Save" />
</StackPanel>
Then create an event handler (another Click event) again and use it to save the textbox value to a public static variable on ModalWindow and call this.Close().
public partial class ModalWindow : Window
{
public static string myValue = String.Empty;
public ModalWindow()
{
InitializeComponent();
}
private void btnSaveData_Click(object sender, RoutedEventArgs e)
{
myValue = txtSomeBox.Text;
this.Close();
}
}
Then, after your .ShowDialog() statement, you can grab that value and use it:
private void btnOpenModal_Click(object sender, RoutedEventArgs e)
{
ModalWindow modalWindow = new ModalWindow();
modalWindow.ShowDialog();
string valueFromModalTextBox = ModalWindow.myValue;
}
Window.Show will show the window, and continue execution -- it's a non-blocking call.
Window.ShowDialog will block the calling thread (kinda [1]), and show the dialog. It will also block interaction with the parent/owning window. When the dialog is dismissed (for whatever reason), ShowDialog will return to the caller, and will allow you to access DialogResult (if you want it).
[1] It will keep the dispatcher pumping by pushing a dispatcher frame onto the WPF dispatcher. This will cause the message pump to keep pumping.
Given a Window object myWindow, myWindow.Show() will open it modelessly and myWindow.ShowDialog() will open it modally. However, even the latter doesn't block, from what I remember.

XAML enable/disable view from another view

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.

How to effect change from child to parent window

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......

How to call the mainwindow object from a second object created from main window in c# .net

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;
}

Categories

Resources