Still new to C# .NET so simple (with explanation) is preferred. 🙂 I have a Windows form with a few textboxes. I want to be able to read in the contents of these textboxes,in a separate file. I’ve seen articles (also here on stackoverflow) which cover similar problems, but don’t work in my case.
The data I want is to be found in textboxes in Form1.
Where I want this data to go is -> myOtherCS, where it will be used in a method (Savedoc).
In Form1.cs I have:
private myOtherCS allOtherMethods;
public static string myText= "";
public static string mytitle = "";
public Form1()
{
InitializeComponent();
allOtherMethods = new myOtherCS();
}
/* I would like the myText to be filled with the contents of Textbox1
* and mytitle to be filled with contents of Textbox2. Ideally when the
* Textboxes have been changed. */
private void TextBox1_TextChanged(object sender, EventArgs e)
{
myText = Textbox1.Text;
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
myTitle = Textbox2.Text;
}
In myOtherCS file I want to be able to use these values within a different method. So first probably “get” and “set”-ing them.
I’ve tried a lot of things but here is one.. to get the idea from.. I do know that you have to change things in both files, and have, but this is to get the idea.
public class GetTextBoxes
{
private string title;
private string text;
public string Title
{
get { return title; }
set { title = value; }
}
public string Text
{
get { return text; }
set { text = value; }
}
}
public void SaveDoc()
{
GetTextBoxes.title;
GetTextBoxes.text;
}
This is PSEUDOcode as of yet, to try to show what I want to do. I’ve tried many things, if someone knows how to do this, I would very much appreciate it! Thanks in advance
You have said that your form is calling methods in another class when you click a button, and those other methods need to use the current value of your text boxes when performing their calculations. The proper way to deal with that is to just have those other methods accept two string parameters and to pass the textbox’s
Textvalue when you call those methods.Doing that will allow you to remove the text changed handlers and the public static fields. It will ensure that the information isn’t exposed to everything, when really only these two classes need to have access to it, and it will make your program easier to maintain going forward.