I changed the startup window at the app.xaml with this code:
Startup="ApplicationStart"
At the app.xaml.cs file is this method:
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
ChooseAccountWindow chooseAccountWindow = new ChooseAccountWindow();
chooseAccountWindow.ShowDialog();
}
The Code of the window (ChooseAccountWindow()):
public partial class ChooseAccountWindow : MetroWindow
{
public ChooseAccountWindow()
{
InitializeComponent();
}
private void btnDastaschentuch2013_Click(object sender, RoutedEventArgs e)
{
//send value "dastaschentuch2013" to the main window
}
private void btnSkeptar_de_Click(object sender, RoutedEventArgs e)
{
//send value "skeptar_de" to the main window
}
private void btnAsdf_de_Click(object sender, RoutedEventArgs e)
{
//send value "asdf_de" to the main window
}
}
If I press one of the button`s, then a value should be send to the main code. How can I do that?
Answer
I had to change the MainWindow.xaml.cs code:
namespace EbayManager
{
public partial class MainWindow : MetroWindow
{
private string selectedAccount;
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string selectedAccount): this()
{
this.selectedAccount = selectedAccount;
}
}
}
public partial class ChooseAccountWindow : MetroWindow
{
public string Result { get; set; }
public ChooseAccountWindow()
{
InitializeComponent();
}
private void btnDastaschentuch2013_Click(object sender, RoutedEventArgs e)
{
this.Result = "dastaschentuch2013";
this.Close();
}
private void btnSkeptar_de_Click(object sender, RoutedEventArgs e)
{
this.Result = "skeptar_de";
this.Close();
}
private void btnTrachsel_de_Click(object sender, RoutedEventArgs e)
{
this.Result = "trachsel_de";
this.Close();
}
}
In App.xaml remove StartupUri to prevent automatical main window opening
In App.xaml.cs:
private void ApplicationStart(object sender, StartupEventArgs e)
{
Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
ChooseAccountWindow chooseAccountWindow = new ChooseAccountWindow();
chooseAccountWindow.ShowDialog();
MainWindow main = new MainWindow(chooseAccountWindow.Result);
// insert your startup uri class name instead of MainWindow;
// add constructor to this window that will take string as input parameter
main.Show();
}
If I read you correctly, you can reference the main window like this:
Application.Current.MainWindow and then set a property on it.
void Button_Click(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow.MyProperty = "SomeValue";
MainWindow.ShowDialog();
this.Close();
}
Related
I made a simple GUI, in which I would like to add an option where I can switch pages, it is the same page when I open it, I would like a bar where I click and switch pages
namespace POS
{
public partial class Form1 : Form
{
private object gunaLabel_date;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Guna.UI.Lib.ScrollBar.PanelScrollHelper flowphelper ;
gunaLabel_date = DateTime.Now.ToLongDateString();
}
private void gunaVScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
private void userControl17_Load(object sender, EventArgs e)
{
}
}
}
i have big problem visual studio wpf , when i try to send any string it doesn't work
i tried to write public string str { set; get; } This problem has been stuck with it for a long time, I tried a lot to use crooked methods, but the program or project crashes.
im using visual studio wpf c#
thank you for answer
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
Info.Content = "Hello world";
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Frame.Content = new Page1();
}
private void button2_MouseLeave(object sender, MouseEventArgs e)
{
Info.Content = "Label";
}
private void button1_MouseLeave(object sender, MouseEventArgs e)
{
Info.Content = "Label";
}
}
here Page class
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
}
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = new MainWindow();
mw.Info.Content = "Test Public";
}
}
Update Page1's button1_MouseEnter code to:
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = Application.Current.Windows.OfType<MainWindow>().First();
mw.Info.Content = "Test Public";
}
or
private void button1_MouseEnter(object sender, MouseEventArgs e)
{
MainWindow mw = (MainWindow)Application.Current.MainWindow;
mw.Info.Content = "Test Public";
}
This question already has answers here:
Change a textbox's text value on a UserControl from a Window in WPF
(2 answers)
Closed 3 years ago.
Ok, I have two windows in my WPF app. I want to change the text of a textbox, from a second window. This should also happen parallel.
I know, this is about multithreadin, but I know very little about it.
This is what my current code looks like, but this is for copying textbox text.
private void copyButton_Click(object sender, RoutedEventArgs e)
{
secondWindow = new SecondWindow();
secondWindow.textBoxS.Text = textBox.Text;
secondWindow.Show();
}
But, I want to change textbox texts dynamically between the MainWindow and the Second window.
So I tried something like this:
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
Task t = Task.Run(() =>
{
secondWindow = new SecondWindow();
secondWindow.textBoxS.Text = textBox.Text;
secondWindow.Show();
});
t.Start();
}
There are several ways to do this. I put two way in below:
Method 1
You can create a public method (e.g. SetTextBoxValue) and
pass the window that contains the TextBox to other window. Then change the TextBox value using that method. like this:
MainWindow
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
public void SetTextBoxValue(string value)
{
SampleTextBox.Text = value;
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
var otherWindow = new AnotherWindow(this);
otherWindow.Show();
}
}
Other Window
public partial class AnotherWindow
{
private readonly MainWindow _mainWindow;
public AnotherWindow(MainWindow mainWindow)
{
_mainWindow = mainWindow;
InitializeComponent();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
_mainWindow.SetTextBoxValue("New value from other window");
}
}
Method 2
You can create a event for other window and subscribe to it in main window. like this:
MainWindow
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
var otherWindow = new AnotherWindow();
otherWindow.TextBoxValueChanged += OtherWindowOnTextBoxValueChanged;
otherWindow.Show();
}
private void OtherWindowOnTextBoxValueChanged(object sender, TextBoxValueEventArgs e)
{
SampleTextBox.Text = e.NewValue;
}
}
Other Window
public partial class AnotherWindow
{
public event EventHandler<TextBoxValueEventArgs> TextBoxValueChanged;
public AnotherWindow()
{
InitializeComponent();
}
private void OnButtonClick(object sender, RoutedEventArgs e)
{
var newValue = "New value from other window";
OnTextBoxValueChanged(new TextBoxValueEventArgs(newValue));
}
protected virtual void OnTextBoxValueChanged(TextBoxValueEventArgs e)
{
TextBoxValueChanged?.Invoke(this, e);
}
}
public class TextBoxValueEventArgs : EventArgs
{
public string NewValue { get; set; }
public TextBoxValueEventArgs(string newValue)
{
NewValue = newValue;
}
}
OUTPUT
try this, initialize the second window in first window constructor
private SecondWindow _secondWindow;
public FirstWindow()
{
_secondWindow = new SecondWindow();
}
and your second form before constructor
private string text;
public string Text
{
get
{
return text;
}
set
{
textBoxOfSecondWindow.Text = value;
text = value;
}
}
then in the copybutton function
private void copyButton_Click(object sender, RoutedEventArgs e)
{
_secondWindow.Text= textBox.Text;
_secondWindow.Show();
}
in the textchange of the first window
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
_secondwindow.Text = textBox.Text;
}
in your second window, place this code in the textBox_TextChanged method.
((MainWindow)Application.Current.MainWindow).txtFirstWindow.Text = txtSecondWindow.Text;
I work a lot of page. When i return the previous page, code reset the page. I want to see previous page. How can i this?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void BtnClickP1(object sender, RoutedEventArgs e)
{
Main.Content = new KK();
}
private void BtnClickP2(object sender, RoutedEventArgs e)
{
Main.Content = new AIRFOIL();
}
private void BtnClickP3(object sender, RoutedEventArgs e)
{
Main.Content = new OB();
}
}
I use new command for every tab page. I think it is wrong. I want to see existing page when i push button. Thanks for helping.
You have to use variable if you want to store the modification in the page.
public partial class MainWindow : Window
{
private KK _kkPage;
private AIRFOIL _airfoilPage;
private OB _obPage;
public MainWindow()
{
InitializeComponent();
_kkPage = new KK();
_airfoilPage = new AIRFOIL();
_obPage = new OB();
}
private void BtnClickP1(object sender, RoutedEventArgs e)
{
Main.Content = _kkPage;
}
private void BtnClickP2(object sender, RoutedEventArgs e)
{
Main.Content = _airfoilPage;
}
private void BtnClickP3(object sender, RoutedEventArgs e)
{
Main.Content = _obPage;
}
}
I have two windows, windowA that has a button to open windowB, and windowB has a button to close itself and also return List value. I tried this code, but the value keep null. windowB has RadGridView control, i want to get the selectedItem from it and add it on a list.
public class WindowA : Window
{
...
private void button_Click(object sender, RoutedEventArgs e)
{
WindowB winB = new WindowB();
if (winB.ShowDialog() == false)
{
listClass lc = winB.SelectedItemButton;
...
}
}
}
public class WindowB : Window
{
...
public listClass SelectedItemButton
{
get { return selectedItem; }
set
{
selectedItem = ((listClass)AGridView.SelectedItem);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
the results are a listClass, but has no value inside. Why? and how can i make selectedItem = ((listClass)AGridView.SelectedItem); this line works to another window?
An example for you:
public partial class MainWindow : Window
{
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 dlg = new Window1();
if(dlg.ShowDialog()??false)
{
MessageBox.Show(dlg.S);
}
}
}
// Dialog
public partial class Window1 : Window
{
public string S
{
get
{
return this.txt1.Text;
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
you should create your listClass variable in Window1 and while you are opening the Window2 you should pass this variable as a parameter. Here is my demo:
First window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestClass testClass = new TestClass();
testClass.Test = "Initial";
Second second = new Second(testClass);
second.ShowDialog();
label.Content = testClass.Test; // It prints "Changed"
}
}
Second window:
public partial class Second : Window
{
TestClass testClass;
public Second(TestClass sent)
{
InitializeComponent();
testClass = sent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
testClass.Test = "Changed"; // Change the value
}
}
My testClass (listClass):
public class TestClass
{
public string Test { get; set; }
}