FolderBrowserDialog showing SelectedPath issue - c#

So I have a folder browser dialog and am having issues with the selected path.
So I want the previous folder that was opened to be selected, and I want it to be scrolled down so that the previous folder is visible.
Now the strange thing is that this works fine, but only sometimes. It's completely random. The path is always highlighted, but it doesn't always scroll down.
Sometimes when I start debugging and click browse, it will open up and be scrolled down to where I want. Then I click ok, click browse again and it's completely random whether or not it's scrolled down to where it should be.
Any thoughts?
Edit: I searched around a lot and found this. It seems to be quite similar to my problem.
"I have tried the test app on Vista 32, XP 32, Win 7 32 & 64. It works fine on everything except Windows 7. Both 32 and 64 appear to have the same issue."
They're saying it's a glitch with Windows 7...?

I ended up using the Ookii dialogs folder browser dialog. Honestly it's just much better than the default folder browser. It also comes with an example, showing you how to use it.

this works for me
folderBrowserDialog1.Reset();
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog1.SelectedPath = WorkingFolder;
but only after the second use of the dialog

FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
folderBrowser.Description = "Select Chase 6 Installation Folder";
folderBrowser.RootFolder = Environment.SpecialFolder.ProgramFiles;
folderBrowser.ShowNewFolderButton = false;
if (Directory.Exists(Properties.Settings.Default.defaultChasePath))
{
string x = Properties.Settings.Default.defaultChasePath;
//Use API Flag to set correct path, following tahter a catch all better to check
//enum for full list
RootSetter.SetRootFolder(folderBrowser, RootSetter.CsIdl.FlagDontVerify);
folderBrowser.SelectedPath = x;
}
if (folderBrowser.ShowDialog(this) == DialogResult.OK)
{
string huz = folderBrowser.SelectedPath;
}
I got from this link
How do I open a FolderBrowserDialog at the selected folder?

Set your selected path to the last folder path so that it will scroll down.
if (folderDialog.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.Path = folderDialog.SelectedPath;
Properties.Settings.Default.Save();
}
Change the code inside if condition.

Related

How to get temporary filename when use OpenFileDialog in C# Winform

In C#-WinForms I use a OpenFileDialog. I want to get all files/filenames in a selected folder, without pressing the ok button of the OpenFileDialog.
Is there a good way to achieve this?
if i understood your question in the right way, you want to get the Filenames out of your selected folder. To archive that, you could use a FolderBrowserDialog.
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
if(folderBrowser.ShowDialog() == DialogResult.OK)
{
var path = folderBrowser.SelectedPath;
System.Windows.IO.DirectoryInfo info = new System.Windows.IO.DirectoryInfo(path);
foreach(var file in info.GetFiles())
{
//do what you want with your files.
//example
listbox1.Items.Add(file.Name);
}
}
If you got some further question, feel free to ask.
-- EDIT --
Ok now i understood what you mean.
You want to get the files out of a folder when clicking on it.
Maybe build your own dialog (simple new window). With a treeview on it. And if you click on a treeviewitem, on ther other side it displays you the files and subfolder.

Linking words to windows Explorer c#

