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=オブジェクト参照がオブジェクトのインスタンスに設定されていませんでした」というメッセージが表示されます。

それを踏んで、爆撃する線は次のとおりです。

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;
        }

再度更新:

「InitializeComponent();」を追加します オーバーロードされたコンストラクターにトリックを実行しました-ありがとう、SW!

4

2 に答える 2

2

私はC#を初めて使用するので、間違っている場合はご容赦ください。

MSDNでは、発生しているエラーは誤った参照が原因であると記載されているため、あなたの場合textBoxFullPathは存在しない可能性があります(スペルを確認する必要があります)。

しかし、ここでは、ファイルストリームに関するものであり、パスを使用しているため、これが役立つ場合があります。(最初の答えを確認してください)。

それが役に立てば幸い。

于 2012-04-12T20:14:49.893 に答える
1

デザイナーは常にパラメーターなしのコンストラクターを呼び出すと思うので、WinFormフォーム用に独自のコンストラクターを作成しようとしないことに惹かれました。

以下の微調整の提案を参照してください。目的の場所に到達できない場合は、お知らせください。更新します。

    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 に答える