传动箱

 

介绍 驱动箱控制允许

  

 

用户在运行时选择有效的磁盘驱动器。此控件可用于显示a 用户系统中所有有效驱动器的列表。您可以创建对话框,使用户能够从列表中打开文件 任何可用驱动器中的磁盘上的文件。此控件非常类似于 驱动箱控制是我们非常熟悉的。 开始 为了让驱动箱显示有效的磁盘驱动器,我们需要找出用户系统中可用的驱动器。一旦获得可用的驱动器信息,它可以显示在一个组合框中。 目录类 为了获得可用的驱动器,. net framework为我们提供了类目录。中定义的类 系统。IO命名空间。这个类公开用于获取本地驱动器、创建、移动和枚举的例程 目录和子目录。我们感兴趣的方法是目录法。GetLogicalDrives方法。 此方法的签名为 隐藏,复制Code

public static string[] GetLogicalDrives();

此方法以“C:\”的形式检索此机器上逻辑驱动器的名称。 隐藏,复制Code

namespace MyControls {
using System;
using System.IO;
using System.WinForms;

public sealed class DriveBox : ComboBox{
    DriveBox(){
        lstCollect = new ObjectCollection(this);
        Style = ComboBoxStyle.DropDownList;
        foreach (String str in Directory.GetLogicalDrives()){    
            str = str.Replace('\\',' ');
            lstCollect.Add(str);
        }
        SelectedIndex = 0;
    }
    public String Drive{
        get{
            return Text;
        }
    }
}
}

上面的代码定义了类DriveBox。DriveBox继承自ComboBox 并因此拥有ComboBox的所有属性。驱动箱设置为下拉列表 风格。添加一个新的属性驱动器,返回当前 选定的驱动器。 使用以下编译器选项编译程序。 隐藏,复制Code

csc filename.cs /out:MyControls.dll /target:library

测试应用程序 现在让我们尝试在一个小型测试应用程序中使用这个驱动箱。这个测试应用程序将显示所有可用的 用户系统中的驱动器。当用户在驱动箱中选择一个驱动器时,应用程序会显示一个提示消息框 当前选择的驱动器。 隐藏,收缩,复制Code

using System;
using System.WinForms;
using System.Drawing;
using MyControls;
    
public class DriveBoxTest: Form{
    private Button btnOK = new Button();
    private DriveBox dbxLocal = new DriveBox();

    public static int Main(string[] args){
        Application.Run(new DriveBoxTest());
                return 0;
    }
        public DriveBoxTest(){
        this.Text = "DriveBox Test Program";

        dbxLocal.Location = new Point(1,10);
        dbxLocal.Size = new Size(80,20);

        btnOK.Location = new Point(90,100);
        btnOK.Size = new Size(80,20);
        btnOK.Text = "OK";

        this.Controls.Add(btnOK);
        this.Controls.Add(dbxLocal);
    
        btnOK.Click += new System.EventHandler(btnOK_Click);
        dbxLocal.SelectedIndexChanged += 
                   new System.EventHandler(dbxLocal_SelectedIndexChanged);
    }
    private void btnOK_Click(object sender, EventArgs evArgs){
        Application.Exit();
    }
    private void dbxLocal_SelectedIndexChanged(object sender,
                                               EventArgs evArgs){
        MessageBox.Show(dbxLocal.Drive,"DriveBox");
    }
}

使用以下编译器选项编译程序。 隐藏,复制Code

csc filename.cs /R:System.dll /R:System.WinForms.dll /R:System.Drawing.dll /R:MyControls.dll 

应用程序的输出将如示例图像所示。 本文转载于:http://www.diyabc.com/frontweb/news172.html

posted @ 2020-08-04 01:57  Dincat  阅读(179)  评论(0编辑  收藏  举报