0

使用此代码(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!

4

2 回答 2

2

我是 C# 的新手,所以如果我错了,请原谅我。

在 MSDN上,它说你得到的错误是由于错误的引用,所以,在你的情况下,我猜textBoxFullPath可能不存在(你应该检查拼写)。

但是在这里,它是关于文件流的,因为您使用的是路径,可能会对您有所帮助。(检查第一个答案)。

希望能帮助到你。

于 2012-04-12T20:14:49.893 回答
1

我认为设计者总是调用无参数构造函数,所以我倾向于不尝试为 WinForm Forms 创建自己的构造函数。

请参阅下面的调整建议 - 如果您没有到达您想要的位置,请告诉我,我会更新。

    public ApplicationProperties(String AFullPath, String ATitle, String ADescription, Boolean ALegacy, Boolean AProduction, Boolean ABeta)
        : this() // --> Call the parameterless constructor before executing this code
    { 
        _fullPath = AFullPath; 
        _title = ATitle; 
        _description = ADescription; 
        legacy = ALegacy; 
        production = AProduction; 
        beta = ABeta; 
        this.CenterToScreen(); // --> Maybe move this to the Shown event (not sure if you need to)
    } 
于 2012-04-12T20:24:35.890 回答