Border Collision Detection - Bouncing Control

To make a label (or any other control.. I just choose label as an example) bounce when it hits the border of a form you'll have to invert the speed with which the label is incremented each tick.

Here's an example:

I will use a label which I've called lblAI, two speed int (AISpeedX and AISpeedY - the speeds with which the label moves on the X and Y axis), two const int (SCREEN_WIDTH and SCREEN_HEIGHT) and one timer which I've called gameTime. Also I've added one Panel called gameArea, but it's not really required.

First we will have to declare our variables just above the constructor:

        const int SCREEN_WIDTH = 700;
        const int SCREEN_HEIGHT = 400;
        int AISpeedX = 3;
        int AISpeedY = 2;

Then we will need to give an initial location to the label. We can do this in the Form1_Load event:

private void Form1_Load(object sender, EventArgs e)
        {
            lblAI.Location = new Point(SCREEN_WIDTH / 2 - lblAI.Width / 2, SCREEN_HEIGHT / 2 - lblAI.Height / 2);
        }

The next step would be to add the Tick event to our timer and inside it we will move the label by incrementing the speed values. Also, at each tick we will check to see if the label's location does not exit the form's borders. For this example I've used a 1 millisecond interval for a smooth result, but you can user whatever you like. Also, remember to enable the timer as it is disabled by default.

private void gameTime_Tick(object sender, EventArgs e)
        {
            if (lblAI.Location.X < 0 || lblAI.Location.X > (SCREEN_WIDTH - lblAI.Width)) AISpeedX = -AISpeedX;
            if (lblAI.Location.Y < 0 || lblAI.Location.Y > (SCREEN_HEIGHT - lblAI.Height)) AISpeedY = -AISpeedY;

            lblAI.Location = new Point(lblAI.Location.X + AISpeedX, lblAI.Location.Y + AISpeedY);
        }

As you can see, at each tick we check to see if the Location on the X axis is lower than 0 or higher than the screen width, and if it is we invert the incrementation value on the X axis. Same goes for the Y axis.

You can notice that when checking for the higher value I've subtracted the label's width for the X axis and the label's height for the Y axis. I did this because we are using the label's location value which is given by the top left point of the label. If we wouldn't have had subtracted the width and the hight respectively we would see the label leave the area until the location point would have been greater than the width or height of the area.

Form Random BackColor

If you want to display a random BackColor for your form or simply generate a random color you can use the FromArgb method from the Color class with 3 int random arguments for Red, Green and Blue.

Here is an example where the form's BackColor is changed every time a button is clicked. In this example I've also added 3 labels to display the 3 generated numbers.

private void button1_Click(object sender, EventArgs e)
        {
            Random RandomClass = new Random();

            int rndRed = RandomClass.Next(0, 255);
            int rndGreen = RandomClass.Next(0, 255);
            int rndBlue = RandomClass.Next(0, 255);

            this.BackColor = Color.FromArgb(rndRed, rndGreen, rndBlue);

            lblBlue.Text = "Blue: " + rndBlue.ToString();
            lblGreen.Text = "Green: " + rndGreen.ToString();
            lblRed.Text = "Red: " + rndRed.ToString();
        }

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:

SortDescription sd = new SortDescription("Content", ListSortDirection.Ascending);
comboBox1.Items.SortDescriptions.Add(sd);

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.

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";
        }