OpenFileDialog不起作用 [英] OpenFileDialog is not working

查看:217
本文介绍了OpenFileDialog不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hai,



OpenFileDialog无法在Windows窗体中运行。当我使用openfiledialog控件时它的敲击(它不是开放)。



我想我错过了一些属性。请帮助我。





还有一个这个在安装程序设置中不起作用。没有设置安装程序,相同的胜利形式将完全独立工作。





谢谢

解决方案

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsApplication1
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object <> span sender,EventArgs e)
{
OpenFileDialog im = new OpenFileDialog();
im.Title = 选择图片;
im.InitialDirectory = @ f:\;
im.Filter = 所有文件(*。*)| *。* |所有文件(*。 。*)| *。*;
im.FilterIndex = 100 ;
im.RestoreDirectory = true ;
if (im.ShowDialog()== DialogResult.OK)
{
txtUploadFile.Text = im.FileName;
}
}
}
}


问题在于MSI线程作为MTA线程运行,但FileDialog.ShowDialog需要STA线程。要实现此目的,您需要启动STA后台线程并从该线程调用该对话框。基本上我做了以下:

- 添加了DialogState类。这跟踪线程的输入和输出。

- 添加了STAShowDialog函数。此函数采用FileDialog,在后台STA线程上调用ShowDialog,然后返回结果。

- 从DialogResult更改了呼叫ret = frm.ShowDialog(); to DialogResult ret = STAShowDialog(frm);





查看此帖子以获取完整信息http://blogs.msdn.com/b/smondal/archive/2012/12/31/10059279.aspx [ ^ ] 。







代码lang改变[/ edit] < /小>


Hai,

OpenFileDialog is not working in windows form. Its strucking (its not opening) the winform when I use openfiledialog control.

I think i missed some properties.Plese help me.


And one more this is not working inside installer setup only. The same win form will be work perfectly alone without setup installer.


Thanks

解决方案

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog im = new OpenFileDialog();
            im.Title = "to select image";
            im.InitialDirectory = @"f:\";
            im.Filter = "All files(*.*)|*.*|All files(*.*)|*.*";
            im.FilterIndex = 100;
            im.RestoreDirectory = true;
            if (im.ShowDialog() == DialogResult.OK)
            {
                txtUploadFile.Text = im.FileName;
            }
        }
    }
}


The problem is that the MSI thread is running as an MTA thread, but the FileDialog.ShowDialog requires an STA thread.  To achieve this you will need to start a STA background thread and call the dialog from that thread. Basically I did the following:

-          Added the DialogState class.  This keeps track of the input and output for the thread.

-          Added the STAShowDialog function.  This function takes a FileDialog, calls ShowDialog on a background STA thread, and then returns the results.

-          Changed the call from DialogResult ret = frm.ShowDialog(); to DialogResult ret = STAShowDialog(frm);



Check this post for a complete information http://blogs.msdn.com/b/smondal/archive/2012/12/31/10059279.aspx[^].



[edit]Code lang changed[/edit]


这篇关于OpenFileDialog不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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