我创建了一个custimazibale提示
public static class Prompt
{
public static string ShowDialog(int columnnumber, string columnname)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = columnname;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label() { Left = 50, Top = 20 };
ComboBox comboBox = new ComboBox() { Left = 50, Top = 50, Width = 400 };
comboBox.Items.AddRange(new string[] { "a","b","c" });
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox.SelectedItem = columnname;
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 80 };
confirmation.Click += (sender, e) => { prompt.Close(); };
textLabel.Text = "Colonne " + (columnnumber + 1).ToString() + " : " + columnname;
prompt.Controls.Add(comboBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
prompt.ShowDialog();
prompt.AcceptButton = confirmation;
return comboBox.Text;
}
}
然后我在点击标题时以我的主窗体调用它
private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
dt.Columns[e.ColumnIndex].ColumnName = Prompt.ShowDialog(e.ColumnIndex, dataGridView1.Columns[e.ColumnIndex].Name);
}
即使单击按钮关闭,问题仍然是我的文本更改。 但是我希望它只在用户点击按钮" OK"。
时才能改变答案 0 :(得分:6)
如果不是null
,您可以评估DialogResult
并返回OK
:
public static class Prompt
{
public static string ShowDialog(int columnnumber, string columnname)
{
using (Form prompt = new Form())
{
// other code
return prompt.DialogResult == DialogResult.OK ? comboBox.Text : null;
}
}
}
然后用你的另一种方法:
private void dataGridView1_ColumnHeaderMouseClick(object sender, EventArgs e)
{
var result = Prompt.ShowDialog(e.ColumnIndex,
dataGridView1.Columns[e.ColumnIndex].Name);
if (result != null)
dt.Columns[e.ColumnIndex].ColumnName = result;
}
在prompt
内,你应该相应地设置DialogResult
:
confirmation.Click += (sender, e) =>
{
prompt.DialogResult = DialogResult.OK;
prompt.Close();
};
提示:除了result != null
,您还可以使用!String.IsNullOrWhiteSpace(result)
仅在输入内容时更新列名。
答案 1 :(得分:3)
我会选择这样的事情:
using(Form prompt = new Form())
{
//Initialize the components of your form
DialogResult result = prompt.ShowDialog();
if(result == DialogResult.OK)
{
//return whatever it is you want to return
}
}
在表单中,您可以通过prompt.DialogResult = DialogResult.OK
设置DialogResult以及更多选项(DialogResult.Cancel,DialogResult.Retry等)。
答案 2 :(得分:1)
你可以在确认时设置一个bool,如果没有确认则使用它返回null,如下所示:
public static string ShowDialog(int columnnumber, string columnname)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 150;
prompt.FormBorderStyle = FormBorderStyle.FixedDialog;
prompt.Text = columnname;
prompt.StartPosition = FormStartPosition.CenterScreen;
Label textLabel = new Label()
{
Left = 50,
Top = 20
};
ComboBox comboBox = new ComboBox()
{
Left = 50,
Top = 50,
Width = 400
};
comboBox.Items.AddRange(new string[] { "a", "b", "c" });
comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox.SelectedItem = columnname;
Button confirmation = new Button()
{
Text = "Ok",
Left = 350,
Width = 100,
Top = 80
};
bool confirmed = false;
confirmation.Click += (sender, e) =>
{
prompt.Close();
confirmed = true;
};
textLabel.Text = "Colonne " + (columnnumber + 1).ToString() + " : " + columnname;
prompt.Controls.Add(comboBox);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
prompt.ShowDialog();
prompt.AcceptButton = confirmation;
return confirmed ? comboBox.Text : null;
}
您的调用代码需要检查null的返回值,并且只有在返回的值不为null时才执行某些操作。