Problem with FolderBrowserDialog - c#

If the dialog click Make new folder, just start editing the name just create a folder and click OK, OK dialogrezalt returns, but in the property SelectedPath he will name the folder New folder, then there is the name of the default
This happens because when we create, just edit and click OK, this property is not updated and the method ShowDialog () returns.
How fix this problem?
Thank you!

I had the same problem - if you created a new Folder with the FolderBrowseDialog, the .SelectedPath showed "xxx\NewFolder" not whatever new name the user had given.
The problem went away once I explicitly gave the command, prior to displaying the dialog,
MyFolderBrowser.ShowNewFolderButton = True

I failed to simulate the problem you are describing, I have tested it:
Create a new Form Form1 add button1 to it and in the button1.Click handler copy this code:
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.SelectedPath;
Console.WriteLine(path);//will not print new folder if the file renamed.
}
}
}
It worked as expected either by creating a new folder and press enter two times. or by creating a new folder and click ok.
Are you using a third party UI Controls, theams...
Edit: You stated:
Yes, if this sample run at windows application, it work correct. But
my application is Excel add-in. And FolderBrowserDialog work that I
write at started post
So you are using a third party "Excel add-in", When using a third party with FolderBrowserDialog or OpenFileDialog.. you may notice a strange behavior depending on the third party..
The solution for the problem you described is either by disabling ShowNewFolderButton or implement your own custom OpenFileDialog.

Related

C# bring Form to Front following File Dialog

When I start up the program I have added code to open a file dialog box, but doing this results in the main form being sent behind Visual Studio(and other open programs) once a file has been selected.
I have tried using this.BringToFront() but this doesn't seem to work.
The program currently only has one form as well, how would I bring this to the front when the program starts?
public Form1()
{
InitializeComponent();
InitialiseDataGrid();
selectFile();
readData();
this.BringToFront();
}
selectFile() is a function that selects a file using a file dialog box,
readData() is a function that reads the data from the text file into a dataGridView.
You should past the owner window's instance while Opening The dialog window. example code:
var file = new OpenFileDialog();
file.ShowDialog(this);
You can use
this.TopMost = true;
You're juggling with different applications: VS and your program. The released version of the program probably won't run through VS anyway.
Bring it to the foreground:
this.Activate();
Use it with caution.

How to show image icons in ViewList when user selects a folder from the FolderBrowserDialog?

So basically what I want is the user presses a browse button and a FolderBroswerDialog pops up. The user then selects a folder and the ViewList is then populated with all the images in that folder in Icon view. How can I do this? The code I currently have will select all the files from a folder and display them in the ListView, however there are no icons. How can I get icons?
Here is the code I currently have...
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog browsefolder = new FolderBrowserDialog();
if (browsefolder.ShowDialog() == DialogResult.OK)
{
listView1.Items.Clear();
string[] myfiles = Directory.GetFiles(folderPicker.SelectedPath);
foreach (string file in myfiles)
{
string fileName = Path.GetFileNameWithoutExtension(file);
ListViewItem myitem = new ListViewItem(fileName);
myitem.Tag = file;
listView1.Items.Add(myitem);
}
}
}
This is not so easy to do in an accurate and performant way. The quick and dirty way is to use Icon.ExtractAssociatedIcon() and add the returned icon to the ImageList associated with the list view. But you won't get the exact same icons you'd see in Explorer. That requires pinvoking SHGetFileInfo(), painful to do yourself but the code is easy to google.
An entirely different approach is to embed the Explorer window into your own form instead of using a ListView. With the major advantages that you'll get the exact same look and you'll automatically get the background thread that looks up icons while your program keeps responsive. With the disadvantage that this won't work for XP. The classes you need are part of the Windows API Code Pack.

Ookii VistaFolderBrowserDialog and getting selected folder

