Order of Events in Windows Forms

1: Application Startup and Shutdown Events:
Control.HandleCreated
Control.BindingContextChanged
Form.Load
Control.VisibleChanged
Form.Activated
Form.Shown

When an application closes, the shutdown events of the main form are raised in the following order:
Form.Closing
Form.FormClosing
Form.Closed
Form.FormClosed
Form.Deactivate

The ApplicationExit event of the Application class is raised after the shutdown events of the main form.

2: Focus and Validation Events:

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ActiveControl property to the current form, focus events of the Control class occur in the following order:

Enter
GotFocus
Leave
Validating
Validated
LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events of the Control class occur in the following order:

Enter
GotFocus
LostFocus
Leave
Validating
Validated

(From Microsoft site: http://msdn.microsoft.com/en-us/library/86faxx0d.aspx)


Communicate with USB port using C#

I worked a GUI project using C# for an audio company. This project needs PC GUI program communicate with a hardware board which has a USB port so that the GUI and the board switch data.

The hardware board has a micro controller chip (MCU), Actually it is Microchip’s PIC. Then we know we can use PIC USB Bootloader in firmware side.

We can download Microchip’s software package about PICDEM Full Speed USB evaluation kit.

And, There is a good guide article from Evan:
PIC USB Bootloader using Microchip frimware and C#

Actually, our GUI program has little different way to communicate with PIC via USB port, let me find time to write later.


How to auto scroll to the bottom of a multiline TextBox in C# ?

At least in .NET 2.0, the TextBox control doesn’t have a property to let the TextBox automatically scroll to the bottom when it is set to multiline.

You have to write code like this:

textBox1.Text += “Test string \n”;
textBox1.Update();
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

OR, you can use the following code:

textBox1.AppendText(“This is a test string \n”);
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

ComboBoxItem Class for .NET 2.0

Sometimes when we use the ComboBox control in .NET 2.0, we need add items which have "combination" data, such as we need to add the following data to a ComboBox:

("string1", 1)
("string2", 1)
("string3", 3)

Normally in .NET 2.0, we just add "single" object to a ComboBox control, how we can add above data ?

In .NET 3.0 or above version, there is a new class named "ComboBoxItem", we can use this class to add "complex" data structure objects to a ComboBox. But, in .NET 2.0, we can not have a ComboBoxItem.

OK, just let us make a ComboBoxItem by ourself:

class MyComboBoxItem
{
public string Name;
public int Value;

public MyComboBoxItem(string Name, int Value)
{
this.Name = Name;
this.Value = Value;
}

public override string ToString()
{
return this.Name;

}
}

Note: we need override ToString() method, otherwise when we use this ComboBoxItem, you just get the ComboBoxItem name itself.

Then you can add items like this:

aComboBox.Items.Add(new MyComboBoxItem("string1",1));
aComboBox.Items.Add(new MyComboBoxItem("string2",2));
aComboBox.Items.Add(new MyComboBoxItem("string3",3));

And you can get selected value like this:

int selectedValue = ((MyComboBoxItem)aComboBox.SelectedItem).Value;