SortDescription sd = new SortDescription("Content", ListSortDirection.Ascending);
comboBox1.Items.SortDescriptions.Add(sd);
Sorting Combobox Items Alphabetically
To sort a collection of items inside a combobox alphabetically, one way to do it would be to use the SortDescription class. For this, first you need to add the System.ComponentModel namespace. Then initialize a new SortDescription class in which you can tell by what and how to arrange the list. And finally simply add the class to the combobox you wish to be arranged. In case you want to arrange a simple combobox (only one thing to arrange it by) use the "Content" string when initializing the class as shown in the example bellow:
Get The Total Size of Multiple HDD
The following code will show you how it can be retrieved the total size of multiple HDDs installed on your system. Using the same method you can do this for available free space. I've also converted bytes to GB as the value is retrieved in bytes. For this you can use the Size property from the Win32_DiskDrive class:
private static String getHDDsize()
{
Double sum = 0;
ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * From Win32_DiskDrive");
foreach (ManagementObject item in mos.Get())
{
double HDD_size = Convert.ToDouble(item["Size"]);
sum += HDD_size;
}
return Convert.ToString(Math.Round(sum / 1073741824, 2)) + " GB";
}
Retrieving the System's Total Physical Memory
Recently I was creating an app in which I needed to display the total physical memory inside a label. At first I was tempted to retrieve the memory using the Capacity property from Win32_PhysicalMemory, but it seems that this property retrieves the memory from each module. Although it does the job, I found out that is more efficient to retrieve the total physical memory using the TotalPhysicalMemory property from the Win32_ComputerSystem WMI class.
The following code shows you how to retrieve it using a foreach loop. Note that this property retrieves the memory in bytes and I've did some calculations to display it in GB. Then, this method can be used to display the memory amount inside a label... or whatever.
The following code shows you how to retrieve it using a foreach loop. Note that this property retrieves the memory in bytes and I've did some calculations to display it in GB. Then, this method can be used to display the memory amount inside a label... or whatever.
private static String getRAMsize()
{
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject item in moc)
{
return Convert.ToString(Math.Round(Convert.ToDouble(item.Properties["TotalPhysicalMemory"].Value) / 1073741824, 2)) + " GB";
}
return "RAMsize";
}
Get Picture Information inside a StatusStrip
In this brief tutorial I will show you how you can get basic information from a picture and display them inside StatusLabels on a StatusStrip control.
If you are using C# 2010 Express, like I am, you will notice that it's very easy to do this. First, set up a new project and on the main form add a pictureBox, a button, and a statusStrip. On the statusStrip you need to add 4 statusLabels in which we will display the Picture's Pixel Size, the Picture's KB Size, the Picture's Path from where it was loaded and the Cursor Coordinates on the PictureBox.
At first we will set up some initial text for the statusLabels and set up some borders between them.
Then we will double click the button that we have added to add a Click event which will allow us to load a picture inside the pictureBox.
Last, we will need to set up a MouseMove event on the Picture box to update the cursor's coordinates.
In case you will no longer see some of the Status Labels upon updating them, it means that the form is too small and there is no room to display them. Enlarging the form should fix the problem.
You should obtain something like in the bellow screenshot:
If you are using C# 2010 Express, like I am, you will notice that it's very easy to do this. First, set up a new project and on the main form add a pictureBox, a button, and a statusStrip. On the statusStrip you need to add 4 statusLabels in which we will display the Picture's Pixel Size, the Picture's KB Size, the Picture's Path from where it was loaded and the Cursor Coordinates on the PictureBox.
At first we will set up some initial text for the statusLabels and set up some borders between them.
public Form1()
{
InitializeComponent();
//we set the text for the initial statusStrip statusLabels
picSize.Text = "Picture Pixel Size: "; //will display the picture's pixel size
picSizeKB.Text = "Picture KB Size: "; //will display the picture's size in KB
cursorPosition.Text = "Cursor Position: "; //will display the position of the cursor on the picture
filePath.Text = "Picture Path: "; //will display the path from where the picture has been opened
//creates a Right side border for every statusLabel on the statusStrip only to separate them
picSize.BorderSides = ToolStripStatusLabelBorderSides.Right;
picSize.BorderStyle = Border3DStyle.RaisedOuter;
picSizeKB.BorderSides = ToolStripStatusLabelBorderSides.Right;
picSizeKB.BorderStyle = Border3DStyle.RaisedOuter;
cursorPosition.BorderSides = ToolStripStatusLabelBorderSides.Right;
cursorPosition.BorderStyle = Border3DStyle.RaisedOuter;
//the text displayed on the button
button1.Text = "Load Picture";
//note that all text and borders we set up earlier can be easily
//setup from the respective control's proprieties panel
}
Then we will double click the button that we have added to add a Click event which will allow us to load a picture inside the pictureBox.
private void button1_Click(object sender, EventArgs e)
{
//when clicking the button we open a window that allows us
//to select the picture we want to load inside the picture box
OpenFileDialog openPic = new OpenFileDialog();
//this Filter allows us to open only JPG files
openPic.Filter = "Image Files(*.jpg)|*.jpg;";
if (openPic.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = new Bitmap(openPic.FileName);
}
//We update the picSize label to show the pixel size of the picture we opened
picSize.Text = "Picture Pixel Size: " + pictureBox1.Image.Width + " X " + pictureBox1.Image.Height;
//The FileInfo is located in IO framework class, so if you haven't called it yet, you should
//before the namespace you will have to add the line "using System.IO;"
//We need this class to get the size of the picture we opened
FileInfo fi = new FileInfo(openPic.FileName);
//Displays the size of the picture in KB
//We have to divide it by 1024 as the Length method calculcates the class in bytes
picSizeKB.Text = "Picture KB Size: " + fi.Length / 1024 + " KB";
//Updates the file path
filePath.Text = "File Path: " + openPic.FileName;
}
Last, we will need to set up a MouseMove event on the Picture box to update the cursor's coordinates.
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
//An if will be used as we don't want anything to be updated unless a picture is loaded
//inside the if statement we check to see if an image is loaded
if (pictureBox1.Image != null)
{
//We add a MouseMove event to the picturebox
//to display the mouse coordinates on the picture
cursorPosition.Text = "Cursor Poistion: (" + e.X + " : " + e.Y + ")";
}
}
In case you will no longer see some of the Status Labels upon updating them, it means that the form is too small and there is no room to display them. Enlarging the form should fix the problem.
You should obtain something like in the bellow screenshot:
Button Effects Using Mouse Events
Say for example you want to create a button that when the mouse cursor hovers over it the button gets bigger and highlighted. Well, in C# if you want to make the button bigger one way to do it would be to use two Mouse events, MouseEnter and MouseLeave.
First you'll have to set the size and location of your button. Then, in the MouseEnter event assigned to the button you'll have to change the location of the button (usually it's about 4-10 pixels on both axis) and then you'll also have to increase the button's size 2 times the amount of pixels you moved it (on both axis - this way the button will remain in the same position). Finally, in the MouseLeave event (also assigned to the button) you will have to return the button to its original location and size.
Following is an example:
First you'll have to set the size and location of your button. Then, in the MouseEnter event assigned to the button you'll have to change the location of the button (usually it's about 4-10 pixels on both axis) and then you'll also have to increase the button's size 2 times the amount of pixels you moved it (on both axis - this way the button will remain in the same position). Finally, in the MouseLeave event (also assigned to the button) you will have to return the button to its original location and size.
Following is an example:
public Form1()
{
InitializeComponent();
//sets the size of the form
this.Size = new Size(500, 500);
//sets the Location of the buton
button1.Location = new Point(100, 100);
//sets the suze of the button
button1.Size = new Size(100, 35);
}
private void button1_MouseEnter(object sender, EventArgs e)
{
//updates the button's Location
button1.Location = new Point(96, 96);
//updates the button's Size
button1.Size = new Size(108, 43);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
//returns the button to its initial location
button1.Location = new Point(100, 100);
//returns the button to its normal size
button1.Size = new Size(100, 35);
}
Subscribe to:
Posts (Atom)