Reputation: 196
I have created a winform (using the designer VS2016) that has a few methods and events (button clicks) to analyse data.
I have four different ways to analyse the same dataset, my winform currently encompasses one of them and it's working fine. It's longer than anticipated and before going further i'd like to organise better, so i added a class:
namespace Analysis1
{
public partial class Form1:Form
private struct DataStruct
public Form1()
public void button1_Click //opens the data source and calls methods
public class Analyse1:Form1
}
however, none of the form textboxes are available in my new class Analyse1 (they are set to ""). I've read quite a few threads (example) that seem to imply that i need to initialise every single textbox on the form to use in another class.
there is only one form, is there any way to call all the textbox values from the form into the new class without going one by one? they are all user entry, and i just need the value, the textboxes don't have any events
thanks
edit: the user will enter a lot of textbox data on the form. depending on which button they click, it will cause a different type of analysis on the data (and return different charts and tables). i just want to be able to access textbox1.Text (and so on, around 120 textboxes) from each analysis class
Upvotes: 0
Views: 44
Reputation: 1593
There's any number of ways you can do that, probably the simplest though, is to use the tag
property on the various text boxes, something like Class1
, Class2
, etc and then expose a property in Form1
like so;
public List<string> Class1Data
{
get
{
List<string> result = new List<string>();
foreach (Button btn in Controls.OfType<Button>())
{
if (btn.Tag.ToString().Equals("Class1"))
result.Add(btn.Text);
}
return result;
}
}
This should make them accessible from Analyse1
.
And for those who like short, neat code;
public List<string> Class1Data
{
get
{
return Controls.OfType<Button>().Where(b => b.Tag.ToString().Equals("Class1")).Select(b => b.Text).ToList();
}
}
Upvotes: 1