Tuesday, April 21, 2009

Free Cool ListBox - Customized for alternate item colors and text wrap properties.

I have got a requirement to design a list box with alternate items coloring and text wrapping feature. I have tried for one in the google but very less help could be found.



So I designed one cool listbox that with the above requirements satisfied. I have given the code below.


My List box looks like the below image..






To acheive this, First you have to add one ListBox control and one Textbox with multiline property enabled underneath it. I have named my textbox as txtMessage and ListBox as ListBox1.



The below code shows how to add the contents from the text box on carriage return press ('Enter' button press)...






   1:  private void txtMessage_KeyPress(object sender, KeyPressEventArgs e)

   2:          {

   3:              if (e.KeyChar == 13)

   4:              {

   5:                  ListBox1.Items.Add(txtMessage.Text.Trim().ToString());

   6:                  txtMessage.Text = "";

   7:              }

   8:          }





Now, folllow the instructions below...



1.Select the ListBox1 from your design view and make the "DrawMode" property as "OwnerDrawVariable"



2. Generate the event for DrawItem and MeasureItem of the ListBox.






   1:    /// <summary>

   2:          /// DrawItem event triggers when the list box is visible in the form

   3:          /// </summary>

   4:          /// <param name="sender"></param>

   5:          /// <param name="e"></param>

   6:          private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)

   7:          {

   8:              if (ListBox1.Items.Count > 0)

   9:              {

  10:                  // Draw the background of the ListBox control for each item.

  11:                  // Create a new Brush and initialize to a Black colored brush

  12:                  // by default. And inintialise other properties to set the font 

  13:                  //bg color font size etc.. as below.

  14:   

  15:                  FontFamily family = FontFamily.GenericSerif;

  16:                  float size = 8F;

  17:                  Font myFont = new Font(family, size, FontStyle.Regular);

  18:                  Color highlightercolor = Color.White;

  19:                  Brush fontcolor = Brushes.Black;

  20:   

  21:                  //Method call to draw the back ground if the list box [Metadata]

  22:                  e.DrawBackground();

  23:   

  24:                  //Inintialize a rectangle to contain each item

  25:                  Rectangle rectangle = new Rectangle(2, e.Bounds.Top + 2,

  26:                              e.Bounds.Height, e.Bounds.Height - 4);

  27:                  

  28:                  //Select alternate items 

  29:                  int type = 0;

  30:                  type = e.Index % 2;

  31:   

  32:                  // Determine the color of the font, bg color, and brush to 

  33:                  //draw each item based on the index of the item to draw.

  34:                  

  35:                  switch (type)

  36:                  {

  37:   

  38:                      case 0:

  39:                          family = FontFamily.GenericSansSerif;

  40:                          size = 8;

  41:                          myFont = new Font(family, size, FontStyle.Regular);

  42:                          highlightercolor = Color.Gainsboro;

  43:                          fontcolor = Brushes.Black;

  44:                          break;

  45:   

  46:                      case 1:

  47:                          family = FontFamily.GenericSansSerif;

  48:                          size = 8;

  49:                          myFont = new Font(family, size, FontStyle.Regular);

  50:                          highlightercolor = Color.WhiteSmoke;

  51:                          fontcolor = Brushes.Black;

  52:                          break;

  53:   

  54:                      default:

  55:                          break;

  56:                  }

  57:                  

  58:                  // Draw the current item text based on the current 

  59:                  // Font and the custom brush settings.

  60:                  e.Graphics.FillRectangle(new SolidBrush(highlightercolor), e.Bounds);

  61:   

  62:                  // Draw each string in the Listbox, 

  63:                  e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 

  64:                      myFont, fontcolor, new RectangleF(e.Bounds.X + rectangle.Width, 

  65:                          e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

  66:                  

  67:                  // If the ListBox has focus, draw a focus rectangle 

  68:                  // around the selected item.

  69:                 // [Metadata]

  70:                  e.DrawFocusRectangle();

  71:              }

  72:   

  73:          }





The above method takes care on the alternate back ground color, font properties etc..







   1:  /// <summary>

   2:          /// Event triggers when the ListBox1 is visible in the form

   3:          /// </summary>

   4:          /// <param name="sender"></param>

   5:          /// <param name="e"></param>

   6:          private void ListBox1_MeasureItem(object sender, MeasureItemEventArgs e)

   7:          {

   8:              Graphics gr = e.Graphics;

   9:              

  10:              //We will get the size of the string which we are about to draw,

  11:              //so that we could set the ItemHeight and ItemWidth property 

  12:              SizeF size = gr.MeasureString(((ListBox)sender).Items[e.Index].ToString(), 

  13:                  ((ListBox)sender).Font,

  14:                  ((ListBox)sender).Width - 3 - SystemInformation.VerticalScrollBarWidth);

  15:              

  16:              //Set the height and width to the item

  17:              e.ItemHeight = Convert.ToInt16(size.Height) + 5;

  18:              e.ItemWidth = Convert.ToInt16(size.Width) + 3;

  19:          }





This above method declares the wrap of the content of the ListBox1.


You can alter the features as you like, hopefully all the code were explained neatly.

Monday, April 20, 2009

Marquee in Windows forms

There are scenarios where we need to use marquees in windows applications. But there are no controls introduced for this in .NET frameworks until 3.5 SP1.


I have tried some marquee controls available ready in google search and code project, but they are not thead safe in my experience i faced problems in using them in my project as it is.


Here I have given a simple code to create your own marquee that is thread safe and reasonably serves your requirement with out causing any problem.






   1:  System.Text.StringBuilder sb;

   2:          /// <summary>

   3:          /// Method called during form load. initiates MArquee items

   4:          /// </summary>

   5:          void StartMarquee()

   6:          {

   7:              //This method sets all the input for your marquee

   8:              sb = new System.Text.StringBuilder(Marquee.marqueeProperties.Text + " -- ");

   9:              //Enable the timer control 

  10:              timerMarquee.Enabled = true;

  11:              //You can simply alter the speed of the marquee

  12:              timerMarquee.Interval = Marquee.marqueeProperties.RotationSpeed;

  13:              //Change the marquee text color

  14:              txtMarqueeDisplay.ForeColor = Marquee.marqueeProperties.TextColor;

  15:              //Change the bach ground color here

  16:              txtMarqueeDisplay.BackColor = Marquee.marqueeProperties.TextBgColor;

  17:          }





The above has to be called from your windows form load where you have to have your text box control where the marquee is going to be displayed. Here my text box is named as "txtMarqueeDisplay".






   1:    /// <summary>

   2:          /// That Runs Marquee

   3:          /// </summary>

   4:          /// <param name="sender"></param>

   5:          /// <param name="e"></param>

   6:          private void timerMarquee_Tick(object sender, EventArgs e)

   7:          {

   8:              //This is the timer's tick event

   9:              char ch = sb[0];

  10:              sb.Remove(0, 1);

  11:              sb.Insert(sb.Length, ch);

  12:              //Change the 100 below to fit your requirement

  13:              if (txtMarqueeDisplay.Text.Length > 100)

  14:                  txtMarqueeDisplay.Clear();

  15:              txtMarqueeDisplay.AppendText(sb.ToString());

  16:          }





The above method the event from the timer control which is availbale from .NET 2.0
the timer is used to make sure the marque is thread safe!



Hope you got the marquee in a very simple way!!

Sunday, April 19, 2009

The Current identity (NTAUTHORITY\NETWORKSERVICE) does not have unit access

This error "The Current identity (NTAUTHORITY\NETWORKSERVICE) does not have unit access to 'C:\Windows\Microsoft.Net\Framework\v2.0.50727\Temporary ASP.NET Files'" is caused due to incorrect configuration of IIS for ASP.NET framework 2.0 and above.



To solve this, we can easily override the faulty settings and re configure the IIS using the following steps...



1. Click start and run the command 'cmd'.

2. Go to the folder "C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727>".

3. Run the command "aspnet_regiis -i".

4. This installs the required files and configure the web service extensions too.







By running the command "aspnet_regiis -i", you install this version of ASP.NET and update script maps at the IIS metabase root and for all scriptmaps below the root. Existing scriptmaps of lower version can be updated to this version.




Also note that, the webservice Extensions has permission 'Allowed' to the 'ASP.NET'. Make sure, you have given the Allowed permission as shown and highlighted in the image.








Hope this helps.



Good Luck!!

The page cannot be displayed 404. File or directory not found.

Though you have installed IIS and copied your web application in to the root of Websites in a virtual directory folder, you may receive this error.
This problem occurs when you start running your 'ASP.NET' web application probably the first time. Or you have installed .NET Framework latest version and trying to host that application in the IIS.



It's time you have to register your iis for running this current version of .NET application.





It's done by using the following simple steps..

1. Click start and run the command 'cmd'.

2. Go to the folder "C:\WINDOWS\Microsoft.Net\Framework\v2.0.50727>"

3. Run the command "aspnet_regiis -i"

4. This installs the required files and configure the web service extensions too.








By running the command "aspnet_regiis -i", you install this version of ASP.NET and update script maps at the IIS metabase root and for all scriptmaps below the root. Existing scriptmaps of lower version can be updated to this version.



Also note that, the webservice Extensions has permission 'Allowed' to the 'ASP.NET'.

Make sure, you have given the Allowed permission as shown and highlighted in the image.










Hope this post helps you to solve the problem. Please feel free to write to me if you still have problem.



Good Luck!!

Saturday, April 18, 2009

Server Application Unavailable - IIS Error

When you try to host your web application and browse, the error "Server application Unavailable" may raise to surprise you.



If you are a victim of this problem, pls read further, its far easy to solve.





Please follow the below steps...





1. Go to your web applications Virtual directory in your website root.



2. Right click and go to 'Permissions' .


3. Under the tab 'Security' you will find a 'Group of users list'.


4. Try to find out 'User_'.


5. If it is not there, click ADD button as shown in the image below.





6. In the popup that arrives, click Advanced, and find now.


7. You will be seeing a lis t of users under Name column.


8. Find Users name of your computer and ADD.


9. So that you can see that name in the security tab.


10. Now refresh and try again.





You are most probably done. If you have any issues pls feel free to write to me in the comments.




Good Luck!!

It is an error to use a section registered as allow defenition='Machine To Application' beyond application level.

This error can be caused by a virtual directory not being configured as an application in IIS.





This error may look strange but the solution is simple. If you manually copy a ASP.NET application as virtual directory. This error may occur due to the application is not configured with an application name.



See the image below..








Follow the below steps...



1. Open the Virtual Directory's properties you will get the popup as seen in the image.


2. Under Application Settings Click the "Create" button. You will now automatically get a application name.


3.Save the settings and try again.





This will hopefully works.


Good Luck!

Page Cannot be displayed HTTP Error 403.1 Forbidden; Execute access is denied.

This is one of the common errors when working with IIS for hosting ASP.NET web applications.


The cause for this error in many cases is due to unmatched Execute Permission.



To solve this follow the steps below...



1. Go to IIS and select your web application's virtual directory.

2. Right click and select properties.

3. On the popup select the virtual directory tab.

4.Under Application Settings find 'Execute Permissions'.

5. In the drop down select Scripts only if your are going to run any scripts in your application and so for any executable.








In my case, i selected 'Scripts Only'.




Hope you got the solution.


Good Luck.

Saturday, April 11, 2009

Input String was not in correct format; String to Int32 Conversion Problem.

There is a strange behavior of the Convert Class, when converting a string of value 0 as decimal "0.0" using the code..




   1:  private void button1_Click(object sender, EventArgs e)

   2:          {

   3:              int Result = 0;

   4:              try

   5:              {

   6:                  string inputString = textBox1.Text;

   7:                  Result = Int32.Parse(inputString);

   8:                  label3.Text = Result.ToString();

   9:              }

  10:              catch (Exception Ex)

  11:              {

  12:                  MessageBox.Show(Ex.Message);

  13:              }

  14:          }



Where the textbox value is "0.0". It returns the Exception "Input string was not in correct format". For all other values it returns the equivalent Integer value including '0'.

How to Solve this problem?

Use the below code..




   1:   private void button1_Click(object sender, EventArgs e)

   2:          {

   3:              int Result = 0;

   4:              try

   5:              {

   6:                  string inputString = textBox1.Text;

   7:                  Result = Int32.Parse(inputString,NumberStyles.AllowDecimalPoint);

   8:                  label3.Text = Result.ToString();

   9:              }

  10:              catch (Exception Ex)

  11:              {

  12:                  MessageBox.Show(Ex.Message);

  13:              }

  14:          }



here NumberStyles is as Enumerator available in System.Globalization namespace.



Caution: Pass only strings with decimal points as argument



Enjoy!