Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / December 2005

Tip: Looking for answers? Try searching our database.

How cancel a Copy operation?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlo (MCP only) - 22 Dec 2005 08:59 GMT
Good morning

this is the code I use in a container control designer to prevent that
another control (any) is copied directly inside him:

 Public Overloads Overrides Function CanParent(ByVal aControl As Control)
As Boolean
   Throw New Exception("Adding an item directly to the CommandBar is not
allowed.")
   Return False
 End Function

This code works, but after the exception is raised and the newly created
control is not added to the container, the newly created control is *added*
to the the parent Form and becomes unused (dead).
This means that an instance of the new control is created in the 'ctor of
the form (inside "Windows Form Designer generated code").

How to avoid this?

Thank you very much

Carlo

-------------------------------------------
Carlo, MCP (Windows Based Applications)
carlodevREMOVE@gmail.com
Mauro Servienti - 22 Dec 2005 14:29 GMT
Hi Carlo,

Il Thu, 22 Dec 2005 09:59:03 +0100 hai scritto :

> Good morning

> this is the code I use in a container control designer to prevent that
> another control (any) is copied directly inside him:

>   Public Overloads Overrides Function CanParent(ByVal aControl As
> Control) As Boolean
>     Throw New Exception("Adding an item directly to the CommandBar is
> not allowed.")
>     Return False
>   End Function

> This code works, but after the exception is raised and the newly
> created control is not added to the container, the newly created control
> is
> *added* to the the parent Form and becomes unused (dead).
> This means that an instance of the new control is created in the 'ctor
> of the form (inside "Windows Form Designer generated code").

> How to avoid this?

I have not done any test with our code but I ususally behave in this way:

a) simply return "false" in the override of CanParent method, this method is
called by the designer during drag operations (of "form's" control only) and
is used to determin if the dragged item can be dropped to the target, this
method is not called during drag operations from the "ToolBox", don't know
why but that is...

b) Create a new class inheriting from Control.ControlCollection (eg
MyControlsCollection), override the Add() method and test here if the
incoming control can be added (if true add it to the "base" class) to your
control controls collection otherwise throw an exception...
ArgumentException shuold be the best choice in this case

c) in your control override the CreateControlsInstance() protected method
and return a new instance of the newly created "MyControlsCollection" class.
CreateControlsInstance() is called once when the control need to access for
the first time his controls collection

In my scenarios these solutions works fine for cut&paste/copy&paste
operations, let us know...

> Thank you very much
> Carlo

HTH,
Mauro Servienti
whynot [ at ] topics [ dot ] it
--
"...voi abili a tenere sempre un piede qua e uno là avrete un avvenire certo
in questo mondo qua però la dignità dove l'avete persa?..."
"Vasco Rossi - Gli Spari Sopra (1993)"
Carlo (MCP only) - 23 Dec 2005 09:04 GMT
Hello Mauro.

I fear I was not enough clear explaining my problem. I'll try once more,
trying to be me more clear...

Assume you have a simple control (MyControl), with no properties or methods,
hidden to the IDE Toolbox and finally with just an attached designer. The
designer has only these lines:
Public Overloads Overrides Function CanParent(ByVal aControl As Control) As
Boolean
 Throw New Exception("Adding an item directly to the CommandBar is not
allowed.")
 Return False
End Sub

Your goal is to manage MyControl via code only, instead via the IDE
designer. To reach this, you need to prohibit to the user to Copy an
existing MyControl and Paste it again into the form or into a generic
container. In the real life, MyControl is part of another control's
collection (a Toolbar), but this is irrilevant, you only need to prevent
MyControl to be copied everywhere.
Now: if you click any MyControl on a form, Ctrl-C and then Ctrl-V, you get a
"Adding an item directly to the CommandBar is not allowed" message and your
copy is correctly aborted. But, if you are very curious and go to snoop
inside "Windows Form Designer generated code" region, you will find a
never-used instance of the newly Ctrl-C(opied) control.
This is just what I need to avoid: a dead instance of MyControl.

Please, accept my best wishes for a Merry Christmes and a very happy new
year, extended to your family!

Sincerely,

Carlo

-------------------------------------------
Carlo, MCP (Windows Based Applications)
carlodevREMOVE@gmail.com

> Hi Carlo,
>
[quoted text clipped - 53 lines]
> certo in questo mondo qua però la dignità dove l'avete persa?..."
> "Vasco Rossi - Gli Spari Sopra (1993)"
Carlo (MCP only) - 23 Dec 2005 09:14 GMT
Sorry, previous post was wrong. The following is correct:
-----------------------------------------

Hello Mauro.

I fear I was not enough clear explaining my problem. I'll try once more,
trying to be me more clear...

Assume you have a simple control (MyControl), with no properties or methods,
hidden to the IDE Toolbox and a very simple container with just an attached
designer. The container's designer has only these lines:
Public Overloads Overrides Function CanParent(ByVal aControl As Control) As
Boolean
 Throw New Exception("Adding an item directly to the CommandBar is not
allowed.")
 Return False
End Sub

Your goal is to manage MyControl via code only, instead that via the IDE
designer. To reach this, you need to prohibit to the user to Copy an
existing MyControl and Paste it again into the container. In the real life,
MyControl is part of container's controls collection (a Toolbar), but this
is irrilevant, you only need to prevent a standalone instance of MyControl
to be copied to the container.
Now: if you click any MyControl on a container, Ctrl-C and then Ctrl-V, you
get a "Adding an item directly to the CommandBar is not allowed" message and
your copy is correctly aborted. But, if you are very curious and go to snoop
inside "Windows Form Designer generated code" region, you will find a
never-used instance of the newly Ctrl-C(opied) control.
This is just what I need to avoid: a dead instance of MyControl.

Please, accept my best wishes for a Merry Christmas and a very happy new
year, extended to your family!

Sincerely,

Carlo

-------------------------------------------
Carlo, MCP (Windows Based Applications)
carlodevREMOVE@gmail.com

> Hi Carlo,
>
[quoted text clipped - 53 lines]
> certo in questo mondo qua però la dignità dove l'avete persa?..."
> "Vasco Rossi - Gli Spari Sopra (1993)"

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.