Go Back   TalkBass Forums > Bass Guitar Forums > Bass Guitar Forums > Off Topic [BG]
Register Rules/FAQ/CUP Members List Search Today's Posts Mark Forums Read

Off Topic [BG] Non-music-related discussion and chat


Supporting Membership
Thank You

Latest Supporting Member
Donate to Upgrade Today

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-30-2010, 09:08 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Visual C# Thread

Sign in to disble this ad
Hello, I'm doing a program in Visual C# for a summer job I have, and I was wondering if anyone around here would be able to help me out with some C# questions I have.

First, I need to display the contents of a folder in some sort of box on the screen. I say some sort of box because I don't know what tool I should use. Those familiar with Visual Studio/Visual C# might be able to help me out with this. So what tool could I use, and what code would properly display it?

I have this code so far to obtain a selected folder path name:

Code:
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) //if user pressed "OK"
{
      label1.Text = folderBrowserDialog1.SelectedPath; //change label to path name
}
That simply changes an on-screen label tool to display the directory name that has been selected. I need to somehow display the contents of this directory on-screen.

__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #2  
Old 06-30-2010, 09:13 AM
lamarjones's Avatar
Registered User
 
Join Date: Aug 2002
Location: Raleigh, NC
Supporting Member
please tell me you have also posted this in coding forums as well.
__________________
What I just said is probably not right.

Funk or not Funk?
http://www.funkuponya.com

http://www.myspace.com/leolikesbass
  #3  
Old 06-30-2010, 09:13 AM
Registered User
 
Join Date: Sep 2006
Location: Maywood, IL
Send a message via AIM to jeremyr Send a message via MSN to jeremyr Send a message via Yahoo to jeremyr Send a message via Skype™ to jeremyr
what type of object is folderBrowserDialog1 ?
__________________
CallowHill + SWR = Tonal perfection!!!!!
  #4  
Old 06-30-2010, 09:18 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by lamarjones View Post
please tell me you have also posted this in coding forums as well.
Yes, but the forum I registered on isn't as popular as TB (nor is any other forum I've registered on) and I know that some CS people are on here as well. I was somewhat hoping for a response asap, hence my posting on two different forums.

See already, I have 2 replies to their 0, and I posted on that forum first.

Quote:
Originally Posted by jeremyr View Post
what type of object is folderBrowserDialog1 ?
I'm not quite sure what you mean, I've only just started using Visual C# and while I've been teaching myself, I only just started using folder dialog boxes. It doesn't show up on the screen, but when I use it, it pops up a folder list and you select a folder from it.
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #5  
Old 06-30-2010, 09:31 AM
Registered User
 
Join Date: Sep 2006
Location: Maywood, IL
Send a message via AIM to jeremyr Send a message via MSN to jeremyr Send a message via Yahoo to jeremyr Send a message via Skype™ to jeremyr
Quote:
Originally Posted by rarisgod View Post
Yes, but the forum I registered on isn't as popular as TB (nor is any other forum I've registered on) and I know that some CS people are on here as well. I was somewhat hoping for a response asap, hence my posting on two different forums.

See already, I have 2 replies to their 0, and I posted on that forum first.



I'm not quite sure what you mean, I've only just started using Visual C# and while I've been teaching myself, I only just started using folder dialog boxes. It doesn't show up on the screen, but when I use it, it pops up a folder list and you select a folder from it.
all objects have a type. whether it's a Custom obj that you've built or a built in object that's predefined.

In all cases if the object isn't static (used typically as a util methods) then it will need to be instantiated. like so

var objFolderItem = new Object;


the type of the variable objFolderItem in this case is a plain Object.

i was simply asking what kind of object it is that you are using already.

Once I know that I can help you find out how to get what the selected items were that the user selected.
__________________
CallowHill + SWR = Tonal perfection!!!!!
  #6  
Old 06-30-2010, 09:39 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by jeremyr View Post
all objects have a type. whether it's a Custom obj that you've built or a built in object that's predefined.

In all cases if the object isn't static (used typically as a util methods) then it will need to be instantiated. like so

var objFolderItem = new Object;


the type of the variable objFolderItem in this case is a plain Object.

i was simply asking what kind of object it is that you are using already.

