472,973 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 software developers and data experts.

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

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

Nov 23 '06 #1
7 4672
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.

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
Nov 23 '06 #2
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

ga********@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.

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
Nov 23 '06 #3
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;
}
}

Olie wrote:
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.

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
Nov 23 '06 #4
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

Nov 23 '06 #5
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 **

Nov 23 '06 #6
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.

Nov 23 '06 #7
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.
Olie wrote:
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.
Nov 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Question | last post by:
I am making a custom small menu which is most basic. Problem is that I can't make the first step work for some reason. I have an image to the left of where would be the layer positioned. This...
8
by: TAM | last post by:
Hi, I have two pages designed using CSS (without tables), http://www.ngrain.com/CSS/home1.htm designed for IE and http://www.ngrain.com/CSS/home2.htm designed for NN, Mozilla, FireFox...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
4
by: Geir Baardsen | last post by:
Hi! Is there an equivalent of "OpenDialog" in Delphi that can be found in MS ACCESS 2000? Me.Name
4
by: Will | last post by:
I'm used to writing my code in VBscript but I have to do this project in C#. I've written some functions on my ASP.Net page and I'm getting an error that a namespace I need access to has not been...
1
by: Paul D. Fox | last post by:
I'm trying to launch a Child Window from a hyperlink on a Datagrid and have it recieve multiple values from the Parent Window. Upon recieving the values in the Child Window, I need to access them...
2
by: news.microsoft.com | last post by:
I am starting to work with ASP.NET but am finding so many options that I am getting confused and would appreciate some advice. I have developed several ACCESS databases that we use internally and...
4
by: SUKRU | last post by:
Hello everybody. Unfortunately I am pretty new to sql-server 2000 I need some help with a Trigger I created. I created a trigger witch takes the id of the affected row and does a update on a...
5
by: Frank | last post by:
with OpenDialog suppose you set ..Filter = "Image (*.ico,*.jpg) |*.ico;*.jpg" ...AddExtension = True Which extension gets tacked on to the name?
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.