MS .Net C# winform UI related tips:Increasing the width of label according to text width (winform Label wordwrap)

If you want Label to grow as its text grows:


(this will make your label to grow in width)
if you want text to get wrapped and your label to become, multiline automatically:

Label.MaximumSize = new Size(max_width_of_label, 0);

(in above label will grow but will not take care of other control, i mean it will not care about them, just try your self and see what happens)

You may also like to see:

Published in: on July 23, 2010 at 4:05 pm  Leave a Comment  
Tags: ,

C# Static class and constructor: constructor in static classes in c#

Static classes in C# doesn’t have instance constructor(constructor used to initialized instance of a class) since it can’t be initialized. Static class is initialized only once when they are used first time in application.

So i was wondering how we can initialize a member if really we need. The answer is using static constructors.

Here is the example of how you can use static constructors in your static class:

static class StaticClass
static StaticClass()
System.Windows.Forms.MessageBox.Show(“I m crated”);
public static String Hi()
return “hi”;

Here, messagebox will be shown when first time StaticClass.Hi() method will be called.

Note: static constructors could be used in normal classes also.

C# tips & tricks: Do you want to rethrow exception ?

Many times during development we really don’t want to handle exception every where, rather we like to throw it again. But what is the best way to do it:

catch(Exception ex)


catch(Exception ex)
throw ex;

This article I found in net gives really good insight of it. It suggests to use first method, as it would not truncate previous stack trace information.

C# tips & tricks: String Concatenation, Best practices

When we are struggling with String concatenation issues, specially which is the best way to concatenate the strings. One of my colleague suggested following articles to me.

though this may not be giving every thing you are looking for, but really very nice and worth readable for the .NET developers.

C#:Handling Enter key press event for textbox.

If you want to do something when focus is on textbox and and user press ‘Enter’ key, do the following way:

private void txt1_KeyPress(object sender, KeyPressEventArgs e)</code>
 if (e.KeyChar == (char)Keys.Enter)
    //do something

where txt1 is textbox

Published in: on August 5, 2009 at 3:31 pm  Leave a Comment  
Tags: , , ,

.Net and MySQL: Example of using MySQL and .Net: A simple example of useing Connector/Net 5.2 to connect with MySQL from .Net

Follow this process:

  1. Download Connector/Net 5.2.
  2. Unzip the binaries. You will find 3-4 dlls along with other files.
  3. Add reference to, in your project.
  4. You can connect to MySQL throw code, as described below:

String conString = “server=server_name; user id=user_name;

password=password; database=db_name; pooling=false;”;

String queryString = “select * from employee;”;

using (MySqlConnection mySqlConnection = new MySqlConnection(conString))


using (MySqlDataAdapter adapter = new MySqlDataAdapter(queryString, mySqlConnection))


DataTable dt = new DataTable();


dataGridView1.DataSource = dt;



Published in: on July 27, 2009 at 8:39 pm  Leave a Comment  
Tags: , , , , , ,

C# .Net and MySQL: how to connect MySQL from .Net ?

I was wondering how to connect MySQL with .Net then i got following solutions in net:

There might be several other articles describing several other ways to connect with MySQL from .Net but I found Connector/Net 5.2 from MySQL good.