i'm writing some values which are global to the whole app. these values can change depending on the user.

// create GLOBAL SETTINGS
        public static class MyGlobals
        {
            public static string userName = "userName";
            public static string pdfPath = @"C:\mypdfs\"; // deafult value, changes
            public static string FontName = "Gotham Book";

        }

now in another part of the app, the user can update the pdfPath.

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                MyGlobals.pdfPath = folderBrowserDialog1.SelectedPath;
            }

this all works, but if the user double clicks the file, it should open, but i get a win32 exception.

// execute selected file
            
            int index = this.listBox1.IndexFromPoint(e.Location);
            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                var file = listBox1.SelectedItem.ToString();
                Process.Start(MyGlobals.pdfPath + file);
                listBox1.Visible = false;
                filesPnl.Visible = false;
                listBox1.Items.Clear();
            }

in addition, if i select user/My Documents/ it populates the list correctly, but it removes "My ", so MyGlobals.pdfPath = user/documents + fileName.pdf

Which i assume is my win32 exception.. Have i missed something, or is there a better way to achieve a global variable which can update and always loads with the app..

Thanks..

Recommended Answers

All 8 Replies

Hello, walid86.
Firstly, try it out by yourself:
1. Find your local folder called "My Documents".
2. Right-click on it and go to tab "Location".
3. See what's in there ;)

Next, it would be great if you posted exception message (so I don't need to recreate all the behavior to reproduce your exception), cause Win32 Exception is too generalized term.

Ok, closer to your exception. I have a few questions:
1. Where do you get it? (exact line of code)
2. What data is in variables, on that line?
3. What's this for:

int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)

it's look like it should be

if (listBox1.SelectedItem != null) {
}

or

if (listBox1.SelectedIndex >= 0) {
}

.. but that's just a thoughts in a loud .. maybe there's something that making you use IndexFromPoint .

hi Antenka, thanks for the reply..

the error is exactly here...

// execute selected file
            
            int index = this.listBox1.IndexFromPoint(e.Location);
            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                var file = listBox1.SelectedItem.ToString();
                Process.Start(MyGlobals.pdfPath + file);  <----- win32exception was unhandles
                listBox1.Visible = false;
                filesPnl.Visible = false;
                listBox1.Items.Clear();
            }

myGlobals.pdfPath = C:\\users\\walid\\documents
file = infopackpdf.pdf

the system cannot find the file specified..

is it ment to be myglobals.pdfpath + "\" + file then?..


i'm using

int index = this.listBox1.IndexFromPoint(e.Location);
if (index != System.Windows.Forms.ListBox.NoMatches)

so i know which index was selected, so the app knows which file to open..

file at location "index"..

go it...

Process.Start(MyGlobals.pdfPath [B]+ "\\" + [/B] file);

random question:

if this application is deployed on a computer, will the path set by the user always reset to the original path, or will it be the path the user updates?

if its always the original, is there a way of putting this setting in a file or somethin so that if the user updates this folder location to their choosing, it will show this location?

thanks..


Walid.

> i'm using so i know which index was selected, so the app knows which file to open..
ok, how about:

int index = listBox1.SelectedIndex;
if (index >= 0) {
}

SelectedIndex property is 0-based. So if user selects any item in list box, it would return the selected index. If nothing selected - it will return -1. IMO, this approach:
1. More readable.
2. Less complex.
3. More appropriate for this situation.
4. And, I suppose, has performance advantages (if not dig too deep: SelectedIndex - is a property, than returns a single value, stored in a listBox; IndexFromPoint - is a method that calculates a selected item index based on where user clicked. Sounds like it has more work to do ;)).

> will the path set by the user always reset to the original path, or will it be the path the user updates

Not sure, that I understand you correctly. But I'll take a shot :) You mean, if the Location of Documents would be changed?
If so - I suppose, you should treat your "My Documents" (in a folder browse dialog) as a shortcut. If user would change the folder, assigned to this shortcut - you'll see the contents of a newly-assigned folder.

If you need to remember the previous-opened location - sure, you can write it in a property-file. And then, for example, make a button "Recent Documents" to view it's content.

// execute selected file
            int index = listBox1.SelectedIndex;
            if (index >= 0)
            {
                var file = listBox1.SelectedItem.ToString();
                Process.Start(MyGlobals.pdfPath + "\\" + file);
                listBox1.Visible = false;
                viewPanel.Visible = false;
                listBox1.Items.Clear();
            }

fixed :D

i'll try and clarify..

the deafult location of saved content is C:\pdfs\...

so when the user saves, it saves the pdf here..

the user has the ability to change this folder to what they choose..

if the user does change this to C:\newpdflocation\

the next time the user opens the app, the location will be C:\pdfs\ right?

how can i change it to remember the location set by the user...

thank :D

> if the user does change this to C:\newpdflocation\
> the next time the user opens the app, the location will be C:\pdfs\ right?
Depends on how app determines the default path to save your pdf's.
If, you write this setting in a config-file, or a property-file when user changes the default path. And then you restore this setting on application start up. Then the new path would be loaded

i knew the answer but still asked the question..lol

since i hard code the path when the form loads, it will not read my mind and look up the users folder location..

time to research config-file's/property-file..

thanks alot for your help...

its nice to get a response.. :)

commented: You're welcome and good luck in learning :) +7

Hate to revive an old thread, but this might be of value to the OP:

string myPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

If you use this instead of a hardcoded path to your files it won't matter if the actual path changes between computers. It's worthwile to make yourself familiar with the folders enumerated by Environment.SpecialFolder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.