07 July 2010
Code : Strip HTML (Remove HTML Tags)
Posted by আলোর ছটা at 10:01 PM 0 comments
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.
Posted by আলোর ছটা at 9:55 PM 0 comments
Code : Download a web page
Posted by আলোর ছটা at 9:46 PM 0 comments
08 October 2009
C# : Dynamically adding control with docking works in reverse way
for (int i = 0; i < 5; i++)
{
Label l = new Label();
l.Text = "Label" + (i + 1);
l.Dock = DockStyle.Top;
panel1.Controls.Add(l);
}
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
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;
}
Posted by আলোর ছটা at 6:52 PM 0 comments
15 July 2009
Show/Hide Form Title in Taskbar in C#
The required property is Form.ShowInTaskbar.
Form2 f2 = new Form2(); // inherited Form class
f2.ShowInTaskbar = false;
f2.ShowDialog(this);
Posted by আলোর ছটা at 11:51 AM 0 comments
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.
Posted by আলোর ছটা at 1:20 PM 0 comments
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); } |
Posted by আলোর ছটা at 6:23 PM 0 comments

