第二次单击按钮时,停止向组合框添加项目 [英] stop adding item to combobox when button is click second time

查看:71
本文介绍了第二次单击按钮时,停止向组合框添加项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个按钮和一个组合框.第一个是浏览excel文件,称为Browsebutton.第二个按钮是选择工作表名称,称为selectButton.但是,如果我改变主意并浏览其他文件并选择其他工作表.工作表不断加在组合框上.当我想从不同的文件中获取不同的图纸时,如何清除组合框?


谢谢,

这是我的代码.


I have 2 buttons and a combobox. The first one is browse for the excel file, called Browsebutton. The second button is to select the sheet name, called selectButton.However, if i change my mind and browse different file and select different sheet. The sheet keep adding up on the combobox. How can I clear the combo box when I want to get different sheet from different file?


thanks,

Here are my code.


private void browseButton_Click(object sender, EventArgs e)
       {
           try
           {
               OpenFileDialog openFdlg = new OpenFileDialog();

               openFdlg.InitialDirectory = "C:\\";
               openFdlg.Title = "Browse your file";

               openFdlg.CheckFileExists = true;
               openFdlg.CheckFileExists = true;

               openFdlg.DefaultExt = "xls";
               openFdlg.Filter = "Excel 2003 (*.xls)|*xls|Excel 2007(*.xlsx)|*.xlsx|DBF (*.dbf)|*dbf|CSV(*.csv)|*.csv|All files(*.*)|*.*";
               openFdlg.FilterIndex = 2;
               openFdlg.RestoreDirectory = true;

               openFdlg.ReadOnlyChecked = true;
               openFdlg.ShowReadOnly = true;

               if (openFdlg.ShowDialog() == DialogResult.OK)
               {
                   filenameTextbox.Text = openFdlg.FileName;
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }







private void getSheetButton_Click(object sender, EventArgs e)
       {
           try
           {
               fileName = filenameTextbox.Text;
               if (fileName.EndsWith(".xlsx"))
               {
                   //connection string for xlsx file
                   excelConn = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
                           fileName + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"");
                   GetSheetName(excelConn);

               }
               if (fileName.EndsWith(".xls"))
               {
                   //connection string for excel 97-2003 file.
                   excelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName
                       + @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
                   GetSheetName(excelConn);
               }
               if(fileName.EndsWith(".csv"))
               {
                   csvConn = "Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(fileName)
                       + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"";
                   GetSheetName(csvConn);
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
           //sheetNameCombobox.Items.Clear();
       }

推荐答案

ComboBox.Items.Clear()

????


只需在GetSheetName()方法.


这篇关于第二次单击按钮时,停止向组合框添加项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