C# 如何在Webforms中的FolderBrowserDialog控件中插入Messagebox

C# 如何在Webforms中的FolderBrowserDialog控件中插入Messagebox,c#,winforms,folderbrowserdialog,C#,Winforms,Folderbrowserdialog,我有一个FolderBrowserDialog控制器,在该控制器中,如果用户选择根驱动器(如C://或E://),它应该会发出一些警报 我们可以通过添加Messagebox来管理这一点,但添加Messagebox的问题是,一旦我们在文件夹对话框中给出ok,它就会关闭,用户会收到警报,因此不需要发出警报,因为我们将结果作为对话框结果传递为ok System.ComponentModel.ComponentResourceManager resources = new System.Componen

我有一个FolderBrowserDialog控制器,在该控制器中,如果用户选择根驱动器(如C://或E://),它应该会发出一些警报

我们可以通过添加Messagebox来管理这一点,但添加Messagebox的问题是,一旦我们在文件夹对话框中给出ok,它就会关闭,用户会收到警报,因此不需要发出警报,因为我们将结果作为对话框结果传递为ok

System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(HeadlessForm));
this.SuspendLayout();
// 
// HeadlessForm
// 
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(124, 0);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "HeadlessForm";
this.Opacity = 0D;
this.Text = "TESTTool";
this.TopMost = true;
this.Shown += new System.EventHandler(this.HeadlessForm_Shown);
this.ResumeLayout(false);
上面是基本的表单代码,我们正在使用下面的代码过度编写,以将其转换为文件夹拨号

var settings = message.RequestData.FirstOrDefault<UserInterfaceSelectPathRequestModel>();

if (settings == null)
    return;

TaskThreadHelper.StartStaTask(() =>
{
    using (var form = new HeadlessForm())
    {
        form.BringToFront();
        form.Show();

using (var dialog = new FolderBrowserDialog { SelectedPath = settings.SelectedPath, Description = settings.Description })
{
    var result = dialog.ShowDialog(form);                          
    var userDerives = Environment.GetLogicalDrives();
    bool b = userDerives.Any(dialog.SelectedPath.Contains);
    if (b == true)
    {
        string alertMessage = string.Format("You have selected Logical Drive {0},Please select any specific folder", dialog.SelectedPath);
        MessageBox.Show(alertMessage, "Direct Logical Drive Selection is Not    Possible",MessageBoxButtons.RetryCancel);
        result = DialogResult.Retry;
    }
var settings=message.RequestData.FirstOrDefault();
如果(设置==null)
返回;
TaskThreadHelper.StartStatTask(()=>
{
使用(var form=new HeadlessForm())
{
形式.BringToFront();
form.Show();
使用(var dialog=newfolderbrowserdialog{SelectedPath=settings.SelectedPath,Description=settings.Description})
{
var result=dialog.ShowDialog(表单);
var userDerives=Environment.GetLogicalDrives();
bool b=userDerives.Any(dialog.SelectedPath.Contains);
如果(b==true)
{
string alertMessage=string.Format(“您已选择逻辑驱动器{0},请选择任何特定文件夹”,dialog.SelectedPath);
MessageBox.Show(alertMessage,“无法直接选择逻辑驱动器”,MessageBox按钮。RetryCancel);
结果=对话框结果。重试;
}

因此,最后我们需要一个文件夹对话框,当用户选择逻辑驱动器时,它应该显示其无效选择,并要求他们选择特定的文件夹。

如果不使用任何自定义对话框,我们只有一个选项,即使用do while循环

var result = DialogResult.No;
do
{
  if (result != DialogResult.Cancel)
    {
     result = dialog.ShowDialog(form);
     var userDerives = Environment.GetLogicalDrives();
     bool b = userDerives.Contains(dialog.SelectedPath);
       if (b == true && result !=DialogResult.Cancel)
        {
        string alertMessage = string.Format("Select Correct path");
        MessageBox.Show(alertMessage, "Invalid build location");
        result = DialogResult.No;
        }         
        else
        {
        return;
      }
  } while (result != DialogResult.OK);

你可以让你自己选择的diaBoxxt不清楚为什么你不能简单地调用SeaDeCudio()。使用Dewhile while循环。并且考虑它是错误的UI小工具,选择驱动器只需要一个组合框。无法使用
FolderBrowseDialog
执行所需操作。您可以使用第三方对话框,也可以自己创建自定义对话框。请参阅。代码项目有许多自定义对话框项目。搜索web:codeproject c#custom dialog