Question: How can I make a list of link buttons so that when a user clicks on a link it opens it up in windows explorer.
So I have a rich text box that contains a list of all folder names and I have a list of all the folder paths. I want to be able to click on the folder name and have it linked using the path to the correct folder in windows explorer.
LinkLabel link = new LinkLabel();
link.Text = transfer2;
//link.Text = "something";
link.Name = dirName;
link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + " ");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
I started with this code. I am using a foreach statement to get the folder names and path. I tried to change the name of link so it will appear to the user that they are clicking on folder A, but when you click on folder A it uses the path to open the windows explorer where the folder is from.
Any ideas or help would be greatly appreciated.
Update
I changed the code a little so now it will display, but I cant scroll down. It appears to be only on the surface as I ran something in the rich textbox and it was scrollable while the link stayed on the surface.
I also added a picture so you can see what the problem is. I scrolled a little bit so it would b easy to see.
LinkLabel link = new LinkLabel();
link.Text = dirName;
//link.Text = "something";
link.Name = transfer2;
//link.LinkClicked += new LinkLabelLinkClickedEventHandler(this.link_LinkClicked);
LinkLabel.Link data = new LinkLabel.Link();
data.LinkData = #"C:\";
link.Links.Add(data);
link.AutoSize = true;
link.Location =
this.Display_Rich_Text_Box.GetPositionFromCharIndex(this.Display_Rich_Text_Box.TextLength);
this.Display_Rich_Text_Box.Controls.Add(link);
this.Display_Rich_Text_Box.AppendText(link.Text + "\n");
this.Display_Rich_Text_Box.SelectionStart = this.Display_Rich_Text_Box.TextLength;
Update: I am trying to make essentially a list of hyperlinks so I don't think I can use linklabel as I think it is in a fixed position.
For the first part of your problem, opening Explorer, you can do this on the click event for each item in the list (or the click event of the whole list area, as I describe later):
System.Diagnostics.Process.Start("explorer.exe", "\"" + path + "\"");
(the quote/slash thing is to make sure paths with spaces work)
For the UI bits, I've never even seen LinkLabels before, so I don't know how you got on the road to that, lol! I'm not sure whether you're using WinForms or WPF but in either you'd generally want to use something like a ListBox (or a custom control that behaves/looks precisely how you want, but I'd guess you aren't ready for that). In WPF you could set the ListBox's ItemsSource to your data and DisplayMemberPath to whatever property the text comes from (if it is only strings then just don't set DisplayMemberPath). You'd then set up an event for clicking on the ListBox and respond to that by checking what item is selected and running the code above to open Explorer.
If you want to get your UI working with minimal changes, try replacing the LinkLabels with Buttons (you can style them to look like links if you want, at least in WPF) since those work the same way.

how can I upload a file via file input box using webbrowser in C#

May be this question is asked in past but I have searched and not found its solution yet.
I have tried all the options that I have found till now but all in vain.
SendKeys doesn't work as it does not fill the file input box with file path, that is to be uploaded.
Cannot set file input box "SetAttribute" value as there is no value attribute available:
thats all.
If I use element.focus() it pops up "choose file to upload" dialog and now I don't know how to fill it programmatically and open it in file input box.
I want it to be automated completed so that user does not have to interact with the application.
Application shall pick the file from hard disk from given file path and fill other fields of form then start uploading, all using webbrowser control in windows form application.
No solutions found!
Can anyone help please? (This is my first ever question on stackoverflow, therefore if I am doing anything wrong then please guide, I mean if I am not allowed to post such question!)
Here is the code:
HtmlElementCollection heCollection = doc.GetElementsByTagName("input");
foreach (HtmlElement heSpan in heCollection)
{
string strType = heSpan.GetAttribute("type");
string strName = heSpan.GetAttribute("name");
if (strType.Equals("file") && strName.Equals("file"))
{
heSpan.Focus();
//heSpan.SetAttribute("value", "test.jpg");
SendKeys.Send("C:\\1.txt");
//heSpan.InnerText = "c:\\1.txt";
}
//Title for the attachment
if (strName.Equals("field_title"))
{
heSpan.InnerText = "1.txt";
}
}
When this code executes, cursor starts blinking in fine input box (as I have set heSpan.focus()) but the file path doesn't show in the file input box.
If I implement
heSpan.InvokeMember("click");
It opens the choose a file to upload dialoge/popup window and there I get stuck, because I don't know how to fill that popup dynamically and then insert the file path in file input box.
Try setting the focus to the WebBrowser control right before you set the focus to the input field.
That worked for me.

Visual C# 2008 Control to set a path

Greetings,
What control in Visual C# 2008 would allow me to set a path and get the value of that path.
For example:
I want the user to click a button then select a path where he/she would do the operation such as save a file in selected path.
You want to use a FolderBrowserDiaglog
var folderBrowserDiaglog = new FolderBrowserDiaglog();
if ( folderBrowserDiaglog.ShowDialog() == DialogResult.OK )
{
string path = folderBrowserDiaglog.SelectedPath;
}
I don't mean to repeat, but none of the other answers seemed to be exactly what you wanted.
There are 3 controls: FolderBrowserDialog, OpenFileDialog, and SaveFileDialog. There names are pretty intuitive. You use all 3 the same way. Tanascius has a good example in his post. The folder dialog you would use if you want the user to select a whole folder to look at. The open you would use if you want the user to select one or many files to open. Save is the same as open, but you would use it when saving a file out.
Hope this helps.
The FolderBrowse Dialog....
SaveFileDialog (for a file) or FolderBrowserDialog (for a folder). Located in the dialogs tab in the toolbox.
Assuming Windows Forms, try the OpenFileDialog. All common dialogs descend from CommonDialog, so looking at the descendents of that class may help, too.

How to print css applied background images with WebBrowser control

I am using the webbrowser control in winforms and discovered now that background images which I apply with css are not included in the printouts.
Is there a way to make the webbrowser print the background of the displayed document too?
Edit:
Since I wanted to do this programatically, I opted for this solution:
using Microsoft.Win32;
...
RegistryKey regKey = Registry.CurrentUser
.OpenSubKey("Software")
.OpenSubKey("Microsoft")
.OpenSubKey("Internet Explorer")
.OpenSubKey("Main");
//Get the current setting so that we can revert it after printjob
var defaultValue = regKey.GetValue("Print_Background");
regKey.SetValue("Print_Background", "yes");
//Do the printing
//Revert the registry key to the original value
regKey.SetValue("Print_Background", defaultValue);
Another way to handle this might be to just read the value, and notify the user to adjust this himself before printing. I have to agree that tweaking with the registry like this is not a good practice, so I am open for any suggestions.
Thanks for all your feedback
Another registry key would be :
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup\Print_Background
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\PageSetup\Print_Background
If you're going to go and change an important system setting, make sure to first read the current setting and restore it when you are done.
I consider this very bad practice in the first place, but if you must do it then be kind.
Registry.LocalMachine
Also, try changing LocalUser instead of LocalMachine - that way if your app crashes (and it will), then you'll only confounded the user, not everyone who uses the machine.
The corresponding HKCU key for this setting is:
HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Print_Background
By default, the browser does not print background images at all.
In Firefox
* File > Page Setup > Check Off "Print Background"
* File > Print Preview
In IE
* Tools > Internet Options > Advanced > Printing
* Check Off "Print Background Images and Colors"
In Opera
* File > Print Options > Check Off "Print Page Background"
* File > Print Preview (You may have to scroll down/up to see it refresh)
var sh = new ActiveXObject("WScript.Shell");
key = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main\\Print_Background";
var defaultValue = sh.RegRead(key);
sh.RegWrite(key,"yes","REG_SZ");
document.frames['detailFrame'].focus();
document.frames['detailFrame'].print();
sh.RegWrite(key,defaultValue,"REG_SZ");
return false;

Categories