Jun282010

readonly CheckBox or CheckBoxList

Published by Pete Celliers at 12:45 PM under c# | CSS | Javascript

Simply add the following to your declaration:
onclick="return false;" 

This will disable the javascript that ticks / unticks the box.

Examples of both readonly CheckBoxLists and readonly CheckBoxListes:

<asp:CheckBoxList ID="CheckBoxList1" runat="server" onclick="return false;">
<asp:CheckBox ID="CheckBox1" runat="server" onclick="return false;" Text="test" />

 

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

May182010

regular expressions in c#

Published by Pete Celliers at 11:33 AM under c# | regular expressions

Characters

identifier

definition

\a

Alert, x07.

\b

Backspace, x08.

\e

ESC, x1B.

\n

Newline, x0A.

\r

Carriage return, x0D.

\f

Form feed, x0C.

\t

Tab, x09.

\v

Vertical tab, x0B.

\0octal

Two-digit octal character code.

\xhex

Two-digit hexadecimal character code.

\uhex

Four-digit hexadecimal character code.

\cchar

Named control character.

Classes

identifier

definition

[...]

Single character from the listed range.

[^...]

Single character not from the listed range.

.

Any single character except for a line terminator (unless in single-line mode - s).

\w

Word character such as [a-zA-Z_0-9]

\W

Non-word character - basically [^a-zA-Z_0-9]

\d

Digit [0-9]

\D

Non-digit [^0-9]

\s

Whitespace character such as [ \f\n\r\t\v]

\S

Non-whitespace character

\p{prop}

Character that is contained in the specified Unicode block / property.

\P{prop}

Character that is not contained in the specified Unicode block / property.

Tests etc

identifier

definition

^

Start of a string, or following a newline in MULTILINE mode.

\A

Beginning of string, in all match modes.

$

End of string or before any newline if in MULTILINE mode.

\Z

End of string but before any final line terminator, in all match modes.

\z

End of string in all match modes.

\b

Boundary between a \w character and a \W character.

\B

Not-word-boundary.

\G

End of the previous match.

(?=...)

Positive lookahead.

(?!...)

Negative lookahead.

(?<=...)

Positive lookbehind.

(?<!...)

Negative lookbehind.

mode modifiers

mode

identifier

definition

Singleline

s

Dot (.) matches any character, including a line terminator.

Multiline

m

^ and $ match next to embedded line terminators.

IgnorePatternWhitespace

x

Ignore whitespace and allow embedded comments starting with #.

IgnoreCase

i

Case-insensitive match based on characters in the current culture.

CultureInvariant

i

Culture-insensitive match.

ExplicitCapture

n

Allow named capture groups, but treat parentheses as non-capturing groups.

Compiled

 

Compile regular expression.

RightToLeft

 

Search from right to left, starting to the left of the start position.

ECMAScript

 

Enables ECMAScript compliance when used with IgnoreCase or Multiline.

(?imnsx-imnsx)

 

Turn match flags on or off for rest of pattern.

(?imnsx-imnsx:...)

 

Turn match flags on or off for the rest of the subexpression.

(?#...)

 

Treat substring as a comment.

#...

 

Treat rest of line as a comment in /x mode.

groups and repititions

identifier

definition

(...)

Grouping. Submatches fill \1,\2,... and $1, $2,....

\n

In a regular expression, match what was matched by the nth earlier submatch.

$n

In a replacement string, contains the nth earlier submatch.

(?<name>...)

Captures matched substring into group, name.

(?:...)

Grouping-only parentheses, no capturing.

(?>...)

Disallow backtracking for subpattern.

...|...

Alternation; match one or the other.

*

Repeated 0 or more times.

+

Repeated 1 or more times.

?

Repeated 1 or 0 times.

{n}

Repeated exactly n times.

{n,}

Repeated at least n times.

{x,y}

Repeated at least x times, but no more than y times.

*?

Repeated 0 or more times, but as few times as possible.

+?

Repeated 1 or more times, but as few times as possible.

??

Repeated 0 or 1 times, but as few times as possible.

{n,}?

Repeated at least n times, but as few times as possible.

{x,y}?

Repeated at least x times, no more than y times, but as few times as possible.

Replacements

identifier

definition

$1, $2, ...

The captured submatches.

${name}

The matched text for a named capture group.

$'

Text before match.

$&

Text of match.

$'

Text after match.

$+

Last parenthesized match.

$_

Original input string.

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

May162010

Right Click and Auto Row Select with a ContextMenuStrip

Published by Pete Celliers at 12:31 PM under c#

If you have a datagridview and want to be able to right-click, which should automatically selecte the row that you've clicked and fire the ContextMenuStrip, it can be done as follows:

 

private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        foreach (DataGridViewRow s in dataGridView1.SelectedRows)
            s.Selected = false;
 
        DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
        dataGridView1.Rows[hit.RowIndex].Selected = true;
        dataGridView1.CurrentCell= dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
        contextMenuStrip1.Show(dataGridView1, new Point(e.X,e.Y) );
    }
}