Once I know that I can help you find out how to get what the selected items were that the user selected.
Oh. The object is built into Visual C#, and I basically just dragged it onto the form. So if that's what you mean by predefined, then yes. I haven't built any objects as I haven't gone that far into object oriented programming yet.
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #7  
Old 06-30-2010, 03:15 PM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Bump.

Nobody knows Visual C#?
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #8  
Old 06-30-2010, 03:44 PM
Registered User
 
Join Date: Apr 2007
Take this with a grain of salt since I do not know C#. But looking at the api and at some quick examples this may do the trick

Note you need to know the path, which should should be a string

DirectoryInfo di = new DirectoryInfo(path);

FileInfo[] files = di.GetFiles("*.");
foreach(FileInfo fi in files)
{
fi.Name;
}

Hope this helps, or at least puts you on the right path until someone with real c# knowledge can help you out

Edit: fi.Name will give you the name of the file. You will have to handle it however you would like, whether it is printing it right to the screen or adding it to another label
  #9  
Old 06-30-2010, 04:06 PM
RedsFan75's Avatar
Supporting Member
 
Join Date: Apr 2007
Location: Cincinnati
Supporting Member
Quote:
Originally Posted by rarisgod View Post
Bump.

Nobody knows Visual C#?
It's what I do for a living.

jokn388 should get what you're looking for.
__________________
P&W #248, Ohio Bassist #168, GK #625, LOG #390, 5 String #108, Ibanez #519 P-Bass #424, Medium Scale #29, Acoustic Bass #63
  #10  
Old 06-30-2010, 06:53 PM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Code:
string[] dirEntries = Directory.GetDirectories(source);
            foreach (string dirName in dirEntries)
            {
                textBox1.Lines[count] = Path.GetFileName(dirName);
                if (count != dirEntries.Length - 1)
                {
                    count++;
                }
            }
Why am I running out of the array??? I don't know of any other way to print on different Lines in the textbox...
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #11  
Old 06-30-2010, 07:42 PM
john turner's Avatar
You don't want to do that. Trust me.
Forum Administrator
 
Join Date: Mar 2000
Location: atlanta ga
might want to initialize count.
__________________
Talkbass Forum Administrator Ask me, I'm here to help.

Lord Only on Myspace - 4 New Lord Only Tracks from our 2nd CD
Lord Only - yes. we're back. sorta
versatile residue -12 minute instrumental

I find it elevating and exhilarating to discover that we live in a universe which permits the evolution of molecular machines as intricate and subtle as we. - Carl Sagan
Rock 'n' Roll... It's got nothing to do with journalists, and it hasn't really even got anything to do with musicians, either. - Pete Townsend
  #12  
Old 06-30-2010, 11:26 PM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by john turner View Post
might want to initialize count.
I should have included that part, but I did earlier in the code. I thought I included all relevant info but yeah that should have been in there also.

Nonetheless, I found out my error was not initializing textbox1.Lines. But also that I didn't even need to do it with a foreach loop. I got it working successfully:

Code:
            folderBrowserDialog1.ShowDialog();
            label1.Text = folderBrowserDialog1.SelectedPath;
            string[] dirEntries = Directory.GetDirectories(folderBrowserDialog1.SelectedPath);
            textBox1.Lines = dirEntries;
            string[] fileEntries = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
            textBox1.Lines = fileEntries;
But I don't really want it in a textbox lol I'd rather have it in a listbox, but there is no "Lines" property for listbox, so it looks as though it won't work the same way with other objects..oh how I've confused myself.
Thanks for now, more to come no doubt!
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #13  
Old 07-01-2010, 01:15 AM
Registered User
 
Join Date: Sep 2006
Quote:
Originally Posted by rarisgod View Post
Code:
string[] dirEntries = Directory.GetDirectories(source);
            foreach (string dirName in dirEntries)
            {
                textBox1.Lines[count] = Path.GetFileName(dirName);
                if (count != dirEntries.Length - 1)
                {
                    count++;
                }
            }
Why am I running out of the array??? I don't know of any other way to print on different Lines in the textbox...
I don't know C#. Java ftw.

Don't forget to make the textbox multiline. I'm not sure how the Lines member works if otherwise, the API isn't clear.

