vb6には、ドライブ名を選択できるドロップボックス/コンボボックスに似たコントロールがあったことを覚えています。これにより、リストボックス内のファイルを列挙する別のコントロールを設定できるイベントが発生します。(drive.eventで、files.path = drive.pathを実行して、この効果を取得します)。
C#にこのようなものはありますか?使用可能なドライブのリストをドロップダウンし、変更されたときにイベントを発生させるコントロール?
これを行うための組み込みのコントロールはありませんが、標準のComboBoxを使用すると非常に簡単に実行できます。フォームに1つドロップし、そのDropDownStyleをDropDownListに変更して編集を防ぎ、フォームのLoadイベントに次の行を追加します。
comboBox1.DataSource = Environment.GetLogicalDrives();
これで、SelectedValueChangedイベントを処理して、誰かが選択したドライブを変更したときにアクションを実行できます。
この質問に答えた後、私はこれを行う別の(より良い?)方法を見つけました。DriveInfo.GetDrives()メソッドを使用して、ドライブを列挙し、結果をComboBoxにバインドできます。そうすれば、どのドライブが表示されるかを制限できます。だからあなたはこれから始めることができます:
comboBox1.DataSource = System.IO.DriveInfo.GetDrives();
comboBox1.DisplayMember = "Name";
これで、comboBox1.SelectedValueのタイプはDriveInfoになるため、選択したゲームに関するより多くの情報を取得できます。また、ネットワークドライブのみを表示したい場合は、今すぐこれを行うことができます。
comboBox1.DataSource = System.IO.DriveInfo.GetDrives()
.Where(d => d.DriveType == System.IO.DriveType.Network);
comboBox1.DisplayMember = "Name";
DriveInfoメソッドの方がはるかに柔軟だと思います。
マットハミルトンの答えは非常に正しかったのですが、質問自体は正しいのだろうかと思います。なぜなら、なぜあなたはそのようなコントロールが欲しいのですか?正直なところ、Windows95は非常に感じがします。Windowsユーザーエクスペリエンスインタラクションガイドラインをご覧ください:http://msdn.microsoft.com/en-us/library/aa511258.aspx
特に一般的なダイアログに関するセクション:http: //msdn.microsoft.com/en-us/library/aa511274.aspx
私はこれに次のようにアプローチします:
foreach (var Drives in Environment.GetLogicalDrives())
{
DriveInfo DriveInf = new DriveInfo(Drives);
if (DriveInf.IsReady == true)
{
comboBox1.Items.Add(DriveInf.Name);
}
}
Drive.IsReady
uの助けを借りて、DeviceNotReady
問題を回避することができDeviceUnavailable
ます。
ボーナス:ComboBox
これは、ドライブTreeView
用、フォルダ用、最後のファイル用
を含む簡単な「ChooseFile」の例でもありListBox
ます。
namespace ChosenFile
{
public partial class Form1 : Form
{
// Form1 FormLoad
//
public Form1()
{
InitializeComponent();
foreach (var Drives in Environment.GetLogicalDrives())
{
DriveInfo DriveInf = new DriveInfo(Drives);
if (DriveInf.IsReady == true)
{
comboBox1.Items.Add(DriveInf.Name);
}
}
}
// ComboBox1 (Drives)
//
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
ListDirectory(treeView1, comboBox1.SelectedItem.ToString());
}
}
// ListDirectory Function (Recursive Approach):
//
private void ListDirectory(TreeView treeView, string path)
{
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
}
// Create Directory Node
//
private static TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
try
{
foreach (var directory in directoryInfo.GetDirectories())
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
}
catch (Exception ex)
{
UnauthorizedAccessException Uaex = new UnauthorizedAccessException();
if (ex == Uaex)
{
MessageBox.Show(Uaex.Message);
}
}
return directoryNode;
}
// TreeView
//
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
listBox1.Items.Clear();
listBox1.Refresh();
PopulateListBox(listBox1, treeView1.SelectedNode.FullPath.ToString(), "*.pdf");
}
// PopulateListBox Function
//
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
try
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while attempting to load the file. The error is:"
+ System.Environment.NewLine + ex.ToString() + System.Environment.NewLine);
}
}
// ListBox1
//
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
//do smt here!
MessageBox.Show(listBox1.SelectedItem.ToString());
}
}
}
}
VB6の昔のように。