[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Apr222010

app settings in web.config

Published by Pete Celliers at 7:05 PM under c#

 In web.config:

<appSettings>
    <add key="ContactUsEmail" value="me@mysite.com"/>
</appSettings>

In your code:

using System.Configuration;

        private void SendEmail(string emailbody)
        {
                string V = (string)ConfigurationSettings.AppSettings["ConnectionInfo"];
        }

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Apr192010

asp net maintain scroll position on postback

Published by Pete Celliers at 4:04 PM under c#

either in you web.config file you can add the following entry:

<system.web>
      <pages theme="standard" maintainScrollPositionOnPostBack="true">
</system.web>
 

or for any page you can set the page directive as follow:

<%@ Page MaintainScrollPositionOnPostback="true" .... 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Apr152010

user agent

Published by Pete Celliers at 6:35 PM under c#

The following line will provide the useragent from the person accesing your web page

 HttpContext.Current.Request.Headers["User-Agent"]

 it would look similar to the following:

useragent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.9) Gecko/20100315 Firefox/3.5.9 GTBDFff GTB7.0 (.NET CLR 3.5.30729)

 

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Apr012010

free autologin web control for automatic login during development purposes

Published by Pete Celliers at 11:55 PM under c#

When we are in the process of development it could be quite a pain to login hundreds of times during the day

The following control would ease this process for you

1) download the dll

2) copy the dll into your bin directory

3) drag and drop the Dll onto your toolbox (when viewing a aspx file, press ctrl-alt-X)

4) Then drag and drop the control from the toolbox onto your login page (where you want to be able to quickly login by selecting a user)

Download the dll here

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 2 Responses

Mar022010

create a folder if not exists

Published by Pete Celliers at 5:21 PM under c#

    string filename = @"c:\here\there\where\myname.txt";

    string dir = Path.GetDirectoryName(filename);
    if (!System.IO.Directory.Exists(dir))
        System.IO.Directory.CreateDirectory(dir);

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Mar022010

running shell commands from within c#

Published by Pete Celliers at 1:45 PM under c#

run shell commands:

 

        private static void RunCommand(string cmdname)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = cmdname;
            p.Start();
        }

 

call it with the following command:

            RunCommand("cmd");  // opens a shell command prompt window



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Mar012010

working with local databases in asp.net

Published by Pete Celliers at 5:56 PM under c# | SQL SERVER

make sure to copy the database in the APP_Folder

use the following connection string as template:

<add name="GuestbookConnectionString1" connectionString="Data Source=.\SQLEXPRESS; AttachDBFilename=|DataDirectory|\Guestbook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
   providerName="System.Data.SqlClient" />

 

 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Mar012010

Sandcastle simplified (building CHM help files for .Net documentation)

Published by Pete Celliers at 4:38 PM under c#

step 1: download sandcastle from one of these 2 locations:

http://sandcastle.codeplex.com/releases/view/13873
http://www.microsoft.com/downloads/details.aspx?familyid=E82EA71D-DA89-42EE-A715-696E3A4873B2&displaylang=en

step 2: Do a straightforward installation

step 3: in visual studio, right-click on your project -> properties -> build -> scroll down and check "XML documentation file"

step 4: build your project

step 5: Run SandcastleGUI, find this under:
C:\Program Files\Sandcastle\Examples\generic 
C:\Program Files (x86)\Sandcastle\Examples\generic    (or for 64bit apps)

