Runtime Editable Control - The Code Project -...

来源:百度文库 编辑:神马文学网 时间:2024/06/28 10:51:52

3,672,741 members and growing!   9,311 now online.txjchen |My Settings |My Bookmarks |My Articles |Sign out
HomeMFC/C++C#ASP.NET.NETVB.NETAll Topics  Help!ArticlesMessage BoardsStoreFrontLounge
All Topics,C#,.NET >>C# Controls >>General
Runtime Editable Control
ByMiljenko Cvjetko.
Inheritable (super class) UserForm that enables runtime moving of controls (editing).
C#
Windows, .NET (.NET 1.0, .NET 1.1, .NET 2.0)
Win32, VS (VS2005), WinForms
Dev
Posted: 6 Dec 2004
Views: 23,485
Announcements
$10,000 worth of prizes to be wonMonthly Competition

Search   Articles Authors   Advanced Search
Sitemap |Add to IE Search
PrintBroken Article?BookmarkDiscussSend to a friend
9 votes for this article.
Popularity: 3.82. Rating: 4 out of 5.
You are signed up for one or morenewsletters but unfortunately we are unable to send you emails. Please clickhere to have an email sent that will allow us to confirm your email address.
Download demo exe - 7.26 KbDownload source + project - 9.23 Kb
Note
This is a simple example developed with VS.NET 2005 Beta 1 codename Whidbey. So, project and solution are not usable with previous versions, while code should run in previous versions without problems.
Introduction and background
Faced with and, as the matter of fact, astonished by user requirement specification for one project, where one of the requirements were that controls (some or all) on the form must be movable (runtime editable in narrower sense).
So after "brainstorming", the decision was made to make a framework to implement a UserControl for runtime movable controls called EditableControl. EditableControl should be super class for any custom made control with arbitrary number and type of controls.
The idea is to allow users to adjust his/hers look and feel through positions of controls, whereby business logic behind the control stays the same (actually, user is not able to change anything except presentation).
The control should be reusable, so it inherits from UserControl, and any other business logic control that inherits from EditableControl will be editable too.
Original control

Control after runtime "editing"

Design
EditableControl has four member variables for layout editing (moving): points for the origin of the moved control, destination, reference to control being moved, and an ArrayList of controls that can be moved (for other controls).
/// /// Point where from Control will be moved /// private Point _point_begin ; /// /// Point where to Control will be moved /// private Point _point_end ; /// /// Moved Control /// private Control _control_moving = null ; /// /// Container for defining set of movable/editable Controls /// public System.Collections.ArrayList EditableControls ;
To accomplish drag&drop of the control, three mouse events must be handled by containing (hosting control, parent) as well as by every contained (child) control. Those events are: MouseDown to detect selection of the control, MouseMove to animate movement, and MouseUp to detect destination point. Actually, only two events are needed: MouseDown and MouseUp, but animation can‘t hurt. Initialization hooks events to all contained child controls.
/// /// initialize member function hooks mouse events of all child /// controls to mouse event handlers of the EditableControl /// protected virtual void initialize ( ) { foreach (Control ctrl in Controls) { ctrl.MouseDown += new MouseEventHandler(MouseDownEventHandler); ctrl.MouseUp += new MouseEventHandler(MouseUpEventHandler); ctrl.MouseMove += new MouseEventHandler(MouseMoveEventHandler); } return; }
And events which are handled for each movable control and do not interfere with control‘s behavior (events and logic):
Collapse
/// /// MouseDownEventHandler detects whether child control was hit and /// sets the starting point for the move of the child control /// /// /// /// /// protected virtual void MouseDownEventHandler ( object sender , MouseEventArgs evnt_args_mouse ) { int x; int y; if ( // while left button pressed (could define other keys) evnt_args_mouse.Button == MouseButtons.Left && // ignore containing (parent) control ! "Core.Windows.Forms.EditableControl" .Equals(sender.GetType().ToString()) && // control is defined as movable EditableControls.Contains(sender) ) { Point pt; pt = Cursor.Position; x_offset_on_client_control = evnt_args_mouse.X; y_offset_on_client_control = evnt_args_mouse.Y; x = x_offset_on_client_control + ((Control)sender).Location.X; y = y_offset_on_client_control + ((Control)sender).Location.Y; pt = new Point(x, y); _point_begin = pt; } foreach (Control ctrl in Controls) { if (ctrl.Bounds.Contains(_point_begin)) { _control_moving = ctrl; } } return; } /// /// MouseMoveEventHandler moves child control on the screen /// /// /// /// /// protected virtual void MouseMoveEventHandler ( object sender , MouseEventArgs evnt_args_mouse ) { if ( ! "Core.Windows.Forms.EditableControl" .Equals(sender.GetType().ToString()) && _control_moving != null && evnt_args_mouse.Button == MouseButtons.Left ) { Point pt = Cursor.Position; _control_moving.Left = (this.PointToClient(pt)).X - x_offset_on_client_control ; _control_moving.Top = (this.PointToClient(pt)).Y - y_offset_on_client_control ; } } /// /// MouseUpEventHandler detects when and where child control released /// and sets the ending point for the move of the child control /// /// /// /// /// protected virtual void MouseUpEventHandler ( object sender , MouseEventArgs evnt_args_mouse ) { if ( _control_moving != null && evnt_args_mouse.Button == MouseButtons.Left ) { int x; int y; x = ((Control)sender).Location.X; y = ((Control)sender).Location.Y; Point pt = new Point(x, y); _point_end = pt; _control_moving.Location = _point_end; _control_moving = null; } return; } Using the code
To make any control (let‘s say SomeUserControl) runtime editable, inherit from EditableControl.
/// /// UserControl with runtime editable layout extends EditableControl /// public partial class SomeUserControl : Core.Windows.Forms.EditableControl { public SomeUserControl() :base() { InitializeComponent(); // define movable controls base.EditableControls.Add(this.textBox1); base.EditableControls.Add(this.dataGridView1); base.EditableControls.Add(this.checkBox1); base.EditableControls.Add(this.checkedListBox1); base.EditableControls.Add(this.button1); // prepare EditableControl base.initialize(); } } TODOs
Resizable runtime controls Keys to control selection of moving control (Alt, Ctrl)
History
First version by moljac++ - 2004.12.03.
Miljenko Cvjetko
Clickhere to view Miljenko Cvjetko‘s online profile.
Other popular C# Controls articles:
Themed Windows XP style Explorer Bar A fully customizable Windows XP style Explorer Bar that supports Windows XP themes and animated expand/collapse with transparency.
XPTable - .NET ListView meets Java‘s JTable A fully customisable ListView style control based on Java‘s JTable.
SourceGrid - Open Source C# Grid Control SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
TaskbarNotifier, a skinnable MSN Messenger-like popup in C# and now in VB.NET too The TaskbarNotifier class allows to display an MSN Messenger-like animated popup with a skinned background
[Top] Rate this Article for us!     PoorExcellent


