|  | | 
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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
06-30-2010, 09:13 AM
|  | Registered User | | Join Date: Aug 2002 Location: Raleigh, NC | | | please tell me you have also posted this in coding forums as well. | 
06-30-2010, 09:13 AM
| | Registered User | | Join Date: Sep 2006 Location: Maywood, IL | | | what type of object is folderBrowserDialog1 ?
__________________
CallowHill + SWR = Tonal perfection!!!!!
| 
06-30-2010, 09:18 AM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by lamarjones 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 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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
06-30-2010, 09:31 AM
| | Registered User | | Join Date: Sep 2006 Location: Maywood, IL | | Quote:
Originally Posted by rarisgod 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!!!!!
| 
06-30-2010, 09:39 AM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by jeremyr 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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
06-30-2010, 03:44 PM
| | | | 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 | 
06-30-2010, 04:06 PM
|  | Supporting Member | | Join Date: Apr 2007 Location: Cincinnati | | Quote:
Originally Posted by rarisgod 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
| 
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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
06-30-2010, 07:42 PM
|  | 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 | 
06-30-2010, 11:26 PM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by john turner 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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
07-01-2010, 01:15 AM
| | | Quote:
Originally Posted by rarisgod 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
| 
07-02-2010, 05:28 AM
|  | Supporting Member | | Join Date: Apr 2007 Location: Cincinnati | | Quote:
Originally Posted by rarisgod 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
| 
07-02-2010, 08:26 AM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by RedsFan75 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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
07-02-2010, 09:14 AM
| | Registered User | | Join Date: Jun 2007 Location: Takoma Park, MD (DC) | | Quote:
Originally Posted by rarisgod Bump.
Nobody knows Visual C#? | No, but I know some visual Db. | 
07-02-2010, 09:29 AM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by Jim Nazium No, but I know some visual Db. | Zing!   
__________________ Quote:
Originally Posted by Thunderscreech Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself | | 
07-02-2010, 10:13 AM
| | | Quote:
Originally Posted by rarisgod 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 | 
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
Last edited by Kosko : 07-02-2010 at 10:21 AM.
| 
07-02-2010, 11:53 AM
| | Registered User | | Join Date: Jul 2008 Location: Somewhere in Canada | | Quote:
Originally Posted by jokn388 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 Social Networking is a plague upon the face of the Earth. | Quote:
Originally Posted by Milky I'd get an Itouch myself |
Last edited by rarisgod : 07-02-2010 at 12:17 PM.
| | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | |