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

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.

finding information about an IP address on the web

Today i need to find information about an ip address, i got from internet. This thing can also happen when you get any spam or something from an ip and you need information about that ip. ARIN WHOIS Database Search helped me to get more information about ip. following is the URL of it:
http://ws.arin.net/whois/

For example if you want to know whom 216.239.51.99 ip belongs, you can type this ip in left text box on the site and on clicking submit query button you will have complete information.

HTML/JavaScript: Dynamically populating combobox/select options

I got a problem that how to dynamically populate values in html combo box using javascript. Or rather say, how to dynamically add options in select element of html.
here is our html code:

<select id="id_s">
 <option value="a">first</option>
 <option value="b">second</option>
</select>

Now suppose i want to add one option ‘third’, here is the code for it:

var combo = document.getElementById('id_s');
combo.options[2] = new Option ("third", "c");

Courtesy : This forum.

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.