Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Can someone please check my understanding here and suggest fix

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
garyusenet@myway.com - 23 Nov 2006 11:55 GMT
The following code was supplied by a kind poster as a solution to a
problem i was having. But it's not quite working. I have commented the
code myself below. Can you please read my comments to make sure what
i've said is correct to check my understanding of what is happening.

Also the problem i have is that the openfiledialog isn't being
displayed when i run the code, does anyone know why?

namespace MyNamespace
{
   public partial class Form1 : Form

   {
       private void Form1_Load(object sender, EventArgs e)
       {
           string file;            // create a new string variable
called file.
           file = FileOpen();      // call the function FileOpen and
set file equal to the returned value.
       }

       private string FileOpen()               //new function, that
returns a string result.
       {
           string file = null;                 //assign local file
variable.

           //the following uses IF to execute the ShowDialog method of
openFileDialog1, and
           //if when the dialog exits the DialogResult is OK - the
Filename selected is stored
           //in the local file variable
           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               file = openFileDialog1.FileName;
           }

           return file;    //this passes the value of the local file
variable back to the calling Form1_Load
                          //method.
       }
   }
}
gary@garyshort.org - 23 Nov 2006 12:45 GMT
Hello,

It looks like what you are trying to do is to open a file dialog and
return the name of the file chosen by the user. If this is the case,
then the following code will suffice...

string fileName = "";
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.OK)
{
   fileName = ofd.FileName;
}

--
Cheers,
Gary
http://www.garyshort.org/

> The following code was supplied by a kind poster as a solution to a
> problem i was having. But it's not quite working. I have commented the
[quoted text clipped - 39 lines]
>     }
> }
garyusenet@myway.com - 23 Nov 2006 13:00 GMT
Thankyou. I tried your code and now have the following: -

namespace MyNamespace
{
   public partial class Form1 : Form

   {
       private void Form1_Load(object sender, EventArgs e)
       {
           string fileName = "";

           OpenFileDialog ofd = new OpenFileDialog();
           DialogResult dr = ofd.ShowDialog();
           if (dr == DialogResult.OK)
           {
               fileName = ofd.FileName;

           }
       }

   }
}

But i still have the same problem, all i have when i run the program is
a blank form, and the open file dialog box isn't displayed.

Please advise,

Gary.
garyusenet@myway.com - 23 Nov 2006 14:35 GMT
Bump! any advances?
gary@garyshort.org - 23 Nov 2006 16:03 GMT
Try cutting the code out of the form load handler and putting it in a
button click handler and see how you go. That code works for me in both
handlers so I'm not sure what your problem is.

--
Cheers,
Gary
http://www.garyshort.org/

> Bump! any advances?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.