Code for moving the form by mouse clicking.

Hi Viewers,

Here by I am trying to give you a sample code for moving the win form by clicking the mouse.

Just use the following code and test it.

using System.Runtime.InteropServices;

public const int WM_NCLBUTTONDOWN = 0xA1;

public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,int Msg, int wParam, int lParam);

[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}


Now you can implement the above code in your application and test it. Working fine for me

...S.VinothkumaR.

Add prerequisites of dotnet framework in visual studio setup project

When we developing an application using any platform we should prepare our application to be deployed properly. Specially for dotnet, the prerequisites of dotnet framework is must. So here by I am explaining how to add framework to be installed before our application installation process.

After developing our .Net application we need to create a setup project. Just follow the steps mentioned in below.

- Go to File menu, select Add New Project and then select Setup And Deployment in under “Other Project Types”.

- Give a name to your project as your wish.

- Then select your setup project solution and add Primary output of your developed project. You just need to right click your setup project in solution explorer, click add --> Project Output --> select your project.

- Then add custom action by right clicking setup project --> view --> Custom Actions --> right click Custom Actions --> Add Custom Actions.

- Now we are going to add the prerequisites for your setup project.

- Select your setup project right click and go to properties then click the Prerequisites button.

- Now select .Net Framework 2.0 tick box after selecting the “Create setup program to install prerequisite components”.

- Then specify the install location for prerequisites by selecting the option box.

- Here I am selecting the second one “Download prerequisites from the same location as my application”.

- Click ok and apply then ok.

- Now build your setup project, the dotnet framework will be added in your debug folder.

That's it...

...S.VinothkumaR.

FileSystemWatcher in C#

Hi all,

Here is the simple description to use FileSystemWatcher in your desktop application by using C#.

Just drag the FileSystemWatcher control in your form from under the Components tab in the toolbox.

And copy the following code in to load event,

private void Form1_Load(object sender, EventArgs e)
{
fileSystemWatcher.Path = flvPath;
fileSystemWatcher.Filter = "*.flv";
fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite;
fileSystemWatcher.EnableRaisingEvents = true;
}

If the file watcher found any newly created file, the following event will rise.

private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " created in " + e.FullPath);
}


An event is rising when a file deleted.

private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " changed in " + e.FullPath);
}

An event is rising when a file renamed.


private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
MessageBox.Show(e.Name + " renamed in " + e.FullPath);
}

That’s it…

...S.VinothkumaR.

How to generate an xml from TreeView using C#?

Hi guys,

Here is the sample code for generating xml file from TreeView. I needed this for one of my project. I just referred lot of sites and finally came to conclusion with the following code. Just copy and paste this code and use it for yours.

using System.IO;

private static StreamWriter sr;

private void btnExportXML_Click(object sender, EventArgs e)
{
if (tvXML.Nodes.Count > 0) // Here tvXML is TreeView control
{
sr = new StreamWriter(path); // Here path is xml path
sr.Write("<" + tvXML.Nodes[0].Text + ">");
parseNode(tvXML.Nodes[0]);
sr.Close();
}
}

//Use the following method for getting value of each and every node

private static void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
if (ctn.Tag != null)
{
sr.Write("<" + ctn.Text + ">");
sr.Write(ctn.Tag);
sr.Write("");
}
else
{
sr.Write("<" + ctn.Text + "/>");
}
}
else
{
sr.Write("<" + ctn.Text + ">");
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
sr.Write("");
sr.WriteLine("");
}

...S.VinothkumaR.

How to bind Countries in a combobox using c#?

Find the code below to bind countries in a combo box using C#.


public void BindCountries()
{
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
{
RegionInfo ri = new RegionInfo(ci.LCID);
comboBox1.Items.Add(ri.EnglishName);
}
comboBox1.Sorted = true;
comboBox1.SelectedIndex = 0;
}

...S.VinothkumaR.

Using ClientScriptManager in C#

We know that we all are using the script tag for JavaScript function in our web page. In the case of some situations we need to add the scripts in code behind by dynamically. So .Net Framework 2.0 introduced a class namely “ClientScriptManager” that is used to manage script in code behind.

Now we are going to about the ClientScriptManager class and its usage with some sample codes.

ClientScriptManager:

This class is used to manage client scripts in .Net. We can get the object of this class from Page.ClientScript. We need to register the client script by using the method of RegisterClientScriptBlock () which is from that object of ClientScriptManager.

The ClientScriptManager class uniquely identifies scripts by a key String and a Type. Scripts with the same key and type are considered duplicates. Using the script type helps to avoid confusing similar scripts from different user controls that might be in use on the page.

The ClientScriptManager class can be used to invoke client callbacks in situations when it is desirable to run server code from the client without performing a postback. This is referred to as performing an out-of-band callback to the server. In a client callback, a client script function sends an asynchronous request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle to process the callback. Use the GetCallbackEventReference method to obtain a reference to a client function that, when invoked, initiates a client callback to a server event.

(This is from MSDN)

Let we watch this in Code behind. Just use the following code in page load and run it.

1. RegisterClientScriptBlock()

In Page_Load,

protected void Page_Load(object sender, EventArgs e)
{
//Get the ClientScript property as an object of ClientScriptManager from Page object.
ClientScriptManager client = this.Page.ClientScript;
//Check the script block is not already registered
if (!client.IsClientScriptBlockRegistered(this.GetType(), "Alert"))
{
client.RegisterClientScriptBlock(this.GetType(), "Alert", "alert('Welcome !')", true);

2. RegisterStartupScript()

RegisterClientScriptBlock and RegisterStartupScript both the methods are using to fire during the startup of page. But the difference is that the RegisterClientScriptBlock method will fire the script “after the form open tag but before the page controls”. And the RegisterStartupScript method will fire the script “after page controls but before the form close tag”. So the first method won’t get the controls value in run time.

For example, we are having a label box with the text “Vinoth” in a form, and then call the script as follows,

ClientScriptManager client = this.Page.ClientScript;
if (!client.IsStartupScriptRegistered(this.GetType(), "Alert"))
{
client.RegisterStartupScript(this.GetType(), "Alert", "alert(document.getElementById('lblName.Text'))", true);
}

...S.VinothkumaR.

How do I change the color of the title bar in a Win Forms .Net app using c#?

We can use the WM_NCPAINT Message to do that. We must have to code for override the WndProc method to change the color of title bar in a Win Forms. Here I have done that with the following code. You can do that by using the code below.

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDc);

[DllImport("User32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_NCPAINT = 0x85;
if (m.Msg == WM_NCPAINT)
{
IntPtr hdc = GetWindowDC(m.HWnd);
if ((int)hdc != 0)
{
Graphics g = Graphics.FromHdc(hdc);
g.FillRectangle(Brushes.Green,new Rectangle(0,0,300,22));
g.Flush();
ReleaseDC(m.HWnd, hdc);
}
}
}


That’s it…

…S.VinothkumaR.