Posts

C# Numeric Only TextBox

Once in a while you find yourself needing to stop certain keypresses in a C# desktop application.  I've found the best way is to handle this in the keypress event of a text box.  This is some very small code that shows how to only allow numeric digits to be pressed. private void textBox_KeyPress( object sender, KeyPressEventArgs e ) {      e.Handled = !char.IsDigit( e.KeyChar ); } Now if you want to allow other characters, like the decimal, etc you need to get a bit fancier. private void textBox_KeyPress( object sender, KeyPressEventArgs e ) {        if( char.IsDigit( e.KeyChar ) ||            e.KeyChar == '.' )        {           e.Handled = false;       }       else       {        ...

C# Form Application in Kiosk Mode/Fullscreen

Sometimes you need to have a C# windows form application run in full screen mode.  Like you see on a kiosk at a mall or some stand alone machine.  It's fairly easy you just need to set some of the properties on the Form either in it's OnLoad method or directly in the designer. In the OnLoad method of the main Form set these values, or again set them directly in the designer. this.MaximizeBox = false; this.TopMost = true; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

C# String Performance

In C# I have read a lot of debate over which way is the best way to do String Concatenation.  I have often read never ever use String1 + String2 due to the memory allocations and time constraints.  I decided to run some tests on the various way of doing String manipulation.  The table below shows the iterations, total times, and average times for some common way of doing string addition.  This was just a test of time, there was no memory consumption test.  All tests were doing adding 7 strings together that were stored in variable names.  There was no string a = "a" + "b";  It was all string a  = var1 + var2;  These were creating a single string for the listed iterations using the method(s) below. I was surprised to see the + method came out ahead in all the tests.  String builder came in a close second.  String.Format was extremely slow but allows for localization which is another story. iterations String + average ...

C# Use a stored procedure to fill a dataset/table

When you fill a DataSet/Table in C# you use a SQL Data Adapter. The code for this is geared towards a SQL SELECT statement. However I'm not a big fan of raw SQL in my code so I found a way to use a stored procedure instead. public static DataTable GetTable() { DataTable dt = null; try { using( SqlConnection connection = new SqlConnection( "your connection string" ) ) { using( SqlDataAdapter sda = new SqlDataAdapter() ) { using( SqlCommand command = new SqlCommand( "your stored procedure", connection ) ) { command.CommandType = CommandType.StoredProcedure; // this line needs to be added for every parameter the SP expects command.Parameters.AddWithValue( "parametere value", paramValue ); sda.SelectCommand = command; dt = new Da...

C# execute a stored procedure using SQL Server

Below we have some code that opens a connection to a database using a connection string then executes a stored procedure. This obviously needs to be placed in some method or class. You will need to add your own connection string and parameter values. try { using( SqlConnection connection = new SqlConnection("some connection string" ) ) { using( SqlCommand command = new SqlCommand( "stored procedure name", connection ) ) { command.CommandType = CommandType.StoredProcedure; // this line needs to be added for every parameter the SP expects command.Parameters.AddWithValue( "parametere value", paramValue ); connection.Open(); // based on the what the SP does, call the execute non query, execute scalar, or execute reader method command.ExecuteNonQuery(); } } } cat...

C# convert string array to integer (or something other kind of data type) array

Many times in coding you are dealing with transforming data from one type to another.  I often find myself creating integers, longs, etc from strings.  Especially in .net for some reason.  I got tired of writing tons of code to handle this.  .Net gives you a great way to convert an array from one type to another. Currently I would always end up writing a for loop iterating through the array and doing a manual copy.  It would look something like this: string[] someStringValues; int[] someIntValues = new int[someStringValues.Length]; for( int i = 0; i < someStringValues.Length; i++ ) {    someIntValues[i] = int.Parse( someIntValues[i] ); } Not horrible code, but there is a more succinct way of doing it! string[] someStringValues; int[] someIntValues = Array.ConvertAll<string, int>( someStringValues, int.Parse ); We have now moved all that code into 1 line.  With some more magic we can also move this into a generic templated solu...

C# Simple Binary Tree

Today I'm getting a bit into data structures. Recently I've found a need to store a large amount of records (16 million) in memory as a sort of cache. I tried using databases, etc, but it was all too slow. I needed super fast access times, well under a millisecond to speed up a large application with many data look ups. I tried a linked list but while the insert times were great the look ups again were too slow. I finally settled on a binary tree. Building the tree is slow, several minutes, but this data doesn't change and gives me super fast look up times, around .005 ms. To achieve this I wrote a very simple binary tree class. It uses a node that is fit for my purposes. To make this class even better I would implement in generics with a node class that has to implement IComparable. This way you can make a binary tree for any type of class that can be compared. For now I'm hard coding the key/values for my specific needs. See the rather large code block bel...