How to get data from textbox that is in another window - c#

i have a main window and when users click a button i create a new window to ask informations.
But after how i get these informations in my main window????
//This is the button event handler in main window
private void Button_Click(object sender, RoutedEventArgs e)
{
AddServer window = new AddServer();
window.Show();
}
thank you

Related

WPF - Go back to first window from second window

I have a MainWindow page and a LoadingWindow. The MainWindow has a button that closes itself and opens LoadingWindow. What I want is when the user closes the Loading window to go back to the MainWindow as a new instance.
MainWindow.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
LoadingWindow load = new LoadingWindow(calibration , "MainWindow" , this);
Mouse.OverrideCursor = null;
Application.Current.MainWindow.Close();
load.ShowDialog(); // Exception is here the next time it is called
}
LoadingWindow.xaml.cs
private void Close_Button(object sender, RoutedEventArgs e)
{
MainWindow main = new MainWindow();
this.Close();
main.ShowDialog();
}
Now when I try to close the Loading window and press the Button_Click, the following error shows up at the load.ShowDialog() although I am declaring a new instance of it.
System.InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed
I read that you cannot open a window after you closed it, but I am having a new instance which should not make this problem.
On your main window don't use the Application.Current.MainWindow instance, use this.Close() instead.
private void Button_Click(object sender, RoutedEventArgs e)
{
LoadingWindow load = new LoadingWindow(...);
Mouse.OverrideCursor = null;
//Application.Current.MainWindow.Close();
this.Close();
load.ShowDialog(); // Exception is here the next time it is called
}
Refer to this thread & this for clarification on the difference.

Closing wpf window before opening a new one

I have a button that opens a WPF window which shows some calculation.
public void SomeButton_Click(object sender, RoutedEventArgs e)
{
Window1 win2 = new Window1();
win2.Show();
win2.Topmost = true;
}
I have a problem that if I change values in the program and press the button again, I have two open windows.
Is there a way that I can close win1 before opening the new version of win2?
I tried with win2.Close(), but since win2 is not known before the application creates it won't obviously work.
Thanks.
You could store a reference to the window in a variable in your class:
private Window2 win2;
public void SomeButton_Click(object sender, RoutedEventArgs e)
{
win2?.Close();
win2 = new Window2();
win2.Show();
win2.Topmost = true;
}
This will close any previous instance of Window2 that you have created and opened in the click event handler before you open a new window.

How do I focus on a form after entering a textbox

I'm not sure if focus is the right word but I have an on key up event on my form that will open a new form and close the current form, however after i enter a textbox or other such object i can't re select the form to be able to activate the key up event
This is code a I am using currently when i click on my form, to try select my currently open form however it does not close the current form when i active the key up event, when i do it this way
private void frmLevel1_Click(object sender, EventArgs e)
{
this.BackColor = GlobalClass.BG;
frmLevel1 lvl1 = new frmLevel1();
lvl1.Select();
}
I'm not 100% sure, but you have a few different questions. I hope it helps.
// Button click event
private void button1_Click(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Form load event
private void Form1_Load(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Close the current form and open another one. Use any event what you want
private void button1_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
// Hide the current form. If you close it it will dispose of all further events
this.Hide();
// Open the new form
frm.Show();
// Close the current form
this.Close();
}
}

Click button and open same window again

In my WPF application, I have one main window (Window.xaml). Which has a button, if user click the button we want to open same window again(Window.xaml). Once again the user clicks the same button we want to open the same window again.
so how do I open the same window again?
You can create new instance of your current window in button click event and achieve it.
Code snippet:
private void button_Click(object sender, RoutedEventArgs e)
{
var currentWindow = new Window();
currentWindow.Show();
}
You have to create a new instance of the window (e.g. MainWindow class) and call the show method.
Just some pseudo code:
void Button_clicked()
{
new MainWindow().Show();
}

How to navigate between windows in WPF?

I have tried to set up a click event for a button that opens another window,but the error I'm getting at NavigationService is that the project doesn't contain a definition for it.
This is how I'm trying to call the page at present:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("TrainingFrm.xaml", UriKind.RelativeOrAbsolute));
}
Can someone point me in the right direction with this or show alternatives to this method for window navigation?
NavigationService is for browser navigation within WPF. What you are trying to do is change to a different window TrainingFrm.
To go to a different window, you should do this:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
var newForm = new TrainingFrm(); //create your new form.
newForm.Show(); //show the new form.
this.Close(); //only if you want to close the current form.
}
If, on the other hand, you want your WPF application to behave like a browser, then you would need to create Pages instead of Forms, and then use a Frame in your application to do the navigation. See this example.
If you want to navigate from Window to Window:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
Window1 window1 = new Window1();
// window1.Show(); // Win10 tablet in tablet mode, use this, when sub Window is closed, the main window will be covered by the Start menu.
window.ShowDialog();
}
If you want to navigate from Window to Page:
private void conditioningBtn_Click(object sender, RoutedEventArgs e)
{
NavigationWindow window = new NavigationWindow();
window.Source = new Uri("Page1.xaml", UriKind.Relative);
window.Show();
}
In order to use NavigationService you should use the Page and not the Window class

Categories