Lock and Unlock textbox on form1, by command in form2 - c#

I have two Forms:
Form1
Form2
Whenever I check/uncheck a CheckBox checkBox1 on Form2 I want to update textbox1.Readonly that is on Form1. If both textbox1 and checkbox1 had been on the same Form it would be easy go:
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
textbox1.Readonly = checkBox1.Checked;
}
What shall I do in my case when textbox1 and checkbox1 are on different Forms?

You can put it like this:
public partial class Form1: Form {
...
// textBox1 is private (we can't access in from Form2)
// so we'd rather create a public property
// in order to have an access to textBox1.Readonly
public Boolean IsLocked {
get {
return textBox1.Readonly;
}
set {
textBox1.Readonly = value;
}
}
}
...
public partial class Form2: Form {
...
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
// When checkBox1 checked state changed,
// let's find out all Form1 instances and update their IsLocked state
foreach (Form fm in Application.OpenForms) {
Form1 f = fm as Form1;
if (!Object.RefrenceEquals(f, null))
f.IsLocked = checkBox1.Checked;
}
}
}

You should use events and delegates.
On Form2, We're create a delegate and event
public delegate void OnCheckedEventHandler(bool checkState);
public event OnCheckedEventHandler onCheckboxChecked;
public void checkBox1_Checked(object sender, EventArgs e)
{
if (onCheckboxChecked != null)
onCheckboxChecked(checkBox1.Checked);
}
And on Form1, we're realize this event:
void showForm2()
{
Form2 f2 = new Form2();
f2.onCheckboxChecked += onCheckboxChecked;
f2.Show();
}
public void onCheckboxChecked(bool checkState)
{
textBox1.ReadOnly = checkState;
}

For simplier and more flexible
Form1:
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 tmpFrm = new Form2();
tmpFrm.txtboxToSetReadOnly = this.txtMyTextBox; //send the reference of the textbox you want to update
tmpFrm.ShowDialog(); // tmpFrm.Show();
}
}
FOrm2:
public class Form2 : System.Windows.Forms.Form
{
public Form2()
{
InitializeComponent();
}
TextBox _txtboxToSetReadOnly = null;
public TextBox txtboxToSetReadOnly
{
set{ this._txtboxToSetReadOnly = value; }
get {return this._txtboxToSetReadOnly;}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if( this._txtboxToSetReadOnly != null) this._txtboxToSetReadOnly.ReadOnly = checkbox1.Checked;
/*
or the otherway
if( this._txtboxToSetReadOnly != null) this._txtboxToSetReadOnly.ReadOnly = !checkbox1.Checked;
*/
}
}

Related

Passing Values from one Form to another Form in a Button click

These are my 2 Forms.
These are the codes for Form 1-->
namespace Passing_Values
{
public partial class Form1 : Form
{
string a="preset value";
public Form1()
{
InitializeComponent();
}
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public void set(string p)
{
MessageBox.Show("This is Entered text in Form 2 " + p);
a = p;
MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
textBox1.Text = "Test 1";
textBox2.Text = a;
textBox3.Text = p;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(a);
}
}
}
These are the codes for Form 2-->
namespace Passing_Values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
Form1 j = new Form1();
j.set(g);
}
}
}
See the picture.You can understand the design.
This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
I would simply pass it in the constructor.
So, the code for form2, will be:
public partial class Form2 : Form
{
string _input;
public Form2()
{
InitializeComponent();
}
public Form2(string input)
{
_input = input;
InitializeComponent();
this.label1.Text = _input;
}
}
And the call in Form1 will be:
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2(this.textBox1.Text.ToString());
fm2.Show();
}
public partial class Form1 : Form
{
Form2 fm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2();
fm2.Show();
fm2.button1.Click += new EventHandler(fm2button1_Click);
}
private void fm2button1_Click(object sender, EventArgs e)
{
textBox1.Text = fm2.textBox1.Text;
}
}
And code in form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
set modifier property of textbox1 and button1 to public
Place a static string in your Form2
public static string s = string.Empty;
and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:
s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();
once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:
foreach (Control text in Controls)
{
if (text is TextBox)
{
((TextBox)text).Text = Form2.s;
}
}

Pass value from datagridview to textbox on other form

