Adding text to RichTextBox in new form in - c#

I have two forms, let's call them Main and Form2.
Main form consists of a button and Form2 consists of a RichTextBox.
What I want my program to do is when I click button in the main form it calls function in class DoSomeWork.Do(). Function Do() gets some text from file, it has to open Form2 and paste this text to that RichTextBox.
Problem is that I don't know how to "access" this RichTextBox and paste text to it.
Thanks in advance.

you can create a property on form 2
public string RichTextboxText
{
get
{
return this.RichTextBox1.Text
}
set
{
this.RichTextBox1.Text = value;
}
}
Then create a new form:
Form2 f2 = new Form2() { RichtTextBoxText = "I like big butts"; }
f2.Show();
Something like this should works
[edit]
just like to add that in this way you can also get the value back to from one.
in form one at any random point you can do:
string RichtEditTextFromForm2 = f2.RichTextBoxText;
given f2 is still active atleast

In Form2 you add a method
public void InsertText(string text)
{
richTextBox1.Text = text;
}
to use the method you open Form2 like this:
Form2 f2 = new Form2();
f2.InsertText("hello world");
f2.Show();

You can pass text values through Constructor.
Eg:
create parameterised constructor for Form2
Public Form2(string str)
{
this.Value=str;
InitializeComponent();
}
NOTE: Value is a public string in form Form2 .and you can set this value to richTextBox in form loading.
richTextBox1.Text=Value;

Related

How can I pass data of datagridview to textboxes in other Form?

Datagridview is located in Form2, TextBoxes in Form1.
Call the Form 2 from Form1 with Show(); where is located dataGridView and then pass this information to textboxes in Form1.
Code Sample in Form2 :
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Form1 exportar = new Form1();
exportar.textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
exportar.comboBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
exportar.textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
exportar.textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
exportar.textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString();
exportar.dateTimePicker1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
exportar.dateTimePicker2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
exportar.textBox7.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[7].Value.ToString();
exportar.textBox8.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[8].Value.ToString();
exportar.textBox9.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[9].Value.ToString();
exportar.textBox10.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[11].Value.ToString();
}
This did not work, but when I place exportar.Show() passed the information. The problem is that doubles the Form1.
You need a reference of Form1 in Form2. You can pass it in the constructor of Form2
private Form1 _form1;
public Form2 (Form1 form1)
{
_form1 = form1;
}
You create and open Form2 like this from within Form1:
var form2 = new Form2(this);
form2.ShowDialog(this);
In order to be able to access the controls of the other form, you must change their Modifer to Internal in the properties window.
then you can set the values like this:
var row = dataGridView1.CurrentRow; // This is "the row".
// No detour through the index is necessary.
_form1.textBox1.Text = row.Cells[0].Value.ToString();
_form1.comboBox1.Text = row.Cells[1].Value.ToString();
But things get simpler if you use data binding. See: A Detailed Data Binding Tutorial
1.Pass it as cunstrctor parameter:
public Form2(string text){
Textbox1. text = text;
}
and
Form2 f = new Form2("something to send to the form");
f.Show();
2.Create a public property for Form2:
public string TheText {get{return TextBox1.Text;}; set {textBox1.Text = value;};}
and then from first form:
Form2 f = new Form2();
f.TheText = "Some text";
f.Show();
Either pass the data in the constructor of your other form, if it's mandatory. Or make a public method available in your other form that allows you to set the data separately.
E.g.
public void setTextBoxData(String text) { ,etc, etc }
You can then call that method on your second form, passing the value you require from the first form.

C# passing data between forms without ShowDialog()

I have a MainForm which has a Textbox and a Button in it. Then I have a second form with a single button. On program start, the MainForm is initialized and when I click the button, the second form shows up (ShowDialog()) still keeping open the MainForm.
So I have these two forms opened next to each other. The thing I want is, that when I click the button, the button will send a string to the MainForm. MainForm will take the text and display it in it's textbox. But I want to make the change happen immediately - without hiding and showing the MainForm again. Sort of like refresh it, when the button on the second form is pressed.
How can I do that?
Note: It's important to have the text, which is send to the MainForm, have declared in the second form. (In my program, the text is dynamically being changed on the second form level)
Try sending the TextBox to the constructor of the second form and the second form, when you give click the button, change the Text property of the TextBox and it will appear as if it will be updated as they are referring to the same place.
public partial class Form1 : Form
{
public Form1(TextBox txt)
{
InitializeComponent();
this.txt = txt;
}
//variable
TextBox txt = null;
private void button1_Click(object sender, EventArgs e)
{
txt.Text = "Your text";
}
}
If I correctly understand, you need create a property in the winform.
ex:
public partial class frmLogin : Form
{
public bool LoggedIn
{
get { return loggedIn; }
}
public frmLogin()
{
InitializeComponent();
}
}
// Now, in your forms, you can do.
frmLogin frm = new frmLogin ();
frm.ShowDialog();
var value = frm.LoggedIn;

