求助openFileDialog1.ShowDialog() 不执行
private void button8_Click(object sender, EventArgs e){
openFileDialog1.Filter = "Excel文件|*.xls";
openFileDialog1.Title = "请选择要导入的文件";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
inFilePath = openFileDialog1.FileName;
label6.Text = "读取中";
readExcel = new System.Threading.Thread(new System.Threading.ThreadStart(getExcelData));
readExcel.Start();
}
}
这段代码单独用的时候没有什么问题 放到我的程序里面 大多数时候没有问题可以执行if语句里面的代码,但是有些时候就不执行,单步调试时发现openFileDialog1.ShowDialog()不反应 --------------------编程问答-------------------- 你不是有判断吗?当表达式不成立的时候if里面的语句肯定不执行啊,你如果打开的那个文件对话框,选择文件之后点确定按钮就会执行if里面的语句,否则是不会执行的啊... --------------------编程问答--------------------
我这边出现的情况是openFileDialog1.ShowDialog() 没有弹出窗口让我选择确定
--------------------编程问答-------------------- 此句有误:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
--------------------编程问答--------------------
this.openFileDialog1.InitialDirectory = "C:\\";
this.openFileDialog1.Filter = "excel文件(*.xls)|*.xls";
this.openFileDialog1.FilterIndex = 2;
this.openFileDialog1.RestoreDirectory = true;
this.openFileDialog1.Title = "打开excel文件";
if (openFileDialog1.ShowDialog()==DialogResult.OK)
string sss = openFileDialog1.FileName;
这样写一点事也没有,是不是你的其他地方写的不好啊 --------------------编程问答-------------------- openFileDialog1初始化放在事件中(每次new一下)试试有没问题
如果没有了,说明你其他地方对这个对象的操作导致的 --------------------编程问答--------------------
Stream myStream = null;--------------------编程问答--------------------
this.openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); //设置对话框显示的初始目录
this.openFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; //设置要查找的文件的类型,这里要查找的是图片类型,如jpg、gif等
this.openFileDialog1.FilterIndex = 2;
this.openFileDialog1.Multiselect = true; //设置为可以选择多个图片文件
this.openFileDialog1.RestoreDirectory = true;
DialogResult dr = this.openFileDialog1.ShowDialog();
string fileName = this.openFileDialog1.FileName; //获取对话框中选中的文件的路径和名称
string extendedName = Path.GetExtension(fileName).ToLower(); //得到后缀
string[] allowExtended ={ ".jpg", ".gif", ".png", ".bmp", ".pdf", ".jpeg", ".tif" };
if (extendedName == allowExtended[0] || extendedName == allowExtended[1] || extendedName == allowExtended[2]
|| extendedName == allowExtended[3] || extendedName == allowExtended[4] || extendedName == allowExtended[5]
|| extendedName == allowExtended[6])
{
if (dr == DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
//循环选中多个文件进行加载
for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
{
fileName = openFileDialog1.FileNames.GetValue(i).ToString();
using (myStream)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
byte[] imgbyte = new byte[fs.Length];
fs.Read(imgbyte, 0, imgbyte.Length);
fs.Close();
myStream = new MemoryStream(imgbyte);
Bindtest(myStream, fileName);
myStream.Close();
}
}
}
}
}
else
{
MessageBox.Show("请上传jpg、gif、png、bmp格式的图片。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
--------------------编程问答--------------------
public void Open()
{
OpenFileDialog openFileDialog1=new OpenFileDialog ();
openFileDialog1.Filter = "Excel文件|*.xls";
openFileDialog1.Title = "请选择要导入的文件";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
inFilePath = openFileDialog1.FileName;
label6.Text = "读取中";
readExcel = new System.Threading.Thread(new System.Threading.ThreadStart(getExcelData));
readExcel.Start();
}
???请指教 --------------------编程问答-------------------- 谢谢 大家了 。每个人的方法我都试过了 但是在我这边都还是打不开 晚些给大家分 我想等等还有什么其他的解决办法 --------------------编程问答-------------------- 光看着一小段代码实在看不出什么错误呀 --------------------编程问答-------------------- if (openFileDialog1.ShowDialog() == DialogResult.OK):这没有错啊,是不是弹出打开文件的对话框之前已经有对话框存在呢。光看这一小段代码还真的不知道错在哪? --------------------编程问答-------------------- 你其他地方有没有错 , 有时候报错不一定在指定的地方,还有可能是在其他地方的 --------------------编程问答--------------------
if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
{
}
这样试试呢LZ --------------------编程问答-------------------- 再不行 加我Q453367672 包解决
补充:.NET技术 , C#