c# winform下新建一个模态窗口,模态窗口中有确定、取消。打开文件 3个按钮,打算在这个模态窗口中用一个按钮打开一个选择文件对话框,以返回文件的路径。
private void btnOpenDialog_Click(object sender, EventArgs e)
{
//OpenFileDialog dialog = new OpenFileDialog
Button btn = (Button)sender;
btn.DialogResult = DialogResult.No;
textBoxFilePath.AppendText(btn.DialogResult.ToString() + " ");
openFileDialog1.Multiselect = false;//该值确定是否可以选择多个文件
openFileDialog1.Title = "请选择标签文件:";
openFileDialog1.CheckFileExists = true;
openFileDialog1.Filter = "Excel文件(*.db)|*.db|所有文件(*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.FilterIndex = 1;
this.DialogResult = DialogResult.None;
/*
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBoxFilePath.Text = openFileDialog1.FileName;
//-->获取文件所在的目录
//Dir = Path.GetDirectoryName(dialog.FileName);
//strFilePath = Path.GetFileName(txtFilePath.Text);
FullPath = openFileDialog1.FileName;
}
*/
if (System.IO.Directory.Exists(FullPath))
{
string connectionString = "data source=" + FullPath;
SQLiteConnection dbConnection = new SQLiteConnection(connectionString);//***
dbConnection.Open();
string sqlCommandString = "select name from sqlite_master where type='table' order by name;";
SQLiteCommand cmd = new SQLiteCommand(sqlCommandString, dbConnection);
using (SQLiteDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
// 表名
PathList.Add(dr["Name"].ToString());
}
}
}
if (PathList.Count > 0)
{
// 批量添加按钮及事件
int k = 0;
Button[] buttons = new Button[PathList.Count];
foreach (string name in PathList)
{
buttons[k] = new Button();
buttons[k].Name = "btn" + k.ToString();
buttons[k].Text = name;
buttons[k].Location = new Point(k * 30 + 90, 16);
buttons[k].Size = new Size(30, 10);
buttons[k].Parent = this;
buttons[k].Click += new System.EventHandler(buttons_Click);
groupBoxForTable.Controls.Add(buttons[k]);
}
// 添加datagridview控件,把第一个表中数据装入控件
//DataGridView dataGridView = new DataGridView();
// 连接数据库
dataGridView.AutoGenerateColumns = false;
string connectionString = "data source=" + FullPath;
SQLiteConnection dbConnection = new SQLiteConnection(connectionString);//***
dbConnection.Open();
string sqlCommandString = "select * from " + PathList[0];
//利用 Adapter 转换结果到 datagrid
SQLiteDataAdapter adapter = new SQLiteDataAdapter(sqlCommandString, dbConnection);
// DataSet ds = new DataSet();
DataTable dt = new DataTable();
// adapter.Fill(ds);
adapter.Fill(dt);
//DataView dv = ds.Tables[0].DefaultView;
//关闭连接,并把结果显示在datagrid上
dbConnection.Close();
dbConnection.Dispose();
//dataGridView.DataSource = dv;
dataGridView.DataSource = dt;
}
}
private void btn_cancel_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.DialogResult = DialogResult.Cancel;
textBoxFilePath.AppendText(btn.DialogResult.ToString() + " ");
}
结果不理想。结果是:每次点击这个打开文件按钮后根本看不到选择文件的对话框,而且该按钮所在的模态对话框也瞬间关闭了。
重写了模态对话框的onclose方法,依然没用。
protected override void OnClosing(CancelEventArgs e)
{
DialogResult result= DialogResult.No;
if(btn_ok.DialogResult == DialogResult.OK)
{
result = DialogResult.OK;
}
else if (btn_cancel.DialogResult == DialogResult.Cancel)
{
result = DialogResult.Cancel;
}
else //if(openFileDialog1.ShowDialog == DialogResult.No)
{
result = DialogResult.No;
}
if (result == DialogResult.No ) //DialogResult.OK || result == DialogResult.Cancel
{
e.Cancel = true; //取消关闭操作
}
else
{
e.Cancel = false;
//base.OnClosing(e);
}
}
我希望在点击确定、取消按钮时模态窗口才关闭,点击打开文件选择文件时显示openfilediaglog对话框,且模态窗口不关闭。