473,387 Members | 1,650 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,387 software developers and data experts.

Can someone please check my understanding here and suggest fix

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.
}
}
}

Nov 23 '06 #1
4 1288
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/
ga********@myway.com wrote:
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.
}
}
}
Nov 23 '06 #2
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.

Nov 23 '06 #3
Bump! any advances?

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

ga********@myway.com wrote:
Bump! any advances?
Nov 23 '06 #5

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

Similar topics

8
by: Qertoip | last post by:
Would you like to suggest me any improvements for the following code? I want to make my implementation as simple, as Python - native, as fine as possible. I've written simple code, which reads...
9
by: Daz | last post by:
Hello hello! I'm trying to finish off putting my design into HTML and I've come across a problem that I can't get my head around. I've got divs floating in two columns, but I'm having problems...
4
by: BinWang85 | last post by:
The following is the code: #include <iostream.h> #include <math.h> //needed for decimal calculations int main() { int employee_num; double FetchHours(void);
2
by: Sam Sungshik Kong | last post by:
Hello! I studied C# a little bit and am trying to compare it with VB.Net. There's 'Option Strict' in VB.Net. I thought that if I turn it on, it is as strict as C# when checking types. See...
2
by: SStory | last post by:
Here is the situation. I want to display Icons, Type of file etc from a file extension. Upon initial program load I may only need icons for certain files. But other operations will require...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
66
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it...
8
by: julia_beresford | last post by:
Hi I need to create the following class at runtime: public class MyCollection : CollectionBase { public void Add(MyItem item) { List.Add(item); }
12
by: MrTemplar | last post by:
Hello everyone, i am new to these forums and in fact new to C++. I am working on a project, and i would be most grateful if i could have my code checked by people who have a better understanding of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.