Can't place a control on a form in Visual Studio - c#

I'm a newbie with Visual Studio Express 2013 and C#. I've borrowed a simple C# Windows application which builds and runs fine, and now I want to add some objects to the main form.
I select an object from the Toolbox, then click in the Designer on my main form where I want it to appear. This works fine for some simple objects like Label, but when I select an OpenFileDialog object nothing appears on my form. An OpenFileDialog "box" appears in a bar below my form instead, and I can't drag it to my form (I get a slashed circle).
I'm surely missing something simple. Thanks for any help.

You cannot drag an OpenFileDialog to the form because it is a non visual control.
To add an OpenFileDialog,just double click the control in the toolbox.That will add it to the form.Now to show the dialog you have to use OpenFileDialog.ShowDialog() in the code behind.Here is an example which shows the dialog on the click of a button.:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
DialogResult result= openFileDialog1.ShowDialog();
if(result==DialogResult.OK)
using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
{
string s = reader.ReadLine();
}
}

Related

C# - VisualStyles.VisualStyleState.NonClientAreaEnabled shows a different dialog with OpenFileDialog call

Setting VisualStyles.VisualStyleState.NonClientAreaEnabled in code shows a completely different dialog for OpenFileDialog call than when done without a VisualStyleState. The drop down for "View Menu" shows a vertical bar without text and the left browser pane is gone.
Image showing problem with Visual Style State set
In our application we need to set Styles as we have developed custom ones.
Issue reproducible on Windows 10 Build 1709, .Net 4.6.1 and default C# forms application. Also reproduced on Windows 10 build 1809. Works fine with all earlier versions of Windows.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Application.VisualStyleState = System.Windows.Forms.VisualStyles.VisualStyleState.NonClientAreaEnabled;
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.CheckFileExists = true;
fileDialog.Filter = " (*.sql)|*.sql";
fileDialog.ShowDialog();
}
}
Without VisualStyleState set, the OpenFileDialog shows a completely different UI, with browser pane on the left side and all drop downs work as expected.
Image showing default behaviour of OpenFileDialog
Any pointers to fix this issue would be helpful.
In one situation a dialog is rendered as an old style dialog and the other one as so-called “Vista-style” dialog:
old style
Vistay style
Here's the code the drives this decision:
https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/FileDialog.cs,986
A type of a dialog depends on whether VisualStyleState.ClientAreaEnabled is set or not. Because you set the style to VisualStyleState.NonClientAreaEnabled the app falls back to the old-style.
There is AutoUpgradeEnabled property that allow rendering the dialog as Vista-style, you could try setting it to true and see if it helps.

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.

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.

Problem with FolderBrowserDialog

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.

Categories

Resources