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;
}
Related
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 a Mdiparent form containing a button and some child forms.
How is it possible to change backcolor of all textboxes in all child forms when clicking the button in parent form?
i know answer is already given.. but i would go with event and delegates..
multicast delegate is best choice is here
so here is my solution.
namespace winMultiCastDelegate
{
public partial class Form1 : Form
{
public delegate void ChangeBackColorDelegate(Color backgroundColor);
//just avoid null check instanciate it with fake delegate.
public event ChangeBackColorDelegate ChangeBackColor = delegate { };
public Form1()
{
InitializeComponent();
//instanciate child form for N time.. just to simulate
for (int i = 0; i < 3; i++)
{
var childForm = new ChildForm();
//subscribe parent event
this.ChangeBackColor += childForm.ChangeColor;
//show every form
childForm.Show();
}
}
private void button1_Click(object sender, EventArgs e)
{
ChangeBackColor.Invoke(Color.Black);
}
}
/// <summary>
/// child form class having text box inside
/// </summary>
public class ChildForm : Form
{
private TextBox textBox;
public ChildForm()
{
textBox = new TextBox();
textBox.Width = 200;
this.Controls.Add(textBox);
}
public void ChangeColor(Color color)
{
textBox.BackColor = color;
}
}
}
This the ChilForm;
public ChilForm()
{
InitializeComponent();
}
public void ChangeTextboxColor()
{
textBox1.BackColor = Color.Yellow;
}
And this is Parent;
ChilForm frm = new ChilForm();
public Parent()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Shows the child
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
//Changes color
frm.ChangeTextboxColor();
}
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");
My main form is a mdi container with a menu strip. When I choose Options-Maintenance I want another mdi to appear. This kind of works. Instead of another mdi container along with the design, a regular smaller form appears and not sure why.
public partial class mdiMain : Form
{
static string sTo = ConfigurationManager.ConnectionStrings["connectionTo"].ToString();
public myDataAccess3 data;
public mdiMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
data = new myDataAccess3(sTo);
frmLogOn frmLogOn = new frmLogOn(data);
if (frmLogOn.ShowDialog().Equals(DialogResult.Cancel))
{
frmLogOn.Close();
frmLogOn = null;
Application.Exit();
return;
}
frmLogOn.Close();
frmLogOn = null;
this.Focus();
}
catch (Exception e1)
{
MessageBox.Show("There was an error " + e1);
}
}
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.Show();
}
}
public partial class mdiMaintenance : Form
{
private myDataAccess3 data;
private mdiMain mdiMain;
public mdiMaintenance()
{
InitializeComponent();
}
public mdiMaintenance(mdiMain mdiMain, myDataAccess3 data)
{
// TODO: Complete member initialization
this.mdiMain = mdiMain;
this.data = data;
}
private void mdiMaintenance_Load(object sender, EventArgs e)
{
}
Thanks for the help
If the form is intended to be an MDI Child then you need to set the MdiParent property:
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.MdiParent = this;
maintenance.Show();
}
I want to see if notch50hzbutton is checked in another form, something like: if (SettingsForm.notch50hzbutton.Checked == true) ..... How can I do this?
namespace ClassLibrary1
{
using GraficDisplay;
using GraphLib;
using PrecisionTimer;
public partial class SettingsForm : Form
{
public SettingsForm()
{
InitializeComponent();
notch50hzbutton.Checked = false;
notch60hzbutton.Checked = true;
}
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
}
}
}
public bool Notch50HzIsChecked
{
get { return notch50hzbutton.Checked; }
set { notch50hzbutton.Checked = value; }
}
You may then access it like a regular property from outside the class.
Make a public property on the form and pass through the value you want to access externally?
A way to check to see if the control is checked without exposing the control itself is to add a public method (or a public property with just the getter) that does that to your SettingsForm.
public bool IsNotch50hzbuttonChecked()
{
return notch50hzbutton.Checked;
}
and then you can check
if (settingsFormInstance.IsNotch50hzbutton())
{
...
}
I would make a corresponding public property that's accessible to the outside:
public partial class SettingsForm : Form
{
public bool Is60Hz {get; private set;}
...
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
Is60Hz = false;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
Is60Hz = true;
}