I have Form1 that has textbox; Form2(current form) has datagridview and button Choose. when i run Form1 that shows in new (nothing data that i wrote on form)
How can i pass value from Form2 to Form1 that keeps all data.
public void btnChoose_Click(object sender, EventArgs e)
{
Form1 form = new Form1;
form.txtMaKeHoach.Text = "value";
form.Show();
this.Close();
}
I hope this is what you are searching for...
Code for Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res != System.Windows.Forms.DialogResult.OK)
{
frm.Dispose();
return;
}
this.txtMaKeHoach.Text = frm.ChosenEntry;
frm.Dispose();
}
}
Code for Form2:
public partial class Form2 : Form
{
private string _ChosenEntry = "";
public Form2()
{
InitializeComponent();
}
private void btnChoose_Click(object sender, EventArgs e)
{
//...
_ChosenEntry = this.dataGridView1.SelectedCells[0].Value.ToString();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
public string ChosenEntry
{
get { return _ChosenEntry; }
}
}

Sending a value from opened form 2 to form 1

Sorry, I know this question, or similar has been asked frequently, but as I've gone through different threads, I just don't know how to apply it to my program.
Here's my situation:
In Form 1, I have a label. There's a button that opens Form 2, which has radiobuttons and a button. The button in Form 2 should send a string value from the radio button, to the label.Text in form 1. How can I go around in doing so?
So, below is what opened form 2.
private void selectkeyButton_Click(object sender, EventArgs e)
{
selectKeyboard sk = new selectKeyboard();
sk.ShowDialog();
}
And in Form 2, here's what i have so far:
public Form1 otherForm = new Form1();
string hotkey = "";
public void hotkeyChanged(object sender, EventArgs e)
{
RadioButton rr = (RadioButton)sender;
switch (rr.Name)
{
case ("buttonF1"):
hotkey = "F1 ";
break;
}
}
public void buttonConfirmKey_Click(object sender, EventArgs e)
{
hotkey = otherForm.keyLabel.Text;
this.Close();
}
Where I have public Form1 otherForm = new Form1();
and hotkey = otherForm.keyLabel.Text; I found it here.
And it doesn't seem to be working, as when I press the button on form2, the form closes but the label in form1 doesn't change...
any ideas?
thanks
There are different approaches to do this. You could go like this :
Solution one:
(Don't forget to set the modifier for you label1 in this case to Public. You can set this in the designer options > under Properties > design)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
Form 2:
public partial class Form2 : Form
{
private readonly Form1 _parent;
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
_parent.label1.Text = textBox1.Text;
Close();
}
}
Solution 2
Instead of setting label1 to public, leave it on private (as default) but set the DialogResult property of button1 on form 2 to "DialogResult OK" (under Properties > Behavior)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res == DialogResult.OK)
{
label1.Text = frm.MyNewText;
}
}
}
Form 2:
public partial class Form2 : Form
{
public string MyNewText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewText = textBox1.Text;
Close();
}
}
in the constructor of the forms you can get the values like this :
in form2 you should add a constructor like this :
public partial class Form2: Form
{
public string _newvalue
public Form2(string value)
{
InitializeComponent();
_newvalue=value
}
//you should assign the value to the label .
}
in form1 you should do this :
form2 new=form2("sampletext");
new.showdialog();
Solution 1:
In Form1:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
}
Codes in Form2:
Form frm_;
public Form2(Form frm)
{
InitializeComponent();
frm_ = frm;
}
private void btnInForm2_Click(object sender, EventArgs e)
{
Label lbl = (Label)frm_.Controls.Find("lblInForm1", true)[0];
string PassVal="What you want";
lbl.Text = PassVal;
}
Solution 2:
in Form 1:
Form2 f = new Form2();
if (f.ShowDialog() == DialogResult.OK)
{
lblInForm1.Text = f.PassVal;
}
in Form 2:
internal string PassVal = "";
PassVal is a Field.

Status variable

I'm programming a windows application (C#) that can only have 3 forms open.
When I click on the button of form1, form2 will open but it only open once.
I don't know why this is happening.
Can you please help me?
Thanks!
This is my code of form1:
public partial class Form1 : Form
{
bool form2Opend = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!form2Opend)
{
Form2 f2 = new Form2(this);
f2.Show();
form2Opend = true;
string data = this.textBox1.Text;
f2.TextInTextBox(data);
}
}
public void TextInTextBox(string text)
{
this.textBox1.Text = tekst;
}
public void putStatusToOff()
{
this.form2Opend = false;
}
}
And here is my code from form2
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 giveToForm)
{
f1 = giveToForm;
InitializeComponent();
}
public void TextInTextBox(string tekst)
{
this.textBox1.Text = text;
}
private void button1_Click(object sender, EventArgs e)
{
string dataFromForm2 = this.textBox1.Text;
f1.TextInTextBox(dataFromForm2);
f1.putStatusToOff();
this.Close();
}
}
In your button1_Click method you check a status variable form2Opend and only open the form if this variable is set to false. If you want multiple instances of Form2 you have to remove that check:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
string data = this.textBox1.Text;
f2.TextInTextBox(data);
}

Connecting a Control of Tools to another Form (void)

i already worked on connecting a tool control to another1 using this code:
--- Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
--- Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
if (timer1.Enabled == true)
{
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
txtMessage.Text = "line: " + line.ToString() + " , column: " + column.ToString();
}
}
}
***** output was**
text of label from Form2 was connected into Form1 .
so its already fixed .
now my problem was is there a way i can do the same way for void function?
i mean for example:
in Form1, i got 1button with a control inside of:
richTextBox1.Copy();
then this control will be for richTextBox1 on Form2 .
(which will copy the selected text in richtextbox on Form2)
is that possible? really need a help .thanks a lot in advance!
Here's something to get you started:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2(this);
}
}
And make sure that richTextBox1 is declared public.
And:
public partial class Form2 : Form
{
Form1 sendingForm;
public Form2(Form1 frm1)
{
InitializeComponent();
sendingForm = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
Text = sendingForm.richTextBox1.Text;
}
}
What's done here is: Initializing the Form2 instance with a reference to the sender Form1 instance, and using that reference to get to the RichTextBox.
EDIT:
Maybe (!) this is what you're looking for:
mainForm.richTextBox1.Copy();
You'd move your declaration of Form2 out to Class level:
--Form1
Form2 frm = null;
private void button1_Click(object sender, EventArgs e)
{
frm = new Form2(this);
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if (frm != null)
{
frm.CopyRichTextBox();
}
}
--Form2
public void CopyRichTextBox()
{
this.richTextBox1.Copy();
}

Categories