如何在 Webforms 中的 FolderBrowserDialog 控件中注入 Messagebox

How to inject a Messagebox inside a FolderBrowserDialog control in Webforms

我有一个 FolderBrowserDialog 控制器,如果用户 select 是根驱动器,例如 C:// 或 E://,它应该会发出一些警报。

我们可以通过添加一个 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);

以上是基本的表单代码,我们用下面的代码覆盖,将其转换为Folder Dialgoue。

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;
    }

所以最后我们需要一个文件夹对话框,当用户 select 逻辑驱动器时,它应该显示其无效 select 离子并询问他们 select 特定文件夹。

在不使用任何自定义对话框的情况下,我们只有一个选项,即使用 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);