how to change a control property from another form

I have two forms in a windows form application. lets call them "first form" and "second form".
i want by clikcing on a button on second form, change the property of one of the controls of the first form. i've defined an event for this. by that i mean when i click on the second form's button, a method within the first form is called. here's the method:
// changes the visibility of the specified control
public void change_visibility()
{
this.new_customer_label.Visible = true;
}
but when i set a breakpoint on this method and check the value after it is executed. the property has't changed. what is wrong? thanks in advance
note: on the second form button's click event, i also close the form.
So firstly, Open up Form1.designer.cs and change the control to public
Form1 this will open Form 2.
Form2 frm2 = new Form2();
frm2.Owner = this;
frm2.Show();
Form2 this will change the property of a control in Form 1
(this.Owner as Form1).label1.Visible = true;
Here is an example of what you can do:
class Form1 : Form {
private Label labelInForm1;
public string LabelText {
get { return labelInForm1.Text; }
set { labelInForm1.Text = value; }
}
}
class Form2 : Form {
Form1 form1; // Set by the property
private Form1 Form1 {
get { return form1; }
set { form1 = value; }
}
private ChangeVisibility()
{
Form1.labelInForm1.Visible = true;
}
}
"note: on the second form button's click event, i also close the form."
Then it would probably be a better design to display the second form with ShowDialog() instead of Show(). Something like:
Form2 f2 = new Form2();
f2.ShowDialog(); // code STOPS here until "f2" is closed
this.new_customer_label.Visible = true;
By default, the designer generates code in the 'Form1.Designer.cs' class. In there, you can see that all the controls are set private, change them to public and then try again...
As you will search this problem on internet you will find different solutions but I think the best solution is to make controls public then you can access these controls from any form.
Follow these instructions.
Open your desire form whose control properties you want to access
Open Form.Designer.cs
Change the desire control class to public
Go to the main form where you want to access or change property
Write this code
Form Form2 objForm=new Form2();
Now access your control property here
objForm.new_customer_label.Visible=true;
I hope this will be helpful to you!!!

Changing a label's text in another form in C#?

I have a label called LabelX1. This is on form2. On form1, i have a button. I want the button's text to be transferred to the other form's label. I have tried
form2 frm2 = new form2();
frm2.labelX1.Text = this.button1.text;
But it does not work. Is there an easy, straight forward way of doing this?
You need to expose your label or its property.
In form 2:
public string LabelText
{
get
{
return this.labelX1.Text;
}
set
{
this.labelX1.Text = value;
}
}
Then you can do:
form2 frm2 = new form2();
frm2.LabelText = this.button1.text;
You could modify the constructor of Form2 like this:
public Form2(string labelText)
{
InitializeComponent();
this.labelX1.Text = labelText;
}
then create Form2 passing in the text:
Form2 frm2 = new Form2(this.button1.text);
Or you can do this >>
((Label)frm2.Controls["labelX1"]).Text = "test";
inside form2 write this
public void ChangeLabel(string s)
{
labelX1.Text = s;
}
then where you create Form 2 do this
form2 frm2 = new form2();
frm2.ChangeLabel(this.button1.text);
Is there an easy, straight forward way of doing this?
Easiest way is to make labelX1 a public member of form2. The issue you're having is because from Form1 code form2.labelX1 isn't visible. In form2 designer you can go to properties of labelX1 and set it's visibility to public/internal.
Better approach would be to expose labelX1.Text as a property which can be set in code outside the class.
form2 frm2 = new form2();
((Label)frm2.Controls["labelX1"]).Text=button1.Text;
frm2.Show();
the only think you have to do is to put the label of the other form as public
for instance:
Form1:
public System.Windows.Forms.Label txtInfo;
then in Form2
Form1 frm =new Form1();
frm.txtInfo.text="....."//you have access because is public
You can make labelX1 public, and it will work, but there is a better way to do this:
http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
I changed my parent window property to the following code:
this.MdiParent.Controls["label1"].Text = "test";
If you are needing to access the form2 from elsewhere in your code (like a button press for instance) you will not be able to see the instance of the form you create. To solve that I create a public instance to hold a reference to it like:
public form2 form2_pub;
Then after you create it you assign the new one to your public instance:
form2 frm2 = new form2();
frm2.Show();
form2_pub = frm2
Now you can reference form2_pub throughout your routines.
Works for me at least.
Remember, in your setter you can run whatever other code you want.
For instance, I use the following to show what I want on another form by just setting show_scanning to true:
public bool show_scanning //turns on the scanning screen
{
set
{
scanning_pnl.Visible = true;
notReady_pnl.Visible = false;
timer1.Enabled = true;
}
}
Generally the controllers are private. That is why you unable to access it from another form. Above mentioned ways such as passing data through parameters etc are so correct. There is one another method,
Go to your form Form2.Designer.cs
private System.Windows.Forms.Label labelX1;
Change 'private' into 'public'.
Now the labelX1 is visible to outside.
The another approach is
Just change the modifier property of label or text to public and now it allows to access the content of form1 textbox to label on another form
So the code is
private void button1_click(){
Form2 obj1 =new Form2();
Obj1.show();
Obj1.label1.text="welcome"+textbox1.Text;
}
Do you have exception? You can make public property on form2, with setter to set text on the label, or make labex1 access modifier public and set it directly. It should work.

