Sign in to follow this  
Savaroth

Question on C# [Solved]

Recommended Posts

Savaroth    122
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 this post


Link to post
Share on other sites
TheTroll    883
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 this post


Link to post
Share on other sites
Savaroth    122
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 this post


Link to post
Share on other sites
TheTroll    883
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


Share this post


Link to post
Share on other sites
benryves    1999
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this