qdreamer 2021-12-27 12:06 采纳率: 33.3%
浏览 102

c# winform下一个模态窗口,打算在这个模态窗口中用一个按钮打开一个选择文件对话框,结果不理想。结果是:每次点击这个按钮后

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对话框,且模态窗口不关闭。
  • 写回答

2条回答 默认 最新

  • Admini$trat0r .net领域新星创作者 2021-12-27 12:26
    关注

    设置好owner父窗体 showdialog就行了

    评论

报告相同问题?

问题事件

  • 创建了问题 12月27日

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大