FAQ  Message score threshold 1.0 2.0 3.0 4.0 5.0    Search comments
View Normal (slow) Preview (slow) Message View Topic View Thread View Expanded (Supporters only)    Per page 10 25 50
New Message Msgs 1 to 4 of 4 (Total: 4) (Refresh) First Prev Next
Subject  Author  Date

 Can you favour me a little  Nhilesh B  7:22 5 Jan ‘06
  Hello,
I am really amazed with you code, but while I tried to implement the code in my application it is not applying, and I can not run the sample as I do not have dotnet 2.0 installed with me.
Can you please send me a dotnet 1.1 version source code?
Thanks in advance.
Regards
Nhilesh Baua
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse


 Tailor Your Application by Building a Custom Forms Designer with .NET (MSDN Article)  Marc Sommer  5:52 6 Dec ‘04
  Tailor Your Application by Building a Custom Forms Designer with .NET
http://msdn.microsoft.com/msdnmag/issues/04/12/CustomFormsDesigner/default.aspx[^]
This article discusses:
Design-time environment fundamentals
Forms designer architecture
The implementation of the forms designer in Visual Studio .NET
Services you need to implement to write a forms designer for your own application
Code download available at:
http://download.microsoft.com/download/d/3/1/d31fff33-fd97-488f-9bbd-4b7402905716/CustomFormsDesigner.exe[^]
And a commercial componment in beta stage at the moment.
seems to be the only runtime form designer component available as for the moment.
Unfortunately commercial.
Form Designer .NET
Runtime Form Designer Control
for C#, VB.NET and Delphi 8
version 1.0 beta 2, November 29, 2004
Form Designer .NET allows you move and resize any control on your .NET application form at runtime. You need not prepare your form to use Form Designer .NET. Just drop Form Designer .NET control onto any form, assign the DesignedForm property, set Active property to true and enjoy!
http://www.greatis.com/dotnet/formdes/[^]
Demo (Beta2):
http://www.greatis.com/fdnetdemo.zip[^]
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse


 Other solution  Robert Rohde  1:15 6 Dec ‘04
  Take a look at:
http://www.codeproject.com/cs/miscctrl/CSPickBoxSample1.asp?target=pickbox
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse

 Re: Other solution  TNTVN  4:02 17 Feb ‘05
  one of a requirement is that make run-time controls look like at design time: resize by dragging around "rectangle" ... Check another solution at:
http://www.windowsforms.com/Forums/ShowPost.aspx?tabIndex=1&tabId=41&PostID=12082#17945[^]
wdyl
[Reply |Email |View Thread |Get Link] Rate this message:12345 (out of 5)
Report asSpam orAbuse

Last Visit: 11:20 Tuesday 2nd January, 2007 First Prev Next
General comment   News / Info   Question   Answer   Joke / Game   Admin message
Updated: 6 Dec 2004 Article content copyright Miljenko Cvjetko, 2004
everything else Copyright ©CodeProject, 1999-2006.
Web17 |Advertise on The Code Project |Privacy

The Ultimate Toolbox •ASP Alliance •Developer Fusion •Developersdex •DevGuru •Programmers Heaven •Planet Source Code •Tek-Tips Forums •
Help!
Articles
Message Boards
StoreFront
Lounge
What is ‘The Code Project‘?
General FAQ
Post a Question
Site Directory
About Us
Latest
Most Popular
Search
Site Directory
Submit an Article
Update an Article
Article Competition
Windows Vista
Visual C++
ATL / WTL / STL
COM
C++/CLI
C#
ASP.NET
VB.NET
Web Development
.NET Framework
Mobile Development
SQL / ADO / ADO.NET
XML / XSL
OS / SysAdmin
Work Issues
Article Requests
Collaboration
General Discussions
Hardware
Algorithms / Math
Design and Architecture
Subtle Bugs
Suggestions
The Soapbox