07 July 2010

Code : Strip HTML (Remove HTML Tags)

string StripHTML(string htmlString)
{

    //This pattern Matches everything found inside html tags;
    //(.|\n) - > Look for any character or a new line
    // *?  -> 0 or more occurences, and make a non-greedy search meaning
    //That the match will stop at the 1st available '>' it sees, and not at the last one
    //(if it stopped at the last one we could have overlooked
    //nested HTML tags inside a bigger HTML tag..)
   
    string pattern = @"<(.|\n)*?>";
    return Regex.Replace(htmlString, pattern, string.Empty);
   
}

Beginner's Unit Testing in C# using NUnit

C# .NET 2.0 Test Driven Development


The above website is helpful for a beginner though some steps could have been clearer.

Code : Download a web page

/// Returns the content of a given web adress as string.
///
///
URL of the webpage
/// Website content
public static string DownloadWebPage(string Url)
{
    // Open a connection
    HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);

    // You can also specify additional header values like
    // the user agent or the referer:
    WebRequestObject.UserAgent = ".NET Framework/2.0";
    WebRequestObject.Referer = "http://www.example.com/";

    // Request response:
    WebResponse Response = WebRequestObject.GetResponse();

    // Open data stream:
    Stream WebStream = Response.GetResponseStream();

    // Create reader object:
    StreamReader Reader = new StreamReader(WebStream);

    // Read the entire stream content:
    string PageContent = Reader.ReadToEnd();

    // Cleanup
    Reader.Close();
    WebStream.Close();
    Response.Close();

    return PageContent;
}

08 October 2009

C# : Dynamically adding control with docking works in reverse way

To create the list of labels (like the following image) dynamically, firstly I wrote the code below the image (very much expected).

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text = "Label" + (i + 1);
l.Dock = DockStyle.Top;
panel1.Controls.Add(l);
}


Surprisingly, I discovered that it is working just in reverse way of my expectation! (like below)


The fact is the first control is being drawn at the bottom. So, I solved it with the help of Controls.SetChildIndex() function in the following way.

Solution:

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text =
"Label" + (i + 1);
l.Dock =
DockStyle.Top;
panel1.Controls.Add(l);
panel1.Controls.SetChildIndex(l, 0);
}

Improved Solution:

In the given solution, what actually happens is that the new control is added at the top (for the style Dock.Top), then it is moved to the bottom. But this change in position may cause unwanted blinking of UI. To avoid this unwanted situation, follow the following steps

  • Add the new control with size (0,0)
  • Change its index to 0 (upper solution)
  • Resize again the control to actual size
Code:

for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text = "Label" + (i + 1);
l.Dock = DockStyle.Top;
addControlInContainer(panel1, l);
}


public static void addControlInContainer(Control container, Control control)
{
// save actual size
Size tempSize = control.Size;

// Add the new control with size (0,0)
control.Size = new Size(0, 0);
container.Controls.Add(control);

// Change its index to 0
container.Controls.SetChildIndex(control, 0);

// Resize the control to actual size
control.Size = tempSize;
}



15 July 2009

Show/Hide Form Title in Taskbar in C#

The required property is Form.ShowInTaskbar.


Sample:

Form2 f2 = new Form2(); // inherited Form class

f2.ShowInTaskbar = false;

f2.ShowDialog(this);

15 June 2009

How to disable close button of form

In a Form, you can enable/disable miximize/minimize button control easily by FormObject.MaximizeButton or FormObject.MinimizeButton property. But unfortunately, there is no such property for close button. You can disable the close button by overriding a property in your form class like below.

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams

{

get

{

CreateParams myCp = base.CreateParams;

myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;

return myCp;

}

}

Note: This procedure works for disabling the close button at the time of form loading, not after loading the form.

10 June 2009

Customized Event and Event Handler

Every .NET developer is familiar with Event and EventHandler. ‘Click’ event of a button is the most common event. Let us take a look at their usage.

Say, we have a form containing a button named ‘button1’. Then we can add a click handler with following code inside the form.


private void button1_Click(object sender, EventArgs e)

{

Console.WriteLine("button1 has been Clicked");

}

Having a careful look at the code, we find that we have to add an event handler to button object’s Click event. The event handler takes two argument sender and event argument. The event argument usually contains information related with the event.

Now, let us come to our customized event and event handler. Say, there is a person who can change his/her phone no. We want to add an event to be fired when the the person changes his/her phone no. For this, we have to do the following tasks-

1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

2. Declare prototype of event handler (2 parameters- sender and arguments)

3. Define a class Person (2 attributes- Name & PhoneNo)

4. Declare the exact name of the event- PhoneNoChanged

5. Fire the event (if any handler is added with the event) when the event actually occurs


The code is given below following exactly the upper steps. Comments is there for better understanding.


using System;

namespace CustomizedEventHandler

{

// step 1. Define our customized event arguments PhoneNoChangedEventArgs (2 attributes- OldPhoneNo & NewPhoneNo)

public class PhoneNoChangedEventArgs : EventArgs

{

public readonly string OldPhoneNo;

public readonly string NewPhoneNo;

public PhoneNoChangedEventArgs(string oldPhoneNo, string newPhoneNo)

{

this.OldPhoneNo = oldPhoneNo;

this.NewPhoneNo = newPhoneNo;

}

}

// step 2. Declare prototype of event handler (2 parameters- sender and arguments)

public delegate void PhoneNoChangedEventHandler(object sender, PhoneNoChangedEventArgs e);

// step 3. Define a class Person (2 attributes- Name & PhoneNo)

public class Person

{

private string _name;

private string _phoneNo;

// step 4. Declare the exact name of the event- PhoneNoChanged

public event PhoneNoChangedEventHandler PhoneNoChanged;

public Person(string name, string phoneNo)

{

_name = name;

_phoneNo = phoneNo;

}

public string Name

{

get { return _name; }

}

public string PhoneNo

{

get { return _phoneNo; }

set

{

//step 5. Fire the event (if any handler is added with the event) when the event actually occurs

if (_name != value) // if phone no changed

{

string previousPhoneNo = _phoneNo; ;

_phoneNo = value;

PhoneNoChangedEventArgs evArgs = new PhoneNoChangedEventArgs(previousPhoneNo, _phoneNo);

firePhoneNoChangedEvent((object)this, evArgs);

}

}

}

private void firePhoneNoChangedEvent(object sender, PhoneNoChangedEventArgs e)

{

if (PhoneNoChanged != null) // if some hadler is added with the event

PhoneNoChanged(sender, e);

}

}

}


The sample code to test the above procedure:


static void Main()

{

// create a person object with PhoneNoChangedEventHandler

Person p = new Person("Alor Chhota", "01911515020");

p.PhoneNoChanged += new PhoneNoChangedEventHandler(p_PhoneNoChanged);

// change phone no

p.PhoneNo = "01730012311";

}

static void p_PhoneNoChanged(object o, PhoneNoChangedEventArgs e)

{

Console.WriteLine("Phone no. changed from " + e.OldPhoneNo + " to " + e.NewPhoneNo);

}

 

© 2007 t!ps n tr!cks: C#



Template unik dari rohman


---[[ Skip to top ]]---