Windows Forms FolderBrowserDialog hangs the application - c#

With winforms, when I right click on a folder or try to delete a folder within the FolderBrowserDialog the window becomes irresponsive and I've to force-close it.
Here's the code:
private void btnOpenFileDialog_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = txtBoxLog.Text;
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
txtBoxLog.Text = folderBrowserDialog1.SelectedPath;
}
}

The problem was system wide, so the control was correctly behaving in an incorrect way (irony).

Related

How can I use FolderBrowserDialog in wpf?

private void btnRadarFolder_Click(object sender, RoutedEventArgs e)
{
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
if(result.)
}
}
The first time I tried to use the System.Windows.Forms.FolderBrowserDialog I got error that Forms don't exist. so I installed the ookii-dialogs-wpf package and once installed the errors are gone but I'm not sure what to do next. The variable result don't have an OK or any properties to continue with the dialog.
When I click the button, it's opening the dialog browser like in the old vista style, but I'm not sure how to handle it in the button/s click event.

Change Path of OpenFileDialog

At the moment I am using this here in my WPF App which works as it should.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
It remembers the last path I was in but I want to change it to a set path now.
I have 3 Radiobuttons and each Radiobutton should lead to a different path so I thought about doing it with a variable I give to the openFileDialog but I am not sure on how to go with that. Has anyone done this and can give me a lead on it ?
You can set IntitialDirectory to the folder you want in code where you show the dialog.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.IntitialDirectory = youFolderPath;
openFileDialogPresentations.ShowDialog();
}
The standard file dialogs have an InitialDirectory property that determines in which folder the dialog opens.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.InitialDirectory = #"X:\Data\Presentations";
openFileDialogPresentations.ShowDialog();
}
You can do it by using InitialDirectory property. You can set three different paths for radio buttons
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory=#"D:\MyDir";
dialog.ShowDialog();
}

How do I show a directory path in a textbox when selected by the user?

First, I am using Visual Studio 2013 and coding in C# to develop a Windows Form Application. I have added the "System.IO" namespace.
I need to show a directory path in a textbox when selected by the user.
The code works correctly to where the user selects a folder from a popup
and presses the OK button, which then displays the number of files within
that folder -- but the folder path does NOT get displayed as I desired.
Code looks like this:
private void button1_Click(object sender, EventArgs e)
{
//
// This event handler was created by clicking the button in the application GUI.
//
DialogResult button1_Click = folderBrowserDialog1.ShowDialog();
if (button1_Click == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// A message pops up and identifies the number of files found within that folder.
//
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string path;
path = folderBrowserDialog1.SelectedPath;
// folderBrowserDialog1.ShowDialog(); // NOT SURE ABOUT USING THIS!
textBox1.Text = path;
}
You could just add this to the end of your button1_Click method (inside the if block):
textBox1.Text = folderBrowserDialog1.SelectedPath;

Something wrong with my Drag_Enter/Drag_Drop events

I set two events in my code, Drag_Drop and Drag_Event, and everytime I try to drag something into the form, the 'circle with a line through it' symbol appears. Funny thing is, I used the same exact code in another project, and it worked perfectly. I registered the events and everything. Here is my code:
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (toSaveIcon)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
if (toSaveIcon)
{
string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string path in filePath)
{
icon = Icon.ExtractAssociatedIcon(path);
Debug.WriteLine("Icon saved successfully");
}
}
}
Funny thing is, I used the same exact code in another project, and it worked perfectly.
I had this issue before, I fixed it by running Visual Studio as a normal user. Make sure it is not running as an Administrator if you are running from visual studio.
You can also run the app directly from the bin folder , you will have a better idea.

How to Browse a particular folder

I have a folderBrowserDialog control and i have a button named click
i did like this way
private void click_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
}
If what you wish to accomplish is to automatically display a folder of your choice whenever the FolderBrowserDialog is shown, then you can use the following code:
private void click_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
f.Description = "Please browse to, and select a folder";
f.RootFolder = #"C:\Name Of Folder You Want Displayed\";
using(f)
{
if(f.ShowDialog() == DialogResult.OK)
{
// Do something when the user clicks OK or Open.
}
}
}
Or, you could simply add a FolderBrowserDialog to your Form from the Visual Studio ToolBox and click on the Properties tab, and then click the down arrow to the right-hand side of the 'Root Folder' option and select one of the listed folders from the drop-down menu.
Hope this helps.
You can use a custom build Folder Browser Dialog/Control for this. Try this: http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx

Categories