C # how to set textbox property in form from another class?

I still can’t access the controls in the form from another class.im new in C #, so my try and error method doesn't actually work.

can anyone give me a simple guide?

this is my piece of code

Form1:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); print pr = new print(); pr.p(); } } } 

This is the print class:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication2 { class print { public print() { } public void p() { Form1 f = new Form1(); f.textBox1.Text = "change text"; } } } 

as you can see, I am trying to change the textBox1 property in the print.but class, when I compile it, im thrown with a System.StackOverflowException!

im getting frustrated now because i can't access it from another class.

can someone give me step by step how to call it from print class? I tried many steps from the Internet, but I just can't get it.

btw, I made the modifier for the text field public.

+4
source share
8 answers

The problem is that the print.p method creates a new form, and the form constructor calls the print.p method print.p . Then another new form is created, the constructor of which calls the print.p method print.p , the loop again and again. Where the StackOverflowException comes from.

The solution is to get a book that teaches object-oriented programming. I know that probably sounds funny or useless, but none of the answers you get on the Q & A site can explain to you why what you are trying to do is a bad idea. External helper classes, such as print , cannot be associated with private members of your form class. Instead, you should call the public methods in your form class that make these changes.

A quick and dirty fix requires you to print.p out a way to get a reference to an instance of your form class in the print.p method. This is the only way you can call methods on this object, rather than creating a new object of this class. One way is to pass the method reference as a parameter:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); print pr = new print(); pr.p(this); } } class print { public print() { } public void p(Form frm) { frm.textBox1.Text = "change text"; } } 

Also, as a somewhat inconsequential stylistic note, you should be aware that almost all coding rules / conventions for C # (and the .NET platform) require that the class and method names be PascalCased, not camelCased. So your print class should be called print .

+5
source

The easiest way is to simply pass the link to the new form.

 print pr = new print(textbox); public print(Textbox textbox) { //do something with textbox. } 

However, there are a number of design patterns that can help with this, such as MVP or MVVM. However, they can be a little advanced for your current level. If this is just a small project, then the code above will be fine.

+1
source

The code you have is problematic for other reasons (@CodyGray explains the reason you get a StackOverflowException ), but in general you can use the property to access without exposing the TextBox control itself; see this MSDN page for details, but for example:

 namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { } public string FormText { get { return textBox1.Text; } set { textBox1.Text = value; } } } } 

Then, to use this property:

 public void p() { Form1 f = new Form1(); f.FormText = "change text"; } 

EDIT:

Since there is at least one nitpick through comments and enough incentive for at least one person to find this point “excellent”, I also suggest a slightly different approach, still using the property ...

Suppose the goal is that you want to set text in p , we just return what we need:

 public string p() { return "change text"; } 

So, to:

 myFormReferenceSomewhereNotInPrintClass.FormText = myPrintClassInstance.p(); 

Or do you want to get or use text in p :

 public void p(string text) { //do your thing with the text } 

So, to:

 myPrintClassInstance.p(myFormReferenceSomewhereNotInPrintClass.FormText); 
+1
source

You get a StackOverFlowException because with this code you are creating a new print instance in Form1 :

 print pr = new print(); 

Then, in the constructor in print you create a new instance of Form1 as follows:

 Form1 f = new Form1(); 

which creates a new instance of print , which creates a new instance of Form1 , which creates a new instance of print , which creates a new instance of Form1 , which ...

You created an endless loop that kills Stack.

+1
source

Ok, not sure why you want to do it like this (I would not do it), but here you go:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); print pr = new print(); pr.p(this); } } public class print { public print() {} public void p(Form1 f) { f.textBox1.Text = "change text"; } } 
+1
source

You should not refer to the properties of UI elements as a cross-class, this is bad practice.

best way to do this:

form1:

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); print pr = new print(); textBox1.Text = pr.p(); } } } 

class print:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WindowsFormsApplication2 { class print { public print() { } public string p() { return "change text"; } } } 
0
source
 public Form1() { InitializeComponent(); print pr = new print(this); pr.p(); } 

and in the print class use this to get the form

 { Form fpassed; public print(Form f) { fpassed = f; } public void p(Form f) { f.textBox1.Text = "change text"; } } 

may call it p (fpassed);

0
source
 public static MainForm frm; public MainForm(){ frm = this; Yes() } public Yes(){ frm.textbox1.text = ":D"; } 
0
source

Source: https://habr.com/ru/post/1348070/


All Articles