I am trying to use the Ookii dialog pack to spawn the new Vista style folder selection dialog. That all works with this simple code:
VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;
dlg.ShowDialog();
However, I cannot see any way of knowing when the user has selected a folder as there are no events on this object. I could poll for changes in SelectedPath, but that seems a terribly inefficient way of doing things.
Is there some generic C# trick I have missed to enable me to know when a user has selected a folder and therefore update other fields appropriately?
Try
VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = Properties.Settings.Default.StoreFolder;
dlg.ShowNewFolderButton = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = dlg.SelectedPath;
}
Simple answer from 2023 for WPF for future people who hate sifting through stackoverflows endless incorrect answers and needless fluff.
This answer uses Ookii.Dialogs.WPF NuGet package you can install from right inside visual studio. At the top left click Project -> Manage NuGet Packages... -> Go to browse tab and search OOkii.Dialogs.Wpf then install it. Now this code will work. :)
You can directly copy paste this.
VistaFolderBrowserDialog FolderSelect = new VistaFolderBrowserDialog(); //This starts folder selection using Ookii.Dialogs.WPF NuGet Package
FolderSelect.Description = "Please select the folder"; //This sets a description to help remind the user what their looking for.
FolderSelect.UseDescriptionForTitle = true; //This enables the description to appear.
if ((bool)FolderSelect.ShowDialog(this)) //This triggers the folder selection screen, and if the user does not cancel out...
{
TextBoxGameFolder.Text = FolderSelect.SelectedPath; //...then this happens.
}
```

How to pop up a text file in C#

This small program shows how to open a txt file on the hard drive, is there a way I can have a procedure that when I click a button a txt file pops up, may someone help me with it or how I can go about it....
This is the procedure below
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.ShowDialog();
textBox1.Text = of.FileName;
}
May somebody tell me what I can do or give some material to read kind of new in C#
Assuming that you like opening notepad with the text file showing on it, you could use:
System.Diagnostics.Process.Start(of.FileName);
This will open the file using the default text editor of the computer.
EDIT
According to your comment, you should the do it like this:
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(#"C:\dir1\dir2\yourfile.txt");
}
Obviusly, you should replace that with the path for your specific file.
Try
MessageBox.Show(File.ReadAllText(of.FileName));
After it, try to learn each component of the statement, what it does.
BTW,
You also need:
if (of.ShowDialog() == DialogResult.OK)
before that, to avoid displaying message in case user clicks Cancel.
Based on the clarification, this is pretty straightforward. Just create a new form class that contains a textbox (and probably a Close button). You'll want a property on the form that will set the textbox. You can launch the this form from your button event handler (the one you have in your example) like this:
using(var myForm = new TextBoxForm()) {
myForm.TextFileContents = <file contents>
myForm.ShowDialog();
}
As for reading in the file contents, you'll want to use File.ReadAllText() as described in Daniel's answer. See the MSDN documentation for more on that. I'll leave the remaining details as an exercise to the reader.

SaveFileDialog bug in WPF

I'm using Microsoft.Win32.SaveFileDialog class to save my files. When I saved file, and minimize my app, I can't restore it back. It happens only after when used Microsoft.Win32.SaveFileDialog. Here is code:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = String.Format("{0} {1} {2}", ev["b"], ev["a"], ev["c"]);
dlg.DefaultExt = ".csv";
dlg.Filter = "Supported format (.csv)|*.csv";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string s = dlg.FileName;
//other code
}
File saves successfully, but I don't know how to solve problem with minimizing. Does anybody knows what it could be?
WPF has all kinds of weird modality issues when you show dialogs without parent windows. I haven't seen this directly with the SaveFileDialog, but I have seen similar behavior with other dialogs. Try using the overload of .ShowDialog() where you pass in the parent window.
I encountered also a strange modality problem with WPF and the Win32 SaveFileDialog / OpenFileDialog.
What happens:
The modal state is violated / gets lost completely and the main window can be clicked while the OpenFileDialog is opened with ShowDialog()
When does it happen:
There was a task running before the OpenFileDialog opens
The debugger breaks into a breakpoint before running the task
Just create a simple WPF Application with a button click event:
private void Button_Click(object sender, RoutedEventArgs e)
{ // <-- Breakpoint sits here
Task.Run(() => {}).Wait();
new Microsoft.Win32.OpenFileDialog().ShowDialog();
}
Using the overloaded ShowDialog(Window owner) function solves this problem.

Categories

Resources