猿问

字符串变量赋值不起作用 C#

我正在调用一个模式表单( ss),它显示给定搜索条件的所有股票代码。用户选择一个项目


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 

{

    selectedStockDescription = dgv_StckSrchRes.Rows[e.RowIndex].Cells[1].Value.ToString();

    selectedStockCode = dgv_StckSrchRes.Rows[e.RowIndex].Cells[0].Value.ToString();


    DialogResult result = MessageBox.Show(

        (selectedStockDescription), 

        "Add this item to the order?", 

         MessageBoxButtons.YesNoCancel, 

         MessageBoxIcon.Question);


    if (result == DialogResult.Yes) 

    {

        stockCode = selectedStockCode;

        stockDescription = selectedStockDescription;


        this.Close();                         

    }

    else if (result == DialogResult.No) 

    { 

        this.Focus();

    }

    else if (result == DialogResult.Cancel) 

    {

        this.Close(); 

    } 

}

并以模态形式设置以下两个公共字符串:


public string stockCode { get; set; }

public string stockDescription { get; set; }

在我的父表单中,我将这些值分配给在类级别初始化的两个变量。


using (StockSearch ss = new StockSearch(selectedDept, txb_StockCode.Text))

{

    if (ss.ShowDialog() != DialogResult.Cancel)

        stckCd = ss.stockCode;

    stockDescription = ss.stockDescription;


    SetFormProperties();

    PopulateStockInformation();

    GetLeadTimes();   

}

然而,只有stockDescription变量是设置。该stckCd保持null-即使在右边的值ss.stockCode-填充。它只是没有分配值stckCd,我需要它才能在我的应用程序的其他地方访问这个值。我试过用一个值初始化它们,但它没有修复它。


任何人都可以请教我吗?


慕姐4208626
浏览 367回答 1
1回答

哈士奇WWW

问题出在StockSearch(模态形式)方面:关闭时StockSearch您应该设置DialogResult:这里有两种可能的关闭方式 -取消(默认行为)和OK。&nbsp; private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) {&nbsp; &nbsp; selectedStockDescription = dgv_StckSrchRes.Rows[e.RowIndex].Cells[1].Value.ToString();&nbsp; &nbsp; selectedStockCode = dgv_StckSrchRes.Rows[e.RowIndex].Cells[0].Value.ToString();&nbsp; &nbsp; DialogResult result = MessageBox.Show(&nbsp; &nbsp; &nbsp; (selectedStockDescription),&nbsp;&nbsp; &nbsp; &nbsp; "Add this item to the order?",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MessageBoxButtons.YesNoCancel,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MessageBoxIcon.Question);&nbsp; &nbsp; if (result == DialogResult.Yes) {&nbsp; &nbsp; &nbsp; // Confirmed: close the form WITH the choice made (DialogResult.OK)&nbsp; &nbsp; &nbsp; stockCode = selectedStockCode;&nbsp; &nbsp; &nbsp; stockDescription = selectedStockDescription;&nbsp; &nbsp; &nbsp; // this.Close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// <- cause of the misbehaviour&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; this.DialogResult = DialogResult.OK; // <- not Close (with cancel) but with OK&nbsp; &nbsp; }&nbsp; &nbsp; else if (result == DialogResult.No)&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; // Not confirmed: keep on selecting&nbsp;&nbsp; &nbsp; &nbsp; this.Focus();&nbsp; &nbsp; else // no need in "if": all the rest is Cancel&nbsp;&nbsp; &nbsp; &nbsp; // Cancellation: close the form WITHOUT choice (DialogResult.Cancel)&nbsp; &nbsp; &nbsp; this.Close(); // <- Close with DialogResult.Cancel&nbsp; }在父窗体端&nbsp; &nbsp;using (StockSearch ss = new StockSearch(selectedDept, txb_StockCode.Text)) {&nbsp; &nbsp; &nbsp;if (ss.ShowDialog() != DialogResult.Cancel) {&nbsp; &nbsp; &nbsp; &nbsp;stckCd = ss.stockCode;&nbsp; &nbsp; &nbsp; &nbsp;// You should not change stockDescription on DialogResult.Cancel&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;stockDescription = ss.stockDescription;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; &nbsp;//TODO: it seems that these three calls should be within if as well&nbsp; &nbsp; &nbsp;SetFormProperties();&nbsp; &nbsp; &nbsp;PopulateStockInformation();&nbsp; &nbsp; &nbsp;GetLeadTimes();&nbsp; &nbsp;&nbsp; &nbsp;}
随时随地看视频慕课网APP
我要回答