技巧:.net中的特殊对话框调用(数据库选择对话框&目录选择对话框)

类别:.NET开发 点击:0 评论:0 推荐:
作者:whxbb

数据库选择对话框。

using System;

using System.Windows.Forms;

namespace Whxbb.Windows.Forms
{
    /// <summary>
    /// 数据库选择对话框。
    /// </summary>
    public class ChooseDatabaseDialog
    {
        private string _connectionString = String.Empty;
        /// <summary>
        /// 获取数据库连接字符串。
        /// </summary>
        public string ConnectionString
        {
            get
            {
                if (_connectionString == string.Empty && _connectionString != "")
                {
                    return _connectionString;
                }
                else
                {
                    string[] tmp = _connectionString.Split(';');
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for(int i = 1; i < tmp.Length; i++)
                    {
                        if (i != 1)
                            sb.Append(";" + tmp[i]);
                        else
                            sb.Append(tmp[i]);
                    }
                    return sb.ToString();
                }

            }
        }

        private  MSDASC.DataLinks _dataLinks;
        private ADODB.Connection _connection;

        /// <summary>
        /// 无参数构造方法。
        /// </summary>
        public ChooseDatabaseDialog()
        {
            _dataLinks = new MSDASC.DataLinksClass();
        }

        /// <summary>
        /// 显示对话框。
        /// </summary>
        /// <returns></returns>
        public DialogResult ShowDialog()
        {
            try
            {
                _connection = new ADODB.ConnectionClass();
                //_connection = (ADODB.Connection)_dataLinks.PromptNew();
                _connection.Provider = "SQLOLEDB.1";                

                object obj = _connection as object;
                _dataLinks.PromptEdit(ref obj);
                if (_connection == null)
                {
                    return DialogResult.Cancel;
                }
                else
                {
                    _connectionString = _connection.ConnectionString;
                    if (_connectionString == "" || _connectionString == null)
                    {
                    
                        return DialogResult.Cancel;
                    }
                    else
                    {
                        return DialogResult.OK;
                    }
                }
            }
            catch (Exception e)
            {
                return DialogResult.Abort;
            }          
        }
    }

}

目录选择对话框。

using System;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace Whxbb.Windows.Forms
{
    /// <summary>
    /// 目录选择对话框。
    /// </summary>
    public class ChooseDirectoryDialog : FolderNameEditor
    {
        private FolderBrowser _browser;

        private string _description = "Choose Directory";
        public string Description
        {
            set { _description = value; }
            get { return _description; }
        }

        private string _choosedPath = string.Empty;
        /// <summary>
        /// 被选择的目录。
        /// </summary>
        public string ChoosedPath
        {
            get { return _choosedPath; }
        }

        private string _defaultPath = string.Empty;
        /// <summary>
        /// 默认路径。
        /// </summary>
        public string DefaultPath
        {
            get { return _defaultPath; }
            set
            {
                _browser.StartLocation = FolderBrowserFolder.MyComputer;
                _defaultPath = value;
            }
        }

        /// <summary>
        /// 无参数构造方法。
        /// </summary>
        public ChooseDirectoryDialog()
        {
            _browser = new FolderBrowser();
        }

        /// <summary>
        /// 指定对话框描述创建一个对话框实例。
        /// </summary>
        /// <param name="dialogTitle"></param>
        public ChooseDirectoryDialog(string description) : this()
        {
            this._description = description;
        }

        /// <summary>
        /// 显示对话框。
        /// </summary>
        /// <returns></returns>
        public DialogResult ShowDialog()
        {
            _browser.Description = this.Description;
            _browser.Style = FolderBrowserStyles.ShowTextBox;
            _browser.StartLocation = FolderBrowserFolder.MyComputer;

            DialogResult result = _browser.ShowDialog();
            if (result == DialogResult.OK)
            {
                _choosedPath = _browser.DirectoryPath;
            }
            else
            {
                _choosedPath = String.Empty;
            }

            return result;
        }
    }
}

本文地址:http://com.8s8s.com/it/it45698.htm