I have a FolderBrowserDialog controller in which if user selects a Root drive such as C:// or E:// it should throw some alerts.
We can manage this by adding a Messagebox but the problem of adding message box is that once if we give ok in the folder dialogue then it will be closed and user will get alert so no use of alerting since we are passing the result as dialogue result as 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);
Above is the basic form code and we are over writing with the below code to convert it as 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;
}
So Finally we need a Folder Dialogue box in which when user selects the Logical drives then it should display the its invalid selection and ask them select particular folder.
Without using any custom dialog we have only one option that is using do while loop.
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);