Asp.net C # transfer value and drag instance in winform window

Source: Internet
Author: User

Mousedown (object sender, mouseeventargs e)
Mouseup (object sender, mouseeventargs e)
Mousemove (object sender, mouseeventargs e)
These three functions are activated when the mouse is pressed, the mouse is released, and the mouse is moved.
Consider the following:
1. You can move the event with the mouse only when you press the mouse.
2. Adjust the position of the widget when the mouse moves
In order to effectively control the first condition, you can drag the mouse event to the mouse to press the event, instead of completing the initialization, in addition, to record the coordinates of the mouse in the control in the initial state, note that x in the mouseeventargs parameter, y represents the layout coordinates of the mouse in the current control, rather than the coordinates of the mouse in the main window.

Rivate void button#mousedown (object sender, mouseeventargs e)
{
This. tmpx = e. x;
This. tmpy = e. y;
This. button1.mousemove + = new system. windows. forms. mouseeventhandler (this. button#mousemove );
}
At the same time, bind the method out of the event with the mouse.
Private void button#mouseup (object sender, mouseeventargs e)
{
This. button1.mousemove-= new system. windows. forms. mouseeventhandler (this. button#mousemove );
}
The last is the mousemove event.
Private void button=mousemove (object sender, mouseeventargs e)
{
This. button1.location = new system. drawing. point (this. button1.location. x + e. x-this. tmpx, this. button1.location. y + e. y-this. tmpy );
}
Here, the new position of the control is recalculated.
In this example, the button is used as the experiment object and the additional definition is
Private int tmpx = 0;
Private int tmpy = 0;

 

Private int tmpx = 0;

Private int tmpy = 0;

Private void panelappsmousedown (object sender, mouseeventargs e)

{

If (e. button = mousebuttons. left)

{

This. tmpx = e. x;

This. tmpy = e. y;

This. panel1.cursor = cursors. sizeall;

This. panel1.mousemove + = new mouseeventhandler (panel=mousemove );

}

}

 

Private void panelappsmouseup (object sender, mouseeventargs e)

{

This. panel1.cursor = cursors. default;

This. panel1.mousemove-= new mouseeventhandler (panel=mousemove );

}

 

Void panelease mousemove (object sender, mouseeventargs e)

{

This. panel1.location = new point (this. panel1.location. x + e. x-this. tmpx, this. panel1.location. y + e. y-this. tmpy );

}


Read a winform, showdialog, and pass a value to the parent form as a child form.

Example:
The following is the subform code. You must enter the phone and return it to the parent form.

Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. text;
Using system. windows. forms;

Namespace windowsapplication1
{
Public partial class phone: form
{
Public phone ()
{
Initializecomponent ();
Btnok. dialogresult = dialogresult. OK;
Btnok. dialogresult = dialogresult. cancel;
}
Public string phonenumber
{
Get {return textbox1.text ;}
Set {textbox1.text = value ;}
}
Private void phone_load (object sender, eventargs e)
{

}
}
}

It does not contain any code for processing button click events. Because the dialogresult attribute of each button is set, the form disappears after you click OK or cancel. The following code shows how to call the phone dialog box in the parent form.

Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. text;
Using system. windows. forms;

Namespace windowsapplication1
{
Public partial class form7: form
{
Public form7 ()
{
Initializecomponent ();
}

Private void button#click (object sender, eventargs e)
{
Phone frm = new phone ();
Frm. showdialog ();
If (frm. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;

}
Else if (frm. dialogresult = dialogresult. cancel)
{
Label1.text = "form was canceled ";

}
Frm. close ();
}
}
}

It looks very simple. Create a new phone object frm and call frm. the showdialog method is to stop the code, wait for the phone form to return, and then check the dialogresult attribute of the phone form. Because the form has not been released and is invisible, you can still access the public attribute phonenumber, once the required data is obtained, you can use the form close method.
Everything is normal, but what if the returned format is incorrect? Put the showdialog method in the loop and you can call it again, so that the user can re-enter the method to get the correct value.

The above code is changed to the following.

Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. text;
Using system. windows. forms;

Namespace windowsapplication1
{
Public partial class form7: form
{
Public form7 ()
{
Initializecomponent ();
}

Private void button#click (object sender, eventargs e)
{
Phone frm = new phone ();

While (true)
{
Frm. showdialog ();
If (frm. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;
If (frm. phonenumber. length = 8 | frm. phonenumber. length = 12)
{
Break;
}
Else
{
Messagebox. show ("");
}
}
Else if (frm. dialogresult = dialogresult. cancel)
{
Label1.text = "form was canceled ";
Break;
}
}
Frm. close ();
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.