Reputation: 123
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
Upvotes: 2
Views: 9695
Reputation: 32541
Let's say that each your main form and the other form contain one textBox. What you could do is open the other form with Show()
, passing in the main form as a parameter. Then, you could add an event handler for the text change. The sample below will update your TextBox
on the other form, when the main form 's TextBox
text is changed. Here is your main form:
public partial class Form1 : Form
{
OtherForm otherForm;
public Form1()
{
InitializeComponent();
otherForm = new OtherForm();
otherForm.Show(this);
textBox1.TextChanged += textBox1_TextChanged;
}
void textBox1_TextChanged(object sender, EventArgs e)
{
otherForm.textBox1.Text = textBox1.Text;
}
}
In the OtherForm
designer, set textBox1
Modifiers
to Public
, as shown below
[EDIT]
Here's a better approach based on Servy's suggestion.
public partial class Form1 : Form
{
OtherForm otherForm;
public Form1()
{
InitializeComponent();
otherForm = new OtherForm();
otherForm.Show(this);
}
}
//...
public partial class OtherForm : Form
{
public OtherForm()
{
InitializeComponent();
}
string SomeMethod()
{
var parent = (Form1)this.Owner;
return parent.textBox1.Text;
}
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = SomeMethod();
}
}
Upvotes: 0
Reputation: 203814
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 Text
value 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.
Upvotes: 0
Reputation: 179
If you are storing the strings as public then all you have to do is say...
Form1 form=new Form1();
Console.out.write(form.myText);
if your strings are public there is no need to use getters and setters. Though i probably would keep them as static. These methods will update after every change so no extra method is required. You could also set your title objects to public they are type so what you did with the string can just be implemented again but with parts of your form.
public System.Windows.Forms.Button button1;
Upvotes: 1
Reputation: 2874
You may want to change your way of thinking, here some thoughts:
Think of it more in terms of data models. What is your Form1 trying to represent conceptually? Based on that create your data model to support that need. By data model I mean just a simple Class(es) with getters/setters as you have defined in your question, you could call your class Document.
public class Document { public string Title {get;set;} public string Text {get;set; }
So instead of having your static fields in Form1:
public static string myText= ""; public static string mytitle = "";
you could define a public property public Document MyDocument {get;set;}
and initialize it in the Constructor.
Then you could use Data Binding so you can bind your model (Document) to your Controls in Form1.
Think of your interaction between Form1 and myOtherCS. How do you envision user's Workflow? Filling Form1 and then execute an action? go to another form, etc? Is myOtherCS just a Service that will process Form1 data?
Rather than helping you with your specific technical issue, i'd like to broaden your perspective and identify what you really want to do, then find the most appropriate technical solution to fulfill that requirement. Believe me, I've struggled a long time with WinForms but it is more important to have clear what you want to accomplish at a high-level.
Upvotes: 1
Reputation: 2413
I think the best way is what you have done . having an public class to get and set values and share them between different windows forms is the idea I myself always use . You can also pass your values through form constructors but that is when you want to get a value only on form initialization ! so in your case I don't think It's a good idea . . . good luck
Upvotes: 1
Reputation: 551
You need to set the properties of allOtherMethods in the textchanged event handler.
Here is the code:
private void TextBox1_TextChanged(object sender, EventArgs e)
{
allOtherMethods.Text = Textbox1.Text;
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
allOtherMethods.Title= Textbox2.Text;
}
And the code
public void saveText()
{
GetTextBoxes.title;
GetTextBoxes.text;
}
is meaningless. You can not declare a method outside a class.
c# is an OOP language. So the common concept here is that you should always try to set the properties or invoke a method of an instance in the event handler.
Upvotes: 0