# Question on C# [Solved]

## Recommended Posts

Heya, I am using a book called Microsoft visual C# express edition, and I am almost done with chapter one, But at the end of the chapter there is an excercise which wants me to display the name of a file I have opened. Now I made a label on a form, and I want to display the name on an image on this label, I know that the FileName contains the info I need, but how can I assign this to the label? Hope I was clear enough, Thanks :) [Edited by - Savaroth on December 12, 2006 2:58:45 PM]

##### Share on other sites
Try this...

OpenFileDialog filedialog = new OpenFileDialog();filediaog.ShowDialog();  //This will create the dialog as a modal dialog.yourlabel.Text = filedialog.Filename;

Hope that helps.
theTroll

##### Share on other sites
I'm not totally sure what you need, but I think what your looking for is...

label.Text = "Whatever";

##### Share on other sites
Well, let me try to explain with some code:

if (openFileDialog1.ShowDialog() == DialogResult.OK)            {                pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);                    }

Now what I want to do is, I want to display the name of the image I opened, onto the label.

Edit: @ TheTroll, That does not work, or I do not completely understand, could you elaborate please?

Edit: @smitty1276 That is what I want, but it does not work if I do it like that, it gives me an error, or I am just doing it wrong, could care to explain a bit more?

##### Share on other sites
Quote:
 Original post by SavarothWell, let me try to explain with some code:*** Source Snippet Removed ***Now what I want to do is, I want to display the name of the image I opened, onto the label.

Ok.. First you need to save the filename and then use it for both.

if (openFileDialog1.ShowDialog() == DialogResult.OK){    yourlabel.Text = openFileDialog1.Filename;    pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); }

Hope that helps.
theTroll

##### Share on other sites
That worked, thanks you very much. :D

##### Share on other sites
When showing dialogs, pass this in as a parameter. So:

if (openFileDialog1.ShowDialog(this) == DialogResult.OK) {    yourlabel.Text = openFileDialog1.Filename;    pictureBox1.Image = Image.FromFile(openFileDialog1.FileName); }

This opens the dialog up as a child of the form that opened it, meaning that you can't do anything on that form until the dialog has been closed. This works for your own forms too.

The Path class has a number of useful methods for manipulating paths. In your case, it might look tidier to say:

yourlabel.Text = Path.GetFileName(openFileDialog1.Filename);

This way, you only see the filename and extension, not the full path.

##### Share on other sites
I will try to remember that, Thanks!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627636
• Total Posts
2978331

• 10
• 12
• 22
• 13
• 33