I Need pass the Value to child Window.In child window there is two Text boxes.I need to show the value in child window Text box while while its getting opened.
I tried the following way,My Child window class as follows,
public partial class ChildWindow:ChildWindow
{
public int abc {get;set;}
public string value{get;set;}
public ChildWindow()
{
InitializeComponent();
this.txtbox1.Text = abc ;
this.txtbox2.Text = value;
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
My Parent Window as follows,
private void EditButton_Click(System.Object sender, System.Windows.RoutedEventArgs e)
{
ChildWindow child= new ChildWindow();
child.abc = 1;
child.alue = "Hello"
child.show();
}
How can I show the child window controls with values (which is getting from Parent) while its getting opened?
You can change the following:
public int abc {get;set;}
public string value{get;set;}
To:
public int abc
{
get
{
int result = 0;
int.TryParse(this.txtbox1.Text, out result);
return result;
}
set
{
this.txtbox1.Text = value;
}
}
public string value
{
get
{
return this.txtbox2.Text;
}
set
{
this.txtbox2.Text = value;
}
}
The problem in your code is, the properties are being assigned during initialization of the control. Not when the properties has been changed.
You can create an overload of constructor.
public ChildWindow(string abc,string value)
{
InitializeComponent();
this.txtbox1.Text = abc ;
this.txtbox2.Text = value;
}
Than create object of childwindow like this
ChildWindow child= new ChildWindow("abc","somevalue");
Related
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 have Parent_1, Parent_2, Parent_3 and etc. and have one Child form
in Parent_1, Parent_2, Parent_3 ... I have:
private string strText;
public string pubText {
get { return strText; }
set { strText = value; } }
on button:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this);
form.ShowDialog();
}
in child form I have the code:
private Parent_1 logicalParent;
public fChild(Parent_1 parent)
{
InitializeComponent();
logicalParent = parent;
this.FormClosed += new FormClosedEventHandler(child_FormClosed);
}
and
void child_FormClosed(object sender, FormClosedEventArgs e)
{
logicalParent.pubText = this.textBox1.Text;
}
This works only for 1 parent form, How Can I use this for other Parent forms???
Please Help
You can create a common interface for the three parent forms:
public interface IParentForm
{
string PubText {get; set;}
}
public class Parent_1 : Form, IParentForm
{
public string PubText
{
get { return this.pubText; }
set { this.pubText = value; }
}
}
//same for Parent_2 and 3
then in your child form declare logicalParent to be of type IParentForm, and change the constructor of the child form to be public fChild(IParentForm parent)
At the button click do the following:
private void btbutton1_Click(object sender, EventArgs e)
{
var form = new fChild(this); // this is not more needed
form.ShowDialog();
pubText = form.pubText;
}
I'm trying to make my own custom input dialogue by designing a form. How would I initialize it so that once I press OK, I can receive the value of the textbox in it, back to where I initially called it?
You can create a form that exposes a property like this:
public class InputDialog:Form
{
public string Result { get; set; }
private void OK_Click(object sender, EventArgs e)
{
Result = txtResult.Text;
this.Close();
}
}
And in your base form you do:
var dialog = new InputDialog();
dialog.ShowDialog();
string Result = dialog.Result;
You can use events for communication between forms. This way InputForm hides logic, properties from outside world.
public class InputEventArgs : EventArgs
{
public string Input { get; private set; }
public InputEventArgs(string input)
{
Input = input;
}
}
public class InputDialog : Form
{
public EventHandler<InputEventArgs> InputSet;
private void OkClick(object sender, EventArgs e)
{
var ev = InputSet;
if (ev != null)
{
ev(this, new InputEventArgs(txtInput.Text));
}
}
}
and in your calling form:
private void ShowInputForm()
{
using (var frm = new InputDialog())
{
frm.InputSet += (s, e) =>
{
txtResult.Text = e.Input;
}
frm.ShowDialog();
}
}
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; }
}
All.
I want to receive checkbox1 data from form1 to form2. I used get and set methods to receive meaning of variable. I used this code for this. But it didn't work. Why? Where is the problem?
form1.cs
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 test = new Form2();
test.checkBox1 = checkBox1.Checked;
test.Show();
}
}
}
form2.cs
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private bool data7;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
set { value = data7; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
set { value = data7; }
should be
set { data7 = value; }
Your set method is wrong. It should be
set { data7 = value; }
Form2 apparently stores the value in the variable data7, but not in a CheckBox. You would have to do something like this to actually store it in a CheckBox
public bool checkBox1
{
get { return myCheckBox.Checked; }
set { myCheckBox.Checked = value; }
}
Another problem is returning the result of user input to Form1. Since you call Form2 with test.Show(); the code after this statment continues immediately, without wating for Form2 to close. Call test.ShowDialog(); instead.
Another option for returning a result while not blocking Form1 is to use an event. Using this definition
public class Form2ResultEventArgs : EventArgs
{
public Form2ResultEventArgs(bool checked)
{
this.Checked = checked;
}
public bool Checked { get; private set; }
}
In Form2 you would define an event like this.
public event EventHandler<Form2ResultEventArgs> Form2Result;
private OnForm2Result(bool checked)
{
var handler = Form2Result;
If (handler != null) {
handler(this, new Form2ResultEventArgs(checked));
}
}
// Assuming that you have a OK button on Form2
private OkButton_Click (...)
{
OnForm2Result(myCheckBox.Checked);
this.Close();
}
In Form1
var test = new Form2();
test.Form2Result += ProcessResult;
test.Show();
...
private void ProcessResult(object sender, Form2ResultEventArgs e)
{
bool result = e.Checked;
...
}
UPDATE
If you only want to set a label, why not just do this
In Form2
public void SetDisplay(bool value) {
label1.Text = value.ToString();
}
In Form1
var test = new Form2();
test.SetDisplay(checkBox1.Checked);
test.Show();
Note that InitializeComponent is called in the constructor of Form2 and therefore the label exists after new Form2(). No need to do that in Form2_Load.
I would like to suggest that when the bool data7 is created that is set a value of false, or true I don't care, cause when the form is first loaded if the user has not set CheckBox1 the Form will crash. And Yes I know the OP, in the Code given, did set the variable.
private bool data7 = false;
I have included Fur Dworetzkys Answer in my suggestion Below
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
//give the variable a default value
private bool data7=false;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
//Here is Furs Correction
set { data7 = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
if you are not doing anything (e.g. validation) with data7 in get or set, then just get rid of them.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool checkBox1 { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
if (checkBox1)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}