Page 1 of 1

Need Some Help (again)

Posted: Thu Jan 31, 2008 7:38 pm
by Andrew_b
First off: I am using C#.

Im trying to get my open button to open but the browser never shows up. I just started today. Also if anyone could help me, i think it would be useful if i could get it to open on a certain directory all the time.

My Code:

Code: Select all

private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog;
        }
It currently does not compile my app cause of some errors. but when it does compile it wont show the browser.

Thanks
-Andrew_b

Posted: Thu Jan 31, 2008 8:02 pm
by Patrickh
if you want to open a file, why are you using a folder browser dialog?
i just started C# yesterday but it should be something like this

Code: Select all

private void openToolStripMenuItem_Click(object sender, EventArgs e) 
{
string path;
OpenFileDialog ofd = new OpenFileDialog;
ofd.InitialDirectory = <directory> //put you directory there
if (ofd.ShowDialog() != DialogResult.Cancel)
{
path = ofd.FileName;
}
}

Posted: Thu Jan 31, 2008 8:07 pm
by OwnZ joO
All that you did in your code was start to declare a variable.

Here's what you need to do

Code: Select all

FolderBrowserDialog fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() != DialogResult.Cancel)
{
 // do whatever you want on a successfull dialog(user didn't press X or cancel)
}
I'll split up something most programmers do in one step into two steps to hopefully give you an idea of what's going on.

When you say

Code: Select all

FolderBrowserDialog fbd;
it just creates a variable with nothing in it(null)
when you say

Code: Select all

fbd = new FolderBrowserDialog();
it creates a new object of FolderBrowserDialog and puts the pointer to where it is in memory into your fbd variable.

This was done in one step in the example I gave.

when you run the ShowDialog method, it returns a DialogResult
The way I gave is the way I usually do it, but you can store the result in a variable if you want

Code: Select all

DialogResult dr = fbd.ShowDialog();
Then you could perform more than one if statement on it, or use a switch.

Posted: Sat Feb 02, 2008 5:55 pm
by Andrew_b
Ok i have everything working great now. it opens txts and shows the text inside of them.

Now what do i use (in C#) to make my menu button 'About' Open a new window that shows what the program is about?

Posted: Sat Feb 02, 2008 6:08 pm
by Anthony
Create an about form

then do

Code: Select all

new About().ShowDialog();

Posted: Sun Feb 03, 2008 2:03 pm
by Andrew_b
Thanks everyone.

How would i close a text file? I tried TextBox1.Text(""); but that just erases the text.

How would i close a file?

Posted: Sun Feb 03, 2008 3:08 pm
by Prey
You can't close a file.. but you can close whatever you opened the file with. For example if you used a StreamReader, you would just call its Close() method.