How to access form control from within ToString method of nested struct
I am building a small windows form application in C#. Within the form code I d开发者_运维技巧efine a public struct with a ToString method that must build part of its output from items in comboBoxes on the same form. This doesn't sound like it should be difficult
public partial class Form1 : Form
{
public struct OrderLineItem
{
string someString;
int index;
string ToString()
{return someString + ActiveForm.sizeComboBox.Items[index].ToString();}
}
}
It complains that it cannot find a definition for sizeComboBox. If I explicitly use the name of the form, it says an object reference is required for the static field... I'm not quite sure what it means by that. Using this.sizeComboBox refers to the struct, not the form. Using just sizeComboBox, again, an object reference is required.
The struct knows about its containing class type, but it doesn't know about any particular instance of that type, unless you tell it. For example, you can create a constructor that takes a Form1 object, and it can save a reference to that form in a member variable.
public partial class Form1 : Form
{
public struct OrderLineItem
{
string someString;
int index;
Form1 parentForm;
internal OrderLineItem(Form1 parentForm)
{
this = new OrderLineItem();
this.parentForm = parentForm;
}
string ToString()
{
if (parentForm == null)
return string.Empty;
else
return someString + parentForm.sizeComboBox.Items[index].ToString();
}
}
}
That said, this is a seriously questionable design.
The tiered structure of this application appears to be upside-down. The order-line-item objects should exist at a lower level than the user interface layer. The UI can sometimes see the business objects (order, order-line-item, etc.), but the business objects should not know anything about the UI.
If you can invert this structure, it will make the code much cleaner.
Suppose the other form is Form2
you can cast ActiveForm
to Form2
:
var form2 = ActiveForm as Form2;
if (form2 != null) // form2 == null if ActiveForm is not of type Form2.
{
form2.sizeComboBox...
}
Edit:
Two notes.
- Instead of getting ActiveForm it is better store form2 in a member variable in form1 when form2 is created.
- You should encapsulate the getting of combobox values behind a property in Form2, like
SelectedFooValue
.
public partial class Form1 : Form
{
internal static Form1 ActiveForm { get; set; }
public Form1()
{
InitializeComponent();
ActiveForm = this;
}
public struct OrderLineItem
{
public override string ToString()
{
return ActiveForm.sizeComboBox.Items[index].ToString();
}
}
However note that this is not the correct approach. Maybe you can post what you are trying to accomplish and we can help?
You need to cast ActiveForm
to Form1
.
Something like this I think (don't have VS open now to check):
return someString + ((Form1)ActiveForm).sizeComboBox.Items[index].ToString();
However, this is generally not a good way of going about things, you shouldn't make your methods in your classes and structs refer directory to controls since then you tie them together to closely. Try to send the data in to the struct instead or create a method on the form to return the data in some way.
精彩评论