The Path.getFileName method returns an empty string if the argument denotes a directory, if I read right. I'm not sure how does the TextBox handle appending empty strings: it could possibly be that the Lines property does not have its size increased or something. Maybe you should check if the string is empty or null, and only append otherwise. Or maybe you should add a newline character at the end?

Also, there's no need to check the count variable inside. It's an index to where you want new text to be appended, right? If so, it should be incremented only when and every time you add text to the textbox. Makes sense. Inside the foreach block you might want to see only
Code:
textBox1.Lines[count++] = Path.GetFileName(dirName);
__________________
Near the tree, by the river, there's a hole in the ground...
Dingwall Club Member #58
  #14  
Old 07-02-2010, 05:28 AM
RedsFan75's Avatar
Supporting Member
 
Join Date: Apr 2007
Location: Cincinnati
Supporting Member
Quote:
Originally Posted by rarisgod View Post
But I don't really want it in a textbox lol I'd rather have it in a listbox, but there is no "Lines" property for listbox, so it looks as though it won't work the same way with other objects..oh how I've confused myself.
Thanks for now, more to come no doubt!
listbox.items.add(Path.GetFileName(dirName))
__________________
P&W #248, Ohio Bassist #168, GK #625, LOG #390, 5 String #108, Ibanez #519 P-Bass #424, Medium Scale #29, Acoustic Bass #63
  #15  
Old 07-02-2010, 08:26 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by RedsFan75 View Post
listbox.items.add(Path.GetFileName(dirName))
Great! This is just what I needed!
Now...is there any way to filter certain file types so only those file types are displayed?
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #16  
Old 07-02-2010, 09:14 AM
Registered User
 
Join Date: Jun 2007
Location: Takoma Park, MD (DC)
Quote:
Originally Posted by rarisgod View Post
Bump.

Nobody knows Visual C#?
No, but I know some visual Db.
  #17  
Old 07-02-2010, 09:29 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by Jim Nazium View Post
No, but I know some visual Db.
Zing!
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself
  #18  
Old 07-02-2010, 10:13 AM
Registered User
 
Join Date: Apr 2007
Quote:
Originally Posted by rarisgod View Post
Great! This is just what I needed!
Now...is there any way to filter certain file types so only those file types are displayed?
Going back to my original post I have a line:
FileInfo[] files = di.GetFiles("*.");

You can alter this line to get a particular filetype. For example to get a list of pdfs:
FileInfo[] pdfs = di.GetFiles("*.pdf");
list of word documents:
FileInfo[] docs = di.GetFiles("*.doc");

etc. There should be a million different ways to do this.

Do you have a c# book/reference? If not, I highly recommend getting one and immersing yourself in the language
  #19  
Old 07-02-2010, 10:19 AM
Registered User
 
Join Date: Dec 2005
Location: Buffalo
Edit: Oh you're reading a folder and not a file... sorry
__________________
Photos of Wagons

Last edited by Kosko : 07-02-2010 at 10:21 AM.
  #20  
Old 07-02-2010, 11:53 AM
Registered User
 
Join Date: Jul 2008
Location: Somewhere in Canada
Quote:
Originally Posted by jokn388 View Post
Going back to my original post I have a line:
FileInfo[] files = di.GetFiles("*.");

You can alter this line to get a particular filetype. For example to get a list of pdfs:
FileInfo[] pdfs = di.GetFiles("*.pdf");
list of word documents:
FileInfo[] docs = di.GetFiles("*.doc");

etc. There should be a million different ways to do this.

Do you have a c# book/reference? If not, I highly recommend getting one and immersing yourself in the language
I ended up finding this on another site. I did have to limit to 2 specific file types, but I sound a solution in just running the same filter method twice with 2 different file types.

Thanks everyone, no doubt there will be more questions to come!
__________________
Quote:
Originally Posted by Thunderscreech View Post
Social Networking is a plague upon the face of the Earth.
Quote:
Originally Posted by Milky View Post
I'd get an Itouch myself

Last edited by rarisgod : 07-02-2010 at 12:17 PM.
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Follow TalkBass on Twitter   Visit TalkBass on Facebook  

All times are GMT -6. The time now is 01:13 PM.




Copyright 2011 Talk Music Group Inc. All rights reserved.
Play guitar? Visit our new sister site TalkGuitar.com [beta]
Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.