Writing to a textbox on a separate form (C#) - c#

Suppose I have two forms: Form1 and Form2. Form2 has a text control named text1
In VB6 I could write to Form 2's textbox
control from Form1 by using: Form2.Text1.text = "Some text here"
How can I make this work in C#? I have tried everything!
What I have tried:
Form2 Frm2 = new Form2();
Frm2.show();
Frm2.Activate(); // Trying everything to make sure it sees the form (it does).
Frm2.Text1 (Doesn't find the control)...
ANSWER:
I ended up making a public routine in Form2, and then just calling this routine from form1. In this public routine of Form2 I would then call the textbox!

I believe all form elements are private by default. So you're going to have to switch their definition to public (in your .designer.cs) or make a public getter and setter for each textbox you want to access.

You need to keep a reference to the second form in the first form.
// form1 code
// variables
Form2 myForm2;
// Form1_Loaded event handler
myForm2 = new Form2();
myForm2.Show();
// place where you want to change text on form2 from within form1
myForm2.Text1.Text = "Some text here";

You have to have a reference to the instance of Form2 in order to write to it. Pass the reference to the instance of Form2 to Form1 and then manipulate it like you are expecting. You can pass this instance in via the constructor or you can pass it in later. You can also store the instance reference in a central location. Just be sure that you don't create a new instance of Form2 when you try to access the instance. For example, if you put the following code in your form:
Form2 myInstance = new Form2();
then you will be creating a new instance of Form2, which probably isn't what you want.

My goal was add text to a Text Box on another form. I had Form1 and Form2. Form2 has a text box control named Text1. In order to make this work, I created a Sub Routine:
public Void WriteToText(string sData)
{
// Here is where I wrote to my textbox
Text1.text = sData;
}
Form 1 code:
Form2 Frm2 = new Form2();
Frm2.WriteToText("My Data");

Related

C# Access active Form from another active Form

Hey I am kinda stuck here and could need a little help or reference.
I have one main form (Form1) and two sub forms (Form2 & Form3).
On Form1 is a Panel (Panel1) with two Buttons (Button1 & Button2).
If I click Button1 I want to display Form2 inside the Panel of Form1.
If I click Button2 I want to display Form3 inside the Panel of Form1.
I got that working already with the following code (Button1 Click):
Form2.TopLevel = false;
Panel1.Controls.Add(Form2);
Form2.FormBorderStyle = FormBorderStyle.None;
Form2.Dock = DockStyle.Fill;
Form2.Show();
now to my actual problem.
In Form1 I have booleans, integers and strings that I want to reach (read and write) from Form2 and Form3.
Since Form1 and either Form 2 or Form 3 are active I can't just use "Form1 form1 = new Form1;" since initializing a new form doesn't use the already created instance. It's a new form so it has the default values from start and not the changed ones from the active Form1 which I want.
So now I want to have a field in Form2 and Form3 to hold the active instance of Form1 so that I can change and read the values of the actual active Form.
How would I do that?
Pack all data you want to pass from form 1 in a class. ( define a class for this ).
You can define constructors for Form 2 and 3 and pass the instance of type you defined from form 1 to 2 and 3. That is one way of doing this.
Another way is to define properties for Form 2 and 3 and assign values before you call .Show() method. This is not a great idea if your form 2 and 3 can be used some where else and you may accidentally not assign the values to the properties.
Did you create instances of Form2 and Form3 in Form1? If Yes, just add an argument to the constructor of both sub forms.
UPDATED
// In Form1
public partial class Form1 : Form
{
private readonly Form2 _form2;
public Form1()
{
_form2 = new Form2(this);
InitializeComponent();
}
}
// In Form2
public partial class Form2 : Form
{
private readonly Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
InitializeComponent();
}
}

How to return a string from child form to main form (not open any new form)?

My problem is:
Main form is opened first, there is a button to open Child form. I use Constructor and I want return a string from Child form to the textbox on Main form, and my code now is:
Form1 f = new Form1(txt1.Text);
f.Show();
But, a new main form will open up, the string won't fill in the first main form.
So how to work with only one main form?
You can do this, get it via OpenForms:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
Make sure you declare your control txt1 as public on its Modifier property so that you can access it from Child form:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.txt1.Text = "Change";
frm.Show();
Now if you are trying to change it via its constructor, I doubt you can do it since it was already initialize, unless you call another new Form1 to initialize it and will go through its constructor again.
What you can do is change its property directly:
Form1 frm = Application.OpenForms.OfType<Form1>().FirstOrDefault();
frm.stringMain = "Foo"; //Your property you want to change
frm.txt1.Text = "Change";
frm.Show();

Getting Form2 variable to Form1 (User text input in TextBox)

The plan:
I want to ask the user with second Form2 to input some text.
When this Form2 is closed, i want to display the input text in a textbox on Form1...
On a button event, on Form2 i can reach Form1's textbox:
Form1 form1 = new Form1();
And:
form1.myText = "Test Name";
And then i close Form2:
this.Close();
But the value "Test Name" does not appear in form1's textbox...
I don't get an error.
When you call new Form1(), then new instance of Form1 is created. You have two objects of Form1. That's why your code doesn't work.
If you want to make it fast, add Form1 as a variable to Form2 class.
public Form1 form1;
Then you can set it just before showing Form2.
Form2 form2 = new Form2();
form2.form1 = this;
form2.Show();
Remember to remove this part: Form1 form1 = new Form1();.
Now your code should work.
Create event handler on Form2 and when open Form2 from Form1 hook on this event. In form2 fire this event with your own eventargs which contains text which u need to show in Form1.
Another solution is Action. Create property Action in Form2 and when opened Form2 set this action. When Form2 is closed fire this action like _action.Invoke(textWhichUneed);
You should make assign value before close. For example
form1.myText = "Test Name";
this.Close();
Also form1 shouldn't be created, but passed. How do you pass form1 into form2?

Passing values between a class and a form

I'm pretty new to C# - but it all went quite well - to this point.
I start with a form and a class which does most of the work (non static and in the program part). I instantiate an object of the class in form 1 and do a log in.
Then I switch over to the next form, form2. Actually, the the class does that. I have a method there, that contains the line:
this.f2 = new Form2();
and then:
f2.Show();
f2 is a class member of the type Form2 - and all just works fine - up to this point.
This Form2 just consists of a big text box in which I want to display network events. The event handler works just fine - but the reference to the form just doesn't seem to work. If I do
f2.tetBox1.Text = "Some text";
it just won't change the the text in the text box.
What am I doing wrong here?
Here is a more detailed description of what I'm doing:
Form1 passes some log in information to myProg, being an instance of MyClass. If the login was successful, Form1 calls myProg.makeForm();
This is what the method in MyClass looks like:
public void makeForm() {
this.f2 = new Form2();
f2.Show();
this.sendString("start f2");
}
This is MyClass.sendString():
private void sendString(string text) {
SystemSounds.Beep.Play();
this.f2.setTextBox(text);
}
This calls, as you see, setTextBox() of Form2 - which I implemented as proposed here. The strange thing is, that up to this point all works well. The Form2 gets shown an textBox1 contains "start f2" - as expected. But when an event comes in, the text in textBox1 doesn't change. the beep get played all right - so the method sendString() gets called alright.
One thing I have observed: If the beep line is placed after the call to this.f2.setTextBox(text);, it doesn't get played if the method is called from the event handler.
f2, btw., is a private member of MyClass:
private Form2 f2;
Any control created by the designer (e.g. dropped from the Toolbox at design-time) is automatically set as private. Therefore, you can't access it from another form.
You don't want to start messing with the designer, instead - create a property on your Form2 which will allow you to modify the text of the Textbox from Form1.
A short example is something like this:
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
// When modifying the Text property it will change the text in textbox1
public string Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
}
Then, in Form1:
Form2 frm2 = new Form2();
frm2.Text = "123"; // Uses the public Text property declared in Form2
frm2.Show();
By default a textbox is private when you place it via the designer, which means you can't access textBox1 from outside of Form2 (it's only 'visible' to code within the Form2 class). You could change the textbox to be internal or public (which would let you do the following (from Form1):
f2.tetBox1.Text = "Some text";
BUT that would be exposing parts of Form2 which only Form2 should really know about...so it isn't the cleanest solution (though it would be likely the quickest))
A better solution might be to create a method or property (internal or public) in Form2 which will set the text value. E.g.:
internal void SetText(string value)
{
textBox1.Text = value;
}
As the method (or property) resides in Form2, it has access to the text box, so it can set the value while textBox1 is still private.
It's safer exposing this method to 'external' code (as opposed to making the textbox visible to them) as all they can do is change the text - whereas exposing the whole textbox might let callers try to change background colour, etc.

Fill Identity Of WinForms Into Textboxes

I have Three Forms as below:
Form1
Form2
Form3
The Form1 and Form3 are communicating with Form2. I want If Form1 visited to Form2 than
Form1 Should leave it’s identity to Form2’s textbox1. If Form3 visited to Form2 than Form3
should leave it’s Identity to Form2’s textbox2.
Is it possible?.
Here is Visted Means as below.
By Form1
Form2 f2= new Form2();
f2.Show();
By Form3
Form2 f2= new Form2();
f2.Show();
And Identity Like "Form1" and "Form3"
I believe you are asking for the form that opens Form2 to set some specific value to the textbox. You could have a property on Form2 that sets the textbox text.
public partial class Form2 : Form
{
/*.......*/
public string Identity
{
set
{
textbox1.Text = value;
}
}
}
Then in Form1 or Form3:
Form2 form2 = new Form2();
form2.Identity = this.Name;
form2.Show();
The best thing to do is overload the Show method for each of your forms to accept something like a "previous form" parameter that will serve as a reference to the caller (you could do this either as a Form object, whose properties you could then access, or simply as a String). Then, wherever you show a second form from an existing form, you pass information about the existing form into the Show method for the second form. The second form's Show method would take care of updating its textbox with the name of the form that displayed it.
For example, each form would contain an overloaded Show method:
public void Show(Form previousForm)
{
//Set the textbox on this form to contain the name of the calling form
myTextBox.Text = previousForm.Name;
//Call the base class's method to show this form
base.Show();
}
And you would show one form from another by calling its overloaded Show method:
private void ShowOtherFormButton(object sender, EventArgs e)
{
//Create a new instance of the form you want to display
Form2 myOtherForm = new Form2();
//Show the other form, passing the calling form as a parameter
myOtherForm.Show(this);
}
This way, no form is responsible for or even allowed to update controls contained by another form. You don't have to expose any additional properties or remember to call any additional methods on the forms you want to show—it's built right into the Show method that you have to call anyway.
Alternatively, you could overload each form's constructor in the same way, but that won't work if you want to be able to move between existing instances of each form. You'll only be able to specify information about the previous form when you're creating a new instance of each form.
You will need a reference to the caller. You can do this by overriding the constructor of the Form.
public partial class Form2 : Form
{
Form _caller;//Caller form
//Default constructor
public Form1()
{
InitializeComponent();
}
public Form1(Form caller)
{
InitializeComponent();
this._caller = caller;
textbox2.Text = caller.Name;
}
}
All you have to do now is call Form2 using the new constructor:
Form2 form2Instance = new Form2(this);
form2Instance.Show();
Whichever form will call Form2, will now have it's name written on textbox2 of Form2
You need to pass data between the window forms(ie.,the name of the form in this scenario)so that you can get it in Form2.
Refer the lint to pass data between window forms: http://www.vbdotnetheaven.com/UploadFile/thiagu304/passdata12262006073406AM/passdata.aspx

Categories

Resources