Question on C# [Solved]

Started by
6 comments, last by Savaroth 17 years, 4 months ago
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]
Advertisement
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
I'm not totally sure what you need, but I think what your looking for is...

label.Text = "Whatever";
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?
Quote:Original post by Savaroth
Well, 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


That worked, thanks you very much. :D
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

I will try to remember that, Thanks!

This topic is closed to new replies.

Advertisement