使用此代码(listApplications 是一个 ListView 控件):
private void ShowApplicationPropertiesForm() {
String FullPath = String.Empty;
String Title = String.Empty;
String Description = String.Empty;
Boolean Legacy = false;
Boolean Production = false;
Boolean Beta = false;
MyCustomListViewItemDescendant lvi = (MyCustomListViewItemDescendant)listApplications.SelectedItems[0];
FullPath = lvi.ExePath;
Title = lvi.Text;
Description = lvi.ToolTipText;
ApplicationProperties ap = new ApplicationProperties(
FullPath,
Title,
Description,
Legacy,
Production,
Beta);
ap.Show();
}
//overloaded form constructor
public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta) {
this.Text = String.Format("{0} Properties", ATitle);
textBoxFullPath.Text = AFullPath;
textBoxTitle.Text = ATitle;
textBoxDescription.Text = ADescription;
checkBoxLegacy.Checked = ALegacy;
checkBoxProduction.Checked = AProduction;
checkBoxBeta.Checked = ABeta;
}
...我得到,“System.NullReferenceException 未处理 Message=Object 引用未设置为对象的实例。”
穿过它,炸弹的线是:
textBoxFullPath.Text = AFullPath;
textBoxFullPath 是表单上的文本框;AFullPath 具有以下类型的有效值:“Q:\What\AreYou\Gonna\Do\BabyBlue.exe”
更新:
部分解决。
这是旧的“过早分配”问题。通过将赋值从构造函数移动到 Load() 事件,它不再爆炸(下面的代码)。
但是,现在运行时表单上没有显示任何内容...???!?
public partial class ApplicationProperties : Form {
String _fullPath = String.Empty;
String _title = String.Empty;
String _description = String.Empty;
Boolean legacy = false;
Boolean production = false;
Boolean beta = false;
public ApplicationProperties() {
InitializeComponent();
}
public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta) {
_fullPath = AFullPath;
_title = ATitle;
_description = ADescription;
legacy = ALegacy;
production = AProduction;
beta = ABeta;
this.CenterToScreen();
}
private void ApplicationProperties_Load(object sender, EventArgs e) {
//this.Text = String.Format("{0} Properties", _title);
Text = String.Format("{0} Properties", _title);
textBoxFullPath.Text = _fullPath;
textBoxTitle.Text = _title;
textBoxDescription.Text = _description;
checkBoxLegacy.Checked = legacy;
checkBoxProduction.Checked = production;
checkBoxBeta.Checked = beta;
}
再次更新:
添加“初始化组件();” 重载的构造函数成功了-谢谢,SW!