Append text in textbox on another form?

I have 2 forms. On Form2, I have a textbox and a button. When I press the button, I want it to append the text in the textbox to a textbox on Form1. Here's my code:
On Form2:
private void button1_Click(object sender, EventArgs e)
{
frm1.AppendTxt(textBox1.Text);
this.Close();
}
On Form1:
public void AppendTxt(string text)
{
this.body.AppendText(text);
MessageBox.Show(body.Text);
}
For some reason, the text isn't showing up in the textbox on Form1. However, the message box that pops up shows the text I input on Form2.
Try body.Text += " " + text;
Most likely you're changing the textbox inside a hidden copy of Form1, not the instance that's displayed on the screen.
Try passing the Form1 handle to Form2's constructor, and saving it in a class member variable (also called field) to use inside the event handler.
Passing values from form to form.
You can try this. first you can declare a variable that all your forms can access
like this one,
string yourValue;
PS: declare that your class
From form1, you can pass a value to that class.
like this,
YourClass.yourValue = textbox1.text;
Once have a value. pass it to your second form like.
textbox2.text = YourClass.yourValue;
Hope it helps you. :D
You have two form: form1 and form2. Two form is opening. In form2 you have one textbox(txt2) and one button(btn2). In form1 you have one textbox(txt1). When user type something on txt2 and click btn2, the text in txt2 will be append to txt1. This is what you want?
If yes:
In form2, you should create a delegate:
// Declare a delegate
public delegate void GetValue(string value);
// Declare event with delegate
public event GetValue btn2_Clicked;
Then, in form2, you create a function to handle that event: Name of function is the same with name of event you just declare and add prefix "On" in it. Like this:
public void Onbtn2_Clicked(string value)
{
if (btn2_Clicked != null)
{
btn2_Clicked(value);
}
}
After that, in btn2 click event, rise your event just created:
private void btn2_Click(object sender, EventArgs e)
{
Onbtn2_Clicked(txt2.Text);
}
Ok, that is done in form2. Comeback form1 to finish:
I assume form2 is opened when user click a button(btn1) in form1, so in btn1 click event:
private void btn1_Click(object sender, EventArgs e)
{
// Create form2
Form2 frm2 = new Form2();
// Handle btn2 click
frm2.btn2_Clicked += new Form2.GetValue(frm2_btn2_Clicked);
// Show form2
frm2.Show();
}
void frm2_btn2_Clicked(string value)
{
// When btn2 is clicked, the text in txt2 will be assign to txt1
txt1.Text = value;
}
And the text will be assign to txt1 in form1
I think you have to create getters and setters for your textbox so it will be exposed as part of your form class and you can access it as a form object
first forn
public string sometext
{
get { return sometext.Text; }
set { sometext.Text = value; }
}
second form
Form1 form= new Form1();
form.sometext = "some name";
here is another short solution i found
In Form1 initiate form 2
Form2 form2 = new Form2();
form2.Owner = (Form)this;
form2.Show();
and in Form2's
this.Owner.Controls.Find("TextBox1",true).First().Text="Hi!!!!"
How about just change the txtBox in the Form2.designer.cs from private to public?
private System.Windows.Forms.TextBox textBox1;
to
public System.Windows.Forms.TextBox textBox1;
This way you can just send it like this in Form2:
Form1 frm1 = new Form1();
private void button1_Click (object sender, EventArgs e)
frm1.textBox1.AppendText(textBox2);
textBox2 being the one where you enter your text in Form2

Categories

Resources