Computer Science 213

C# Code Snippets for Visual Studio

When you need code for a specific job, then check out this list. Instead of scrounging around in your notes, I put some major code snippets here for you. In most cases they are copied from our C# classroom projects.

 
  • Forms

 

Change form background color or change form size at run time.
Make
ENTER key work like TAB

How to launch a browser & go to URL
  • Data Entry
  • Managing Data Entry Issues

Copy selected item in Listbox to a textbox
How to
concatenate strings
Simple math operations (
computing sales tax)
How to display items & pricing as currency

  • Event Handlers
  • Using Control Arrays & Lists
  • Using List Boxes

Add Item to list box
Remove list box item
Load list box with items & pricing information at design time
Load List Box with Items & Pricing at run time
Getting the list box items, pricing, & photo information
Foreach Loop and listboxes
  • Menus & Windows Dialog Boxes
Using Drop-Down menus and Windows dialogs
Using OpenFile Dialog Box
  • Using Controls

Web Browser control
How to display image in a picture box
 
  • If , Switch, & Loops
  • Bitwise Operations
  • Formatting Strings

How to format numbers
How to format data as currency
How to create multi-line text in a label
Working with escape sequences
 
  •  Custom Methods
How to create custom methods (functions)
  • String Functions

How to use the TRIM function
Using Substring instead of Left, Mid, Right
 
  • Variables

How to use module-level variables
Using Totals & Counts with module-level variables
Using variables to add contents of textboxes
 
  • Collections (For Each loops, Lists<>)


foreach Loop using a list box
foreach loop to f
ind textboxes in the controls collection
foreach loop for clearing textbox controls inside a container control
 

Working with Lists<>

 

  • File I/O & Networking
  • Classes

Introduction to Classes Part 1
Introduction to Classes Part 2

 

  • ADO Stuff

DataSources and SQL with error handling
Read SQL DB using ADO in Code 

 

  • Extras

Copyright 1999-2016 Ronald P. Kessler, Ph.D.
All rights reserved.

Revised: 9/1/2016