2

メッセージボックスを使用して、ユーザーがグリッドビューから行を削除するかどうかを確認しますが、どのような回答をしてもフォームを閉じてform1に戻ります

これは、viewTransactions フォームが読み込まれる場所です。このコードは Form1 にあります。

 private void btnViewTrans_Click(object sender, EventArgs e)
 {
    viewTransactions = new View_Transactions(newList);
    if (viewTransactions.ShowDialog() == DialogResult.OK)
    {
       newList.Equals(viewTransactions.getList());
    }

 }   

これは、viewTransaction フォームで messageBox が表示される場所です。

    ///////////////////////////////
    //Remove an item from the list
    private void button3_Click(object sender, EventArgs e)
    {
        DialogResult result = new DialogResult();
        result = MessageBox.Show("Are you sure you want to delete this element?", "Confirmation", MessageBoxButtons.YesNoCancel);
        if (result == DialogResult.Yes)
        {
            foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {
                tmpList.remove(item.Index);//remove item from tmpList used to update the passed in list                    
                dataGridView1.Rows.RemoveAt(item.Index);//remove the item from the dataGrid
            }
        }
    }

メッセージボックスを使用して警告を表示するまで、コードに問題はありませんでした。DialogResult が他の ShowDialog に渡されていると思います。それが私のフォームを閉じている理由です。

4

2 に答える 2

0

this.dialogResult = dilaogResult.None; を追加して解決しました。button3_Click が呼び出されるとすぐに、 base.DialogResult が何らかの理由でキャンセルされました

スティーブ、私があなたのラインを試したとき、それはまだ閉じていましたが、それが私がそれを理解した方法である方法を教えてくれてありがとう

private void button3_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.None;
        DialogResult result = new DialogResult();
        result = MessageBox.Show("Are you sure you want to delete this element?", "Confirmation", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes)
        {
            foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
            {
                tmpList.remove(item.Index);//remove item from tmpList used to update the passed in list                    
                dataGridView1.Rows.RemoveAt(item.Index);//remove the item from the dataGrid
            }
        }

    }
于 2014-09-26T17:06:49.843 に答える