step 6: Add your assembly (the dll file which is normally in your application's "bin\debug" folder)

step 7: Also add the XML file which should normally be in your application's "bin\debug" folder)

step 8: Pick any name for your project (I picked "MyTestProject")

step 9: Click "build"

step 10: Locate your project under: (simply just find it that you know where it is)
C:\Program Files\Sandcastle\Examples
C:\Program Files (x86)\Sandcastle\Examples   (64bit)

step 11: Open a command prompt by:
click "Start" -> "run"  and type "cmd" <enter>

step 12: change your directory to where your product output reside:
(e.g. "cd  C:\Program Files\Sandcastle\Examples\MyTestProject\vs2005\chm" )
(where the "MyTestProject.hhc" file is located in my case)

step 13:  Convert the file:
"C:\Program Files\HTML Help Workshop\hhc.exe"  MyTestProject
"C:\Program Files (x86)\HTML Help Workshop\hhc.exe"  MyTestProject    (64 bit)

step 14: you should now have a MyTestProject.chm file



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Jan052010

Simple File Read to String and Write String To File C# Methods

Published by Pete Celliers at 1:20 AM under c#

using System.IO;

        public static string LoadFromFile(string filename)
        {
            StreamReader streamReader = new StreamReader(filename);
            string text = streamReader.ReadToEnd();
            streamReader.Close();
            return text;
        }

 

 

        public static void WriteToFile(string filename, string text)
        {
            StreamWriter streamWriter = new StreamWriter(File.Create(filename));
            streamWriter.Write(text);
            streamWriter.Close();
        }
 



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Nov252009

List.Sort made simple

Published by Pete Celliers at 1:14 PM under c# | Generics

This is a simple example of how to sort a Generic List    List(T)

 

using System;
using System.Collections.Generic;
 
public class MySortExample
{
    private static int CompareVehiclesByWeight(Vehicle x, Vehicle y)
    {
        if ((x == null) && (y == null))
             return 0;
        if ((x == null) && (y != null))
             return -1;
        if ((x != null) && (y == null))
             return 1;
        return (x.Weight.CompareTo(y.Weight));
    }
 
    public static void Main()
    {
        List<Vehicle> Vehicles = new List<Vehicle>();
 
        Console.WriteLine("\nCapacity: {0}", Vehicles.Capacity);
 
        Vehicles.Add(VehicleType.Car);
        Vehicles.Add(VehicleType.Bicycle);
        Vehicles.Add(VehicleType.Plane);
        Vehicles.Add(VehicleType.Ship);
        Vehicles.Add(VehicleType.Motorbike);
 
        Vehicles.Sort(CompareVehiclesByWeight);
    }
} 

You can created multiple sorting algorithms, sorting on different elements/fields and then just call the sort method that you want to use at the appropriate time.



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Nov252009

c# operators

Published by Pete Celliers at 12:15 AM under c#

Operator category Operators
Arithmetic +   -   *   /   %
Logical (boolean and bitwise) &   |   ^   !   ~   &&   ||   true   false
String concatenation +
Increment, decrement ++   --
Shift <<   >>
Relational ==   !=   <   >   <=   >=
Assignment =   +=   -=   *=   /=   %=   &=   |=   ^=   <<=   >>=
Member access .
Indexing []
Cast ()
Conditional ?:
Delegate concatenation and removal +   -
Object creation new
Type information as   is   sizeof   typeof   
Overflow exception control checked   unchecked
Indirection and Address *   ->   []   &


[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses

Nov252009

String.Format

Published by Pete Celliers at 12:11 AM under c#

Character Description Examples Output
C or c Currency Console.Write("{0:C}", 2.5);

Console.Write("{0:C}", -2.5);

$2.50

($2.50)

D or d Decimal Console.Write("{0:D5}", 25); 00025
E or e Scientific Console.Write("{0:E}", 250000); 2.500000E+005
F or f Fixed-point Console.Write("{0:F2}", 25);

Console.Write("{0:F0}", 25);

25.00

25

G or g General Console.Write("{0:G}", 2.5); 2.5
N or n Number Console.Write("{0:N}", 2500000); 2,500,000.00
X or x Hexadecimal Console.Write("{0:X}", 250);

Console.Write("{0:X}", 0xffff);

FA

FFFF

 

in a similar manner we can format dates:

        //output: Apr 21, 2010
        string s = DateTime.Now.ToString("MMM dd, yyyy");
       
        //output: 2010/Apr/21
        string s = DateTime.Now.ToString("yyyy/MMM/dd");
       
        //output: 2010/04/21 .... 18/00/23/06
        string s = DateTime.Now.ToString("yyyy/MM/dd .... HH/mm/ss/hh");
       
        //output: 20100421
        string s = string.Format("{0:yyyyMMdd}",DateTime.Now);
        



[KickIt] [Digg] [del.icio.us] [Facebook] [Google] [StumbleUpon]

Tags:

E-mail | Permalink | Post RSSRSS comment feed 0 Responses