Connecting a Control of Tools to another Form (void) - c#

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();
}

Related

Changing text in one form, from another form

Im trying to change the text in Form1 when pushing the button on Form2
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textCh = "Text has been changed";
}
}
Form 1:
public partial class Form1 : Form
{
public string textCh {
get
{
return this.textCh;
}
set
{
this.label1.Text = value;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
When I'm pushing button nothing happens, the text remain the same.
Another method of passing Form1 to Form2 via the Show() method and the .Owner property:
// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"
Then, in Form2, you CAST .Owner to type Form1:
// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed)
{
f1.textCh = "Text has been changed";
}
There are several way to do this here 2 examples
This example use references whitout any use of events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show();
}
}
here the Form2 is created passing the Form1 as parameter
public partial class Form2 : Form
{
Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var lbl = (Label)_parent.Controls.Find("label1", false).First();
lbl.Text = "new text";
_parent.Update();
}
}
Than the Form2 use that for set the value wanted.
This example use events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
frm.Show();
}
private void Frm_UpdateLabelEvent(string str)
{
label1.Text = str;
}
}
and here the code of Form2
public partial class Form2 : Form
{
public event Action<string> UpdateLabelEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UpdateLabelEvent("new string value");
}
}
Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.
Solution
private void button1_Click(object sender, EventArgs e)
{
//Form1 f1 = new Form1(); GET rid of this line
f1.textCh = "Text has been changed";
}
you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.ShowDialog();
}
public Form2()
{
public Form1 f1 { get; set; }
...

Change the form.text programmatically with a button of other form

I can't update the first form of my application. when it opens it loads all the elements, then through a button I open a second form and from that, with a button I should reload all the controls of the first form including form1.text but this does not happen. despite the marker I saw that the text variable is updated correctly, however on a graphic level it does not change.
form1:
public partial class Form1 : Form
{
public string mail { get; private set; }
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
this.Text = "table - Last Update: " + DateTime.Now.ToString();
...some other code...
}
public void updateform()
{
this.Controls.Clear();
InitializeComponent();
Form1_Load(null, null);
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show();
}
}
form2:
public partial class Form2 : Form
{
public Form2(String stringa)
{
InitializeComponent();
email = stringa;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.updateform();
this.Close();
}
You can pass a reference to Form1, into Form2 using the Show() command. The reference can be accessed using the .Owner property.
In Form1:
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show(this); // <-- pass reference to Form1
}
In Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = this.Owner as Form1; // <-- attempt to convert .Owner to Form1
if (f1 != null)
{
f1.updateform();
}
this.Close();
}

form1 string is don't pass to form2

want to make form1 pubId value changed from form2. pubId is always get null didn't change. How can I solve this problem?
Form1 code:
public string pubId = string.Empty;
public void button1_Click(object sender, EventArgs e)
{
try
{
form2 _frm2 = new form2();
_frm2 .FormClosed += _frm2_FormClosed;
}
catch (Exception ex)
{
MessageBox.show(ex);
}
}
private void _frm2_FormClosed(object sender, FormClosedEventArgs e)
{
if (pubId == "8")
{
MessageBox.show("works");
}
}
Form2 code:
public void buttonsend_Click(object sender, EventArgs e)
{
idfrm2 = "8";
form1 _frm1 = new form1 ();
_frm1.pubId = _idfrm2;
this.Close();
}
In this line you create a new form with new empty pubId.
form1 _frm1 = new form1 ();
Just create a constructor for form2 to pass it.
For example:
private string _pubId;
public form2(string pubId)
{
_pubId = pubId;
}
Then you can use it:
form2 _frm2 = new form2(pubId);
You can maintain reference to Form2 inside a List of controls maintained by Form1 and access it as below :
Form1 Code :
public partial class Form1 : Form
{
Form2 localfrm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosed += Frm2_FormClosed;
localfrm2 = frm2;
frm2.Show();
}
private void Frm2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show(localfrm2.PubId);
}
}
Form 2 :
public partial class Form2 : Form
{
public string PubId { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PubId = textBox1.Text;
}
}
Upvote if this solves your problem

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);
}

Categories