.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Movable form with designed header

Posted By:Nikhil Kumar       Posted Date: January 12, 2010    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

Many times I have seen that many programmer creates designable hearder for their windows forms but then donsen't have the fesility of moving like predifined windows forms, but after reading this article this is simple to create a .dll form header movable.
 


Background

Many times I have seen that many programmer creates designable hearder for their windows forms but then donsen't have the fesility of moving like predifined windows forms,  but after reading this article this is simple to create a .dll form header movable.

You can create any control movable on the form by this codeâ?¦

Okay lets start to do this___      

First step

First you have to create a Header by Windows Control Library in Visual Studio / any version.

After creating header just use this in your windows project as a usercontrol from toolbox after importing.

Interface


 

Coding

Here is the code in C#

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Movable_Form

{

    public partial class Form1 : Form

    {

        public int diff_x;

        public int diff_y;

        public bool mouse_down = false; 

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)

        {

            diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;

            diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;

            mouse_down = true; 

        }

 

        private void Form1_MouseUp(object sender, MouseEventArgs e)

        {

            mouse_down = false; 

        }

        private void button1_Click(object sender, EventArgs e)

        {

            Dispose();

            this.Close(); 

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)

        {

            if (mouse_down == true)

            {

                Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);

                Form.ActiveForm.Location = p;

            } 

        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)

        {

            diff_x = Form.MousePosition.X - Form.ActiveForm.Location.X;

            diff_y = Form.MousePosition.Y - Form.ActiveForm.Location.Y;

            mouse_down = true; 

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)

        {

           if (mouse_down == true)

            {

                Point p = new Point(MousePosition.X - diff_x, MousePosition.Y - diff_y);

                Form.ActiveForm.Location = p;

            } 

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)

        {

            mouse_down = false;

        }

    }

}

Now here you are seeing this form header is different and also movable

For further help contact me : I will be happy to help you.

 

Thanks

Nikhil Kumar


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend