从openFileDialog浏览窗口中仅选择.txt文件时如何启用按钮 [英] How to enable button when select only .txt file from openFileDialog browse window

查看:110
本文介绍了从openFileDialog浏览窗口中仅选择.txt文件时如何启用按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

试试这个

 private void btnBrowse_Click( object sender,RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.DefaultExt =" .txt" ;;
openFileDialog.Filter ="文本文件(* .txt)| * .txt |所有文件(*。*)| *。*" ;;
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
result = openFileDialog.Filter;
if(openFileDialog.ShowDialog()== true)//打开文件对话框窗口
{
txtpath = openFileDialog.FileName;
FileNameTextBox.Text = txtpath;
string ext = System.IO.Path.GetExtension(txtpath);

if(ext.ToUpper()==" .TXT")
{
btnCompaire.IsEnabled = true;
}


}
}


Hi,

解决方案

try this

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.DefaultExt = ".txt";
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; 
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
result = openFileDialog.Filter;
if (openFileDialog.ShowDialog() == true) //Open file dialog window
{
txtpath = openFileDialog.FileName; 
FileNameTextBox.Text = txtpath;
string ext =  System.IO.Path.GetExtension(txtpath);

if (ext.ToUpper() == ".TXT")
             { 
btnCompaire.IsEnabled = true;
             }


}
} 


这篇关于从openFileDialog浏览窗口中仅选择.txt文件时如何启用按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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