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:

Label.AutoSize=true;

(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);
Label.AutoSize=true;

(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:

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

Some interesting read on Bit Twiddling: setting and resetting(clearing) bit

a | b…. a & c…….a^b…..~a*(a^b)…..

If above criptic notations make some sense for you and you enjoyed reading above then here is some resource for you. I was actually searching way to set or reset a bit on a number. Finally i got some solution to share:

Setting a bit:
number = number | (1 << n); //(it will set (n+1) th bit)

e.g.:
num1 = 32;//100000
n = 2;
num2 = num1 | (1<<n);//num2=36 (100100)…it is setting 3rd bit

Resetting(Clearing) a bit:
number = number & ~(1<<n); //it will clear (n+1)th bit.

e.g.:
num1 = 34; //100010
n = 1;
num2 = num1 & ~(1<<n); //num2=32(100000)…it is clearing 2nd bit

using above we can write function to set and reset bit:


/*set the bit at bitPos*/
int setBit(int num,int bitPos)
{
  return num | (1<<(bitPos-1));
}

and to clear:

/*reset(clear) the bit at bitPos*/
int resetBit(int num, int bitPos)
{
  return num & ~(1<<(bitPos-1));
}

Here I have found some good resource on bit twiddling:

Bit Twiddling Hacks

Some thing more advance:

Advanced bit manipulation-fu

reference:

1. Stack Overflow

How To display table in GridView: Dynamically filling data in GridView from DataTable in ASP .Net

I had a datatable and i needed to show in a GridView in my ASP .Net page. Following is the code which help me to do so.

//get table to be shown in grid view;
DataTable dataTable = GetRequiredTable();
//assing table to the DataSource property of GridView
myGridView.DataSource=dataTable;
//bind data from a data source to the GridView control
myGridView.DataBind();

Here GetRequiredTable() is a user(example) function which return a table to display in to gridview.
You can also use DataSet in place of DataTable.

Published in: on November 17, 2009 at 10:27 am  Leave a Comment  
Tags: , ,

DCOM Error : Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.

When we tried to use word automation from .Net in ASP .Net application earlier it was working but then it stopped working in some of the machine giving error
“Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005.”.
We had DCOM setting in place, IIS setting was also ok. Then I got following solution:

  • I changed the AppID Registry key under HKEY_CLASSES_ROOT\AppID\WINWORD.EXE from old entry to {00020906-0000-0000-C000-000000000046}.

(for some it will work after this, but if still its not working go for second.)

  • 2. Also changed the DCOM setting.
    • a. Open the Component Service Console.(Either by running command: dcomcnfg OR by opening it from Control Panel->Administrative Tool -> Component Services)
    • b. Now Select ‘Microsoft Word Document’, Under Component Services->Computers->My Computer -> DCOM Config.
    • c. Do Right click and select Properties.
      it will open Microsoft Word Document Properties dialog.
    • d. In Microsoft Word Document Properties dialog, select Identity tab.
    • e. In Identity tab, if ‘The launching user.’, in our case third option was selected, so we selected 2nd option.

This may solve out problem, hope it would be helpful for you also. If you need to say something please feel free to speak through comments.

 

References:

http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/006cbbac-101a-40ea-ae16-d59a7a2da8b1

Published in: on November 3, 2009 at 4:17 pm  Comments (7)  
Tags: , , , , , , ,

A very (extremely) simple ASP .Net Hello World example, Just for fun…

I was going through an ASP.Net book, and thought of writing extremely simply Hello World example. This will give an insight of how simple and small page you can write using ASP .Net.

For each example I have given asp .net code and corresponding browser generated code.

Example1

<%@ Page Language="c#"  %>
<%Response.Write("Hello World");>

Brower Source:

Hello World

Example2:

<%@ Page Language="c#" %>
<script runat="server">
void SayHello()
{
Response.Write("Hello World !");
}
</script>
<html>
<body>
<% SayHello(); %>
</body>
</html>

Brower Source:

<html>
<body>
Hello World !
</body>
</html>

Adding macro in to toolbar as tool bar button, creating macro and adding it, with small example

How nice it would be if your favourite macro is available as toolbar button. Its really easy to do. Suppose you have created a macro hello and you want to add it, then just follow these step:

Adding macro to toolbar

  1. Go to menu: tools->Customize… => it will open Customize window.
  2. In Customize window go to ‘Commands’ tab and select ‘Macros’ at left pane, all the macros you have created would be visible at right pane.
  3. Just drag the macro you want to toolbar.
  4. It would automatically create a button with macro name.

Customizing toolbar macro button

  1. Write click on macro toolbar button while Customize window is open.
  2. In this menu you can do all the customization. Changing name, image etc.
  3. You can also assign a shortcut key. Just put &1(if you want alt+1 to a shortcut key) or which ever combination you want.

Office tips and tricks: Creating and adding new toolbar in ms office, a very simple example

You would have seen many toolbars in your office app. Have you ever thought of having toolbar with your own name ;). Yes you can add new tool bar with some really simple step in to your office app(word/outlook/excel etc.). So here we go:
1. Go to menu: tools->Customize… => it will open Customize window.
2. In Customize window go to Toolbars tab and click on “New…” button. It will open a small window here you need to give toolbar name.
3. Your toolbar is ready to add.
4. You can drag buttons from Customize window’s Command tab.
5. For ex. in Command tab select ‘File’ at left pane and drag ‘Open’ in right pane to the newly created toolbar. Its done.

You can also add previously created macro in to this toolbar.

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#: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 mysql.data.dll, 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();

adapter.Fill(dt);

dataGridView1.DataSource = dt;

}

}

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