Tuesday, November 30, 2010

Common C# Controls

    There are a TON of controls in the C# toolbox, which is by default docked on the left side of your C# windows.  If you need a certain something on your form, there's probably a control for it in the toolbox.  The problem is: What is that control I need?

    Here's a quick list of the controls I feel are the most useful, the 3-letter prefix I use, and what they are for.

    Before we start, however, I would like to say that the following properties are pretty much always important:
    • Name is how your control will be referenced in your code.
    • Anchor is how your control will change in size/position when your form is resized.
    • Enabled is whether your control is usable.
    • Location is where the control is.
    • Size is how large your control is.
    • Text is what is displayed through your control.
    • Visible is whether or not your control can be seen.
    • TabIndex arranges the order of the controls that are selected as the user repeated presses the Tab key.
    And remember, the Click event is important for nearly every control because it's what happens when the control is clicked.

    Now, here's the list:
    • Form: (frm) This is what holds everything together!
      • Properties:
        • FormBorderStyle is your type of form.  I like to choose FixedDialog to make sure the user can't resize the form.
        • Icon is the picture displayed in the left of the title bar.
        • TopMost is whether your form will stay on top of all other windows (except other TopMost forms) like Task Manager.
        • MaximizeBox and MinimizeBox are whether the Form can be maximized or minimized, respectively.
        • ShowInTaskbar is whether your form will be shown in the Windows taskbar.
      • Events:
        • FormClosing is when your form is closing and you can stop it from closing by putting "e.Cancel = true;" in the method.
    • Button: (btn) The heart and soul of a form.  There's almost nothing useful you can make without a button.
    • Label:  (lbl) This is just some text that describes something on your form.  Not much more to it.
    • TextBox: (txt) A box for the user to put some text in.
    • CheckBox: (chk) This is a toggle between checked and unchecked.  In your code to test whether or not the box is checked, use: "chkCheckBoxName.Checked"
    • RadioButton: (rad) Similar to a CheckBox, except only one can be checked at a time in a group.  The same thing as the CheckBox to check whether or not the RadioButton is checked.
    • GroupBox: (grp) Used to group similar controls or have multiple groups of RadioButtons, since checked RadioButtons in a GroupBox don't affect other RadioButtons not in the GroupBox.
    • ComboBox: (cmb) A drop-down list for the user to select from.
    • Timer: (tmr) Activates an event every time the timer "ticks" based on a set interval.
    • NumericUpDown: (nud) Lets the user choose the number.  Use the Value property to get/set the number shown.
    • ProgressBar: (prg) Shows progress in a bar, like in an installer.  Use the Value property to get/set how full the bar is, and use Maximum and Minimum to set the bounds for the numbers in the ProgressBar.
    • TrackBar: (trb) Shows a sliding value bar that lets the user set something by sliding a downward pointed arrow.  You've probably seen this in Windows, such as when you change your mouse sensitivity.
    • BackgroundWorker: (bgw) Runs stuff in the background.  It's very useful because running long segments of code or long loops causes the form to freeze up, so running the same task with the BackgroundWorker allows the form to still be responsive while taking on another task.  It's multithreading!
    I hope some of this sparked some new ideas for you!  Maybe you should implement some of these into your new programs.  Or update some of your older ones with more appropriate controls.

    No comments:

    Post a Comment