I have been searching for DAYS for material explaining how to do this...
I have a code in WPF (C#) that loops through a collection of FrameworkElements. The objective is to save each of the FrameworkElements as separate images (gif files). The code is working currently and does in fact save all of the images I need.
However, the user has to manually click the "Save" button in the SaveFileDialog box for EACH element. There are over 100 elements. I want the code to Open the SaveFileDialog box and automatically "click" the "Save" button for the user throughout the loop (ie - for each element).
Here is the code currently...
foreach(FrameworkElement x in y)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = " ...blah...";
dlg.DefaultExt = "...blah..";
Nullable<bool> result = dlg.ShowDialog(); // (A)
string path = dlg.FileName;
int selectedFilterIndex = dlg.FilterIndex;
if(result == true) // (B)
{ ... then do all the rendering, cropping, etc.. .. }
}
I know that I can write some line in between the line containing "(A)" and "(B)" that will activate the "Save" button. I cannot find any examples online that eliminate the "if" statement.
I don't want my code to "wonder if this result will be true". I want the code to actually set the result to true regardless of anything else.
Any help is greatly appreciated.
Related
I have some basic code for opening an OpenFileDialog.
OpenFileDialog dlg = new OpenFileDialog();
dlg.DefaultExt = ".csv";
dlg.Filter = "FileType|*.csv";
dlg.Multiselect = false;
bool? result = dlg.ShowDialog();
It works fine, but my Windows 7 users are complaining that the mouse scroll wheel doesn't scroll the list of files. I think the reason for this is that the initial focus is on the text field for the file name. If I click on the list of files, they become scrollable. How can I set the initial focus in the OpenFileDialog to be the list of files instead of the textbox?
I have a small application that needs to save to contents of a rich text box as a text file upon a button click. What I currently have is
//save button logic
private void saveBtn1_Click(object sender, EventArgs e)
{
//set up new SaveFileDialog
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text Files (*.txt)|*.txt";
saveFileDialog1.Title = "Save a text File";
//prompt the user
DialogResult result = saveFileDialog1.ShowDialog();
//if they select save write the contents of the RTB to a text file
if (result == DialogResult.OK)
{
System.IO.File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
}
}
It seems like a pretty straight forward piece of code, but I can't figure out why the dialog opens twice.
Just expanding Servy's comment into an answer because I suspect his diagnosis is correct. I'm going to give some simple suggestions for diagnosing/finding the problem. Firstly, you can put a break point in the event handler, if you're taken into that event handler twice then that's obviously the problem. The other solution (without even running) is simply to right click on the method name and select "find all references" this will give you list of all the references to the this method within your code. I'm guessing you will find 3; the method itself, the place where you expect to be registering it, and the place you were you don't. Remove the one and your problem will go away.
Shortest answer: you called the save dialog() twice in your code. Remove the second one and your issue will clear up.
Because you are used twice.
Thisa is simple excample for you
SaveFileDialog save = new SaveFileDialog();
save.DefaultExt = "zip";
save.Filter = "zip faylı (*.zip)|*.7z|Hamısı(*.*)|*.*";
save.ShowDialog(); // this is first using
if (save.ShowDialog() == DialogResult.OK) // and second using save.ShowDialog()
{
// yours code here
}
I am developing a windows forms application which uses OpenFileDialog to select files and drop it to a ListView.
The user should not be able to add same file to the listview twice. This should NOT happen:
How can this be done??
Have you tried checking if the listview contains the file before adding a new one?
The openfiledialog lets you filter by extension, but not by file name so you need to process the user selection after it closes. Perhaps showing a message dialog to tell the user that they selected a duplicate would be the best way to handle the invalid selection.
Try out this code.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var file = openFileDialog1.FileName;
if (listView1.FindItemWithText(file) == null)
listView1.Items.Add(file);
}
You can also add else with a message box informing user about choosing a duplicate file.
Check against existing entries.
var txt = comboBox1.Text;
if (!listView1.Items.ContainsKey(txt))
{
lvi.Text = txt;
// this is the key that ContainsKey uses. you might want to use the value
// of the ComboBox or something else, depending the combobox is freetext
// or regarding your scenario.
lvi.Name = txt;
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
lvi.SubItems.Add("");
listView1.Items.Add(lvi);
}
How prevent duplicate items listView C#
I'm trying to make this program in C# using WPF in Visual Studio. This is basically what it has to do.
When a button called "Browse" is clicked on the main form, it will open up a new form/window that let's the user browse to any directory that he chooses. After he selects the folder and clicks "Open" (or some other button on that form), the path of that directory, for example, "C:\temp" will be stored in a string variable so it can used later.
My first problem is, what do I write in the even handler of the "Browse" button that will open up a window that let's the user browse and select a folder? Is there a default window I can use or do I have to create a new form for it? Please note, the user has to select a folder, not a file like the default "Open" window.
Secondly, how do I reference a string variable so that it stores the path of the directory that the user selected?
The type you are looking for is the OpenFileDialog
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
The basic usage is the following
using (FileDialog fileDialog = new OpenFileDialog()) {
if (DialogResult.Ok == fileDialog.ShowDialog()) {
string fileName = fileDialog.FileName;
...
}
}
EDIT
Comments clarified OP is looking to open a directory vs. a file. For this you need the FolderBrowseDialog
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
For choosing directory you should use FolderBrowserDialog. It's a control from WinForms. WPF doesn't have it's own.
For example:
var dialog = new FolderBrowserDialog();
var result = dialog.ShowDialog();
if (result == DialogResult.OK)
// ...
Don't forget to add reference to the System.Windows.Forms.
Simply do this on Button Click
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
(folderpathTB is name of TextBox where I wana put the file path, OR u can assign it to a string variable too)
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.