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

How to highlight those huge files display in listbox

Hi,
I am using c# to create a window application which display all the file name from the server into a listbox. The file sizes of the files in the listbox some are in Bytes, KB, and MB.
For now, my intention is to identify those file sizes (file sizes which exceed 50MB) by highlighting the file names which display in the listbox.
I have no idea how to write the coding. Anyone can help me, please......?

Thanks.
Apr 17 '07 #1
16 1714
SammyB
807 Expert 512MB
Hi,
I am using c# to create a window application which display all the file name from the server into a listbox. The file sizes of the files in the listbox some are in Bytes, KB, and MB.
For now, my intention is to identify those file sizes (file sizes which exceed 50MB) by highlighting the file names which display in the listbox.
I have no idea how to write the coding. Anyone can help me, please......?

Thanks.
I would make the DrawMode of the ListBox owner draw and add a DrawItem event for the ListBox. There is an example at http://msdn2.microsoft.com/en-us/lib....drawmode.aspx. You would just replace the switch statement with logic that says if big then use the red brush. HTH --Sam
Apr 17 '07 #2
Hi Sam,
For the dumb question I have now. How to add the DrawItem event to the listbox? :(
I only managed to change the DrawMode to OwnerDraw.

And one more thing that i don't understand is how to get this:
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

When I double-click on my UI (listbox). it returns this:
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)

I am very new in this area... Please be patient to me and thanks a lot for your guidance.
Apr 17 '07 #3
SammyB
807 Expert 512MB
Hi Sam,
For the dumb question I have now. How to add the DrawItem event to the listbox? :(
I only managed to change the DrawMode to OwnerDraw.

And one more thing that i don't understand is how to get this:
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

When I double-click on my UI (listbox). it returns this:
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)

I am very new in this area... Please be patient to me and thanks a lot for your guidance.
No problem, we'll go very slow.
  1. Open a new C# Windows Application and add a ListBox to the form
  2. Double-click the form to generate the load event. Add some code to put stuff in the ListBox:
Expand|Select|Wrap|Line Numbers
  1. listBox1.Items.AddRange(new string[] { "A", "B", "C", "D" });
  1. Click on the Form1 design tab and select the ListBox.
  2. In the Properties Window, change the DrawMode to OwnerDrawFixed.
  3. In the Properties Window, click on the lightning-bolt at the top, so that instead of properties, you see events.
  4. Double-click on DrawItem to add the event and a code outline
  5. Switch to the MS Web Page with the sample DrawItem code and copy their C# code for the DrawItem event. Paste this code into your project, replacing the outline.
  6. Press F5 to run your project
You should see the first three items in different colors. HTH --Sam
Apr 17 '07 #4
SammyB
807 Expert 512MB
Hi Sam,
And one more thing that i don't understand is how to get this:
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)

When I double-click on my UI (listbox). it returns this:
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e).
As for your other question, because of the "using System.Windows.Forms;" at the top of your module, "System.Windows.Forms.DrawItemEventArgs e" is the same as "DrawItemEventArgs e". --Sam
Apr 17 '07 #5
SammyB
807 Expert 512MB
Chin did all that I mentioned above, but now is having trouble knowing which are the large files in the paint event. He PM'ed me (which is a no-no: post questions in the forum), so I am just restating the problem:

...The problem that I facing is I can't call the fi.Length (which is refer to file size) function ... the DrawItem event....
Apr 18 '07 #6
SammyB
807 Expert 512MB
The problem that I facing is I can't call the fi.Length from the DrawItem event
So, we need to keep the FileInformation objects around so that we can use them. We will keep the current set in a global variable, mFiles. It will be a generic List of type FileInformation.

At the top of your code just after the class statement, define mFiles:
Expand|Select|Wrap|Line Numbers
  1. List<FileInfo> mFiles = new List<FileInfo>();
Now, wherever you load the ListBox, load mFiles instead:
Expand|Select|Wrap|Line Numbers
  1. foreach (string s in System.IO.Directory.GetFiles("Your Path")))
  2.     mFiles.Add(new FileInfo(s));
Next, in the Form Load Event, setup the ListBox's DataSource to be mFiles:
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.     lstFiles.DataSource = mFiles;
  4.     lstFiles.DisplayMember = "Name";
  5.     lstFiles.ValueMember = "Length";
  6. }
This will cause the ListBox to get it's displayed items from mFiles using the Name property of the FileInformation objects. Make sure that you have removed all of the lstFiles.Items.Add statements from your code: you have to do it one way or the other.

Now, finally, you can add an if statement to the DrawItem event:
Expand|Select|Wrap|Line Numbers
  1. Brush myBrush = Brushes.Black;
  2. if (mFiles[e.Index].Length > 4000)
  3.      myBrush = Brushes.Red;
