473,414 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 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 4694
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.