c# control value problem at form load [duplicate]
like coffee for an insomniac, i have another question to post.
in a form's load event, i create and initialize controls based on attributes of a class passed as an argument into the form's constructor. the comboboxes and checkboxes fail early (method "initializeControls()") into the form creation but not later (method "resetData()"). i fail to understand as both are called during event "SomeForm_Load(sender, e)". did i do something as basic as use the wrong event? as stated in my earlier question posts, i'm new to c#. any guidance appreciated. feel free to tell me if i use far too much reflection, too, :D.
the pseudo code, for the gist of my problem:
public class SomeForm : Form
{
#region fields
private int _id { get; set; }
private int _id2 { get; set; }
#endregion fields
...
public SomeForm()
{
InitializeComponent();
}
public SomeForm(int id, int id2) : this()
{
_id = id;
_id2 = id2;
}
#region init
private void SomeForm_Load(object sender, EventArgs e)
{
method1(); // fails
method2(); // works
}
private void method1()
{
var ds = new []{1,2};
CheckBox cb = new CheckBox();
cb.DataSource = ds;
cb.SelectedValue = _id; // <== this is the problem. these two
this.Controls.Add(cb); // <== steps should be switched.
}
private void method2()
{
cb.SelectedValue = _id2;
}
...
}
the real code:
public class SomeForm : Form
{
...
#region fields
...
private MultiState.Update _child { get; set; }
protected object data { get; set; }
private Type _masterType { get; set; }
private List<PropertyInfo> _mpks = new List<PropertyInfo>();
private User _user { get; set; }
#endregion fields
...
public SomeForm()
{
InitializeComponent();
}
public SomeForm(User user, Type amaster, object aobject, Dictionary<string, object> astate) : this()
{
data = aobject;
_masterType = amaster;
dataState = astate;
_user = user;
}
#region init
private void SomeForm_Load(object sender, EventArgs e)
{
initialize();
}
private void initialize()
{
tsData.Visible = false;
if (data != null)
{
initializeControls(); // FAILS!! without exception
dataId = DataService.GetPrimaryKeyValue(data);
resetData(); // SUCCEEDS
}
}
private void initializeControls()
{
...
ComboBox cb = new ComboBox();
cb.Enabled = fdEnabled; // correctly read from linq datacontext custom attribute
cb.FormattingEnabled = true;
cb.Location = new Point(x, y);
cb.Name = _CP_COMBOBOX + pi.Name;
cb.Size = new Size(_WIDTH_CODE, _HEIGHT_SINGLE);
cb.TabIndex = i;
cb.TabStop = true;
cb.Leave += new EventHandler(this.ctlEdit_Leave);
// set drop-down
cb.DataSource = domain; // correctly populated from service class
cb.ValueMember = "Id";
cb.DisplayMember = "Label";
// set default
if (fdDefault != null)
{
THE ASSIGNMENT BELOW FAILS!! the desired value calculates correctly (wth, when uncommented), but it is simply ignored without any exception.
//object wth = GetDomainKeyAsIdentifierSafe(domain, fdDefault, _user); // correctly parsed
cb.SelectedValue = GetDomainKeyAsIdentifierSafe(domain, fdDefault, _user); // HUGE FAIL!!
}
...
}
#endregion init
...
#region persistence
...
protected string resetData()
{
string rc = "";
if (!isDataNew()) // this form only modal
{
//resetDataState();
#region bind-object-vs-set
foreach (Control control in this.Controls)
{
try
{
if (control.Name.StartsWith(_CP_TEXTBOX))
{
MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + control.Name.Substring(_CP_TEXTBOX.Length));
obje开发者_JAVA技巧ct value = mi.Invoke(data, null);
control.Text = value == null ? "" : value.ToString();
}
else if (control.Name.StartsWith(_CP_CHECKBOX))
{
#region bind-object-vs-set-cbx
bool ck = false;
string scontrol = control.Name.Substring(_CP_CHECKBOX.Length);
MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + scontrol);
object value = mi.Invoke(data, null);
if (value != null)
{
if (value.GetType() == typeof(bool))
{
ck = (bool)value;
}
else if (value.GetType() == typeof(Nullable<bool>))
{
Nullable<bool> nvalue = (Nullable<bool>)value;
if (nvalue.HasValue)
ck = nvalue.Value;
else
ck = AtsService.GetDefaultBoolean(data.GetType(), scontrol);
}
}
CheckBox cbx = (CheckBox)control;
cbx.Checked = ck;
#endregion bind-object-vs-set-cbx
}
else if (control.Name.StartsWith(_CP_COMBOBOX))
{
MethodInfo mi = data.GetType().GetMethod(DataService.LINQ_GET + control.Name.Substring(_CP_COMBOBOX.Length));
object value = mi.Invoke(data, null);
ComboBox cb = (ComboBox)control;
THE ASSIGNMENT BELOW MYSTERIOUSLY WORKS!!
cb.SelectedValue = value == null ? FormService.NOSELECTION_ID : value; // but this one works!!
}
}
}
}
}
...
#endregion persistence
}
edit: added bigger flags at success and fail points.
Though your problem is not clear enough, but i think you are creating a local variables inside "initialzeControls()" and you are expecting them to be available at the class level somehow..
you have to create controls at the class level for that.. may be i'm wrong, because your error is not clear. you have to present your problem clearly, what you are expecting and why and what you are actually getting..
It's a bit hard to follow your wall of code since you have used ambiguous names and haven't documented your code properly.
I suggest you narrow down your problem by setting breakpoints and stepping into your code: F11
.
精彩评论