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.

Please advise of basic problem in my code (using opendialog)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
garyusenet@myway.com - 23 Nov 2006 10:52 GMT
This is the first time i've worked with openfile dialog.
I'm getting a couple of errors with my very basic code.

Can someone point out the errors in what i've done please.
==========================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyNewProject
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void Form1_Load(object sender, EventArgs e)
       {
           string file;
           FileOpen();
       }

       private void FileOpen()
       {

           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               file = openfiledialog1.FileName;
           }

       }
   }
}

Problems : -
=============
Warning    1    The variable 'file' is declared but never used
Error    2    The name 'file' does not exist in the current context
Error    3    The name 'openfiledialog1' does not exist in the current
context

TIA

Gary
Olie - 23 Nov 2006 11:04 GMT
Oh dear... I think you would save allot of time by going back to basics
and reading a book on basic csharp programming. It looks like you do
not understand some of the basic concepts of classes which are
fundamental if you want to program in C#.

One of those principals is variable scope. Any variable you use has to
be declared to have a scope in a particular domain. Where and how you
declare that variable has a dramatic effect on its scope.

In your example you declare file in the function Form1_Load() but this
means it only has scope in that function and will be destroyed once the
function exits. In order to give it scope in the FileOpen() function
you need to declare it in the class. If you declare it in the class
then you have the option of three different scopes Private, Protected
and Public. Each of these will give the variable a different scope.

I can not tell you what the error is with openfiledialog1 as the code
which would tell me the answer is part of the partial class which you
have not posted here. My bet is though that it is not declared as
openfiledialog1. Did you rename it?

garyuse...@myway.com wrote:
> This is the first time i've worked with openfile dialog.
> I'm getting a couple of errors with my very basic code.
[quoted text clipped - 47 lines]
>
> Gary
garyusenet@myway.com - 23 Nov 2006 11:16 GMT
Thankyou for your time Olie, I do understand the basics of classes, but
obviously still have much to learn! I had thought that the load
function did not exit until after the closing brace, and because i
called the fileopen function before this i had beleived that the
variable would still be accesible. I have moved the string outside of
the load method and into the class and it is now working.

One thing I can't understand is why if i leave the string variable in
the load function and add 'public' in front of it, like so: public
string file  - why i can't then access it in the function openfile. I
had thought that public meant that anything in the code file can access
the variable?
-----------
also here is the rest of the class, in case it helps anyone looking at
my opendialog problem.

namespace MyNewProject{
   partial class Form1
   {
       /// <summary>
       /// Required designer variable.
       /// </summary>
       private System.ComponentModel.IContainer components = null;

       /// <summary>
       /// Clean up any resources being used.
       /// </summary>
       /// <param name="disposing">true if managed resources should be
disposed; otherwise, false.</param>
       protected override void Dispose(bool disposing)
       {
           if (disposing && (components != null))
           {
               components.Dispose();
           }
           base.Dispose(disposing);
       }

       #region Windows Form Designer generated code

       /// <summary>
       /// Required method for Designer support - do not modify
       /// the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
           this.openFileDialog1 = new
System.Windows.Forms.OpenFileDialog();
           this.SuspendLayout();
           //
           // openFileDialog1
           //
           this.openFileDialog1.FileName = "openFileDialog1";
           //
           // Form1
           //
           this.ClientSize = new System.Drawing.Size(292, 266);
           this.Name = "Form1";
           this.Load += new System.EventHandler(this.Form1_Load);
           this.ResumeLayout(false);

       }

       #endregion

       private System.Windows.Forms.OpenFileDialog openFileDialog1;
   }
}

> Oh dear... I think you would save allot of time by going back to basics
> and reading a book on basic csharp programming. It looks like you do
[quoted text clipped - 69 lines]
> >
> > Gary
Lenard Gunda - 23 Nov 2006 11:12 GMT
Hi!

You declare the "file" variable in one method (Form1_Load), and try to
use it from the other method (FileOpen). The scope of your variable
declaration is the Form1_Load() method. To use it in the other method,
you need to declare it there. Or you need to make it a field for your class.

Try something like this:

         private void Form1_Load(object sender, EventArgs e)
         {
             string file;
             file = FileOpen();
         }

         private string FileOpen()
         {
         string file = null;

             if (openFileDialog1.ShowDialog() == DialogResult.OK)
             {
                 file = openfiledialog1.FileName;
             }

             return file;
         }

There are of course a lot of ways to do this, depending on how/what you
would like to do.

Hope this helps

-Lenard

> This is the first time i've worked with openfile dialog.
> I'm getting a couple of errors with my very basic code.
[quoted text clipped - 47 lines]
>
> Gary
garyusenet@myway.com - 23 Nov 2006 11:21 GMT
Thankyou that seems to be working much better, i have the following: -

   public partial class Form1 : Form
   {
       private void Form1_Load(object sender, EventArgs e)
       {
           string file;
           file = FileOpen();
       }

       private string FileOpen()
       {
           string file = null;

           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
               file = openFileDialog1.FileName;
           }

           return file;
       }
   }
}

----
but the open file dialog isn't being displayed when my form is run.

I found out i had named it wrongly (wrong case which is why it wasn't
displaying).

can someone suggest why the open dialog isn't being displayed please?

thankyou
garyusenet@myway.com - 23 Nov 2006 11:23 GMT
Edit:
**i found out i hadn't named it properly wrong case, which is why IT
WASNT ACCESSIBLE in my original code. BUT i still have the problem that
it isnt displaying **
Olie - 23 Nov 2006 11:56 GMT
Any variable declared in a function will only have scope inside that
function. For your variable to have class scope you must declare it
inside the class but outside a function.

You said that public means that it can be accessed from anywhere in the
class. Well it does allot more than that it can be accessed from
everywhere. If you are declaring a class scope variable or member
variable then you should realy only declare it as either private or
protected and the latest documentation even says you should not declare
it as protected.

The reason you can not see your dialog is that you have to tell it to
display. Call the function ShowDialog() on the object and it will popup.
Olie - 23 Nov 2006 12:03 GMT
Sorry you are calling ShowDialog(). It should work, you might want to
check that all the properties are set correctly and try stepping
through the code to see what is happening.

Just a bit of advice on forum etiquette, you should not post the same
or similar question in multiple threads.

> Any variable declared in a function will only have scope inside that
> function. For your variable to have class scope you must declare it
[quoted text clipped - 9 lines]
> The reason you can not see your dialog is that you have to tell it to
> display. Call the function ShowDialog() on the object and it will popup.

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.