Now, it looks very nice! :rolleyes:
Apr 18 '07 #7
Hi Sam,
Firstly, very very thanks for your great support and help!!! I really appreciate it!
Right now, I am still facing some problem with my coding...

Here is part of the code that I have. The problem that I facing is I can't call the fi.Length (which is refer to file size) function from private void lstFiles_SelectedIndexChanged(object sender, EventArgs e) into the DrawItem event ( private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)) that I manage added in with the guidance you provided to me.

If you refer to code as below which is in Bold, I managed to highlight the file name. But, this is not I want. What I want is highlight those file name which exceed a limit filesize. I am trying to write in if(fi.Length>=9000000) but it gives me error because I cannot get the fi.Length from private void lstFiles_SelectedIndexChanged(object sender, EventArgs e).

I hope you understand my explanation of the problem that I face...sorry for distubring you again. Thanks a lot, Sam.

Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. lblDir.Text = treeView1.SelectedNode.FullPath;
  6. string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
  7. FileInfo fi = new FileInfo(path);
  8.  
  9. lblFile.Text = lstFiles.SelectedItem.ToString();
  10. lblSize.Text= FormatFileSize(fi.Length);
  11. }
  12. catch { }
  13. }
  14.  
  15. private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
  16. {
  17. lstFiles.DrawMode = DrawMode.OwnerDrawFixed;
  18. e.DrawBackground();
  19. Brush textBrush = SystemBrushes.ControlText;
  20. Font drawFont = e.Font;
  21.  
  22. if (lstFiles.Items[e.Index].ToString() == "Filename") {
  23. textBrush = Brushes.Red;
  24. if ((e.State & DrawItemState.Selected) > 0)
  25. drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);
  26. }
  27. else if ((e.State & DrawItemState.Selected) > 0)
  28. {
  29. textBrush = SystemBrushes.HighlightText;
  30. }
  31.  
  32.  
  33. e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, textBrush, e.Bounds, StringFormat.GenericDefault);
  34.  
  35. e.DrawFocusRectangle();
  36. }
  37.  
Apr 19 '07 #8
SammyB
807 Expert 512MB
See my reply in post #7, just above your post. --Sam
Apr 19 '07 #9
Sam....I am gonna crazy of the coding....I was trying to do as your guidance. However, it didn't work. Although the script can be run without any error, but the outcome was not follow the filesize specification. It simply highlight those files in the listbox. It didnt not follow the if...else loop that i specified the file size in the script.If I remove all of the lstFiles.Items.Add statements from the code, the coding works in the way that not I want.
Let me explain the work that I am doing. My coding work like this...It get the files from the directory which display all the folder in TreeView. Then, from the selected node of treeview, you will get the files listed from the selected folder in treeview display in the listbox. From the listbox which listed out all the files, i want to highlight those file that meet my file size specification.

Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. lblDir.Text = treeView1.SelectedNode.FullPath;
  6. string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
  7. FileInfo fi= new FileInfo(path);
  8. lblFile.Text = lstFiles.SelectedItem.ToString();
  9. lblSize.Text = FormatFileSize(fi.Length);
  10.  
I was thinking that propably the path reference is incorrect and that's why it didn't read the file size specification. If you refer to the code above, my path is
string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
FileInfo fi= new FileInfo(path);

But, under the DrawItem event, i don't know how to make the similar path as mentioned above into DrawItem event.


Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
  2. {
  3.  
  4. foreach (string s in System.IO.Directory.GetFiles("c:\\"))
  5. {
  6. mFiles.Add(new FileInfo(s));
  7.  
  8. Brush textBrush = Brushes.Black;
  9.  
  10. if (mFiles[e.Index].Length>1000)
  11. }
  12.  
Apr 19 '07 #10
SammyB
807 Expert 512MB
Sam....I am gonna crazy of the coding....I was trying to do as your guidance. However, it didn't work. Although the script can be run without any error, but the outcome was not follow the filesize specification. It simply highlight those files in the listbox. It didnt not follow the if...else loop that i specified the file size in the script.If I remove all of the lstFiles.Items.Add statements from the code, the coding works in the way that not I want.
Let me explain the work that I am doing. My coding work like this...It get the files from the directory which display all the folder in TreeView. Then, from the selected node of treeview, you will get the files listed from the selected folder in treeview display in the listbox. From the listbox which listed out all the files, i want to highlight those file that meet my file size specification.

Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
  2. {
  3. try
  4. {
  5. lblDir.Text = treeView1.SelectedNode.FullPath;
  6. string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
  7. FileInfo fi= new FileInfo(path);
  8. lblFile.Text = lstFiles.SelectedItem.ToString();
  9. lblSize.Text = FormatFileSize(fi.Length);
  10.  
I was thinking that propably the path reference is incorrect and that's why it didn't read the file size specification. If you refer to the code above, my path is
string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
FileInfo fi= new FileInfo(path);

But, under the DrawItem event, i don't know how to make the similar path as mentioned above into DrawItem event.


Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
  2. {
  3.  
  4. foreach (string s in System.IO.Directory.GetFiles("c:\\"))
  5. {
  6. mFiles.Add(new FileInfo(s));
  7.  
  8. Brush textBrush = Brushes.Black;
  9.  
  10. if (mFiles[e.Index].Length>1000)
  11. }
  12.  
My sample code is a work. If you can stay awake for a half-hour, I'll post it. --Sam
Apr 19 '07 #11
sure.......please take your time. I am also still working on it...
Apr 19 '07 #12
SammyB
807 Expert 512MB
Here is my sample:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. namespace ListBoxPaint
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void Form1_Load(object sender, EventArgs e)
  18.         {
  19.             List<FileInfo> files = new List<FileInfo>();
  20.             foreach (string s in System.IO.Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)))
  21.                 files.Add(new FileInfo(s));
  22.             lstFiles.DataSource = files;
  23.             lstFiles.DisplayMember = "Name";
  24.             lstFiles.ValueMember = "Length";
  25.         }
  26.         private void lstFiles_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
  27.         {
  28.             // Set the DrawMode property to draw fixed sized items.
  29.             lstFiles.DrawMode = DrawMode.OwnerDrawFixed;
  30.             // Draw the background of the ListBox control for each item.
  31.             e.DrawBackground();
  32.             ListBox lb = sender as ListBox;
  33.             FileInfo fi = lb.Items[e.Index] as FileInfo;
  34.             // Define the default color of the brush as black.
  35.             Brush myBrush = Brushes.Black;
  36.             if (fi.Length > 4000)
  37.                     myBrush = Brushes.Red;
  38.             // Draw the current item text based on the current Font and the custom brush settings.
  39.             e.Graphics.DrawString(lstFiles.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
  40.             // If the ListBox has focus, draw a focus rectangle around the selected item.
  41.             e.DrawFocusRectangle();
  42.         }
  43.     }
  44. }
This is a little different from my earlier post. I have removed the global mFiles & replaced it with a local files. Each element of files is a FileInfo and these objects are used by the ListBox in its Items collection, so I can cast ListBox.Items[e.Index] back into a FileInfo.

This code is just a subset of your code.
  1. I created the files generic list in the Load event from just the files in My Documents: you will create/recreate it in your TreeView SelectedIndexChange from the files in your selected directory.
  2. In your lstFiles SelectedIndexChanged, you will not need to create a FileInfo object. lblSize is just lstFiles.SelectedValue.
Keep smiling: you're just about done! :D Sam
Apr 19 '07 #13
Sam,
I don't want the files refer to MyDocuments. I want to refer it to a drive c. Can I do like this as below?
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             List<FileInfo> files = new List<FileInfo>();
  4.             foreach (string s in System.IO.Directory.GetFiles("c:\\"))                files.Add(new FileInfo(s));
  5.  
By the way, my treeview coding is as below:
Expand|Select|Wrap|Line Numbers
  1.        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 TreeNode node = e.Node;
  6.                 string strFullPath = node.FullPath;
  7.                 DisplayFiles(strFullPath);
  8.  
When I try to do as excatly what you did, my treeview display nothing. But this time, the highlight funtion works properly.

I can't smile anymore...i am crying for this sick coding....having headace and can't think of any....:(

thanks for being such patient in guiding me, a real dumb person in this programming language:(...
Apr 19 '07 #14
SammyB
807 Expert 512MB
> a real dumb person in this programming language
No you're not: I give up on the dumb ones! BTW, if I stop responding, it doesn't mean that I've decided that you are dumb. I'll be of of town Friday thru Monday.

Let's write down the big picture:

TreeView
  • contains the contents of C drive
  • looks like left side of Explorer window
  • initialized by form load
ListBox
  • contains the files in the directory of the currently selected Tree Node
  • loaded by the SelectedIndexChanged event of the TreeView via the DisplayFiles routine
  • has the DrawMode set to OwnerDrawFixed
  • Each Item is drawn by the DrawItem event
DisplayFiles
  • called when the SelectedIndexChanged event of the ListBox fires
  • reloads the files in the ListBox
  • creates a new generic list of FileInfo objects, one object for each file in the currently selected directory
  • generic list is used by ListBox via DataSource, DisplayMember & DisplayValue property
  • code is like Sam's Form load event
lblFile
  • contains the filename of the currently selected ListBox item
  • lblFile.Text = lstFiles.SelectedItem.ToString()
lblSize
  • contains the size of the currently selected ListBox item
  • lblSize.Text = lstFiles.SelectedValue.ToString()
Does this help? Is there any more events or controls? --Sam
Apr 19 '07 #15
Hi Sam, sorry for the late reply.
Regarding on the post above, I did have the events and controls. Besides, I have btnShowFiles events where it loads the path (e.g.:c drive) and I can click to expand and collapse the treeview.
Another controls is FillDirectory. This function will let users to expand up to certain levels to see the files/directories.

After been doing some debuging, I found that I have a problem of my coding in path reference area. What I mean is if I did the coding as below, the listbox ONLY display the files in C drives. This is because the coding only ask to reference to C drives and get the files not directories. How is the coding in order to make it display all the directories and files in C drives?
Expand|Select|Wrap|Line Numbers
  1.             foreach (string s in System.IO.Directory.GetFiles("c:\\"))
  2.                 files.Add(new FileInfo(s));
  3.  

And another problem is the files directly display in the listbox. I want to make it displays the files and directories in the listbox based on selected node of treeview. How to do that?

Another questions:
Based on the coding below, my path reference is as stated in the code.
Expand|Select|Wrap|Line Numbers
  1.         private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
  2.         {
  3.             try
  4.             {
  5.                 lblDir.Text = treeView1.SelectedNode.FullPath;
  6.                 string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
  7.                 FileInfo fi= new FileInfo(path);
  8.  
When I trying to make the similar reference of the path as above coding to DrawItem events, i get an error massage:
NullReferenceException was unhandled - Object reference not set to an instance of an object.
Expand|Select|Wrap|Line Numbers
  1. private void lstFiles_DrawItem(object sender, DrawItemEventArgs e)
  2.         {
  3.             //foreach (string s in System.IO.Directory.GetFiles("C:\\"))
  4.               //  files.Add(new FileInfo(s));
  5.  
  6.             string path = Path.Combine(treeView1.SelectedNode.FullPath, lstFiles.SelectedItem.ToString());
  7.             FileInfo fi = new FileInfo(path);
After all, the path is the refernce that I want....how to code it?

Thanks. Hope this is clear for you...
Apr 25 '07 #16
SammyB
807 Expert 512MB
...the coding only ask to reference to C drives and get the files not directories. How is the coding in order to make it display all the directories and files in C drives?...
You'll need two loops: first with Directory.GetDirectories, then Directory.GetFiles. Both return a string array.

.... I want to make it displays the files and directories in the listbox based on selected node of treeview.
The SelectedIndexChanged event of the Treeview must reload the listbox.

.... my path reference [in the DrawItem event]....
You no longer have the path, but you still have all of the FileInfos, use them:
Expand|Select|Wrap|Line Numbers
  1.      FileInfo fi = lb.Items[e.Index] as FileInfo;
  2.      // Define the default color of the brush as black.
  3.      Brush myBrush = Brushes.Black;
  4.      if (fi.Length > 4000)
  5.         myBrush = Brushes.Red;
Apr 25 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RC | last post by:
I have a form where the user enters the Model info. The user picks the Model from either a combobox or listbox which both are filled from the Models table. If the Model type isn't in the boxes,...
3
by: Simon Rowe | last post by:
Probably really simple but I cant work it out... I have a list box on a form with a few records in. When I open the form the first record is sort of highlight with a dashed box, when I cursor...
18
by: Alpha | last post by:
Hi, I'm working on a Windows applicaton with VS 2003 on windows 2000. I have a listbox that I have binded to a dataset table, "source" which has 3 columns. I would like to display 2 of those...
0
by: Namespace | last post by:
Hi I am having trouble selecting more than one value as selected in a listbox through code. The property "ListSelectionMode.Multiple" is set. There is no problem selecting manually (CTRL +...
3
by: Gummy | last post by:
Hello, I have an ASPX page on which I place a UserControl 15 times (they only need to be static controls on the page). This UserControl is a set of two listboxes with radiobuttons above the...
0
by: uninvitedm | last post by:
I'm making a JSP page where (ideally) you select files to upload, each selected file is added to a listbox. The form is submit, (along with some other parameters), and the files are uploaded. They...
5
Scott Price
by: Scott Price | last post by:
I'm not quite ready to give up on this yet... Using MS Access 2003, WinXP SP2. I have a listbox that I'm trying to get to highlight (select) a specific record using the GotFocus event (the...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
1
by: balurbabu | last post by:
I am asp.net 2.0 c#.net, I have a list box,listLocation which allows the user to select mulitple selections. These are stored in a database as comma separated values like bangalore,chennai,delhi...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.