Change the anchor property of a control.

M

Mr. X.

Hello.
How can I capture the event, when changing the anchor property of the
control ?

I.e :
myPanel.anchor = AnchorStyles.Left & AnchorStyles.Bottom
....
How can I know, when the Anchor property is changed (which event?) ?

Thanks :)
 
A

Armin Zingler

Am 12.06.2010 23:46, schrieb Mr. X.:
Hello.
How can I capture the event, when changing the anchor property of the
control ?

I.e :
myPanel.anchor = AnchorStyles.Left & AnchorStyles.Bottom
....
How can I know, when the Anchor property is changed (which event?) ?


Like I said, about once or twice, you should switch Option Strict On.

Then, why do you want to have an event? There is no such event, and there
is no overridable method that is called when the property changes (like
'OnAnchorChanged'). I would handle the Resize event instead. Whether the
control is anchored or not shouldn't matter.

If you set the property like above, you know where the value changes.
 
H

Herfried K. Wagner [MVP]

Am 12.06.2010 23:46, schrieb Mr. X.:
How can I capture the event, when changing the anchor property of the
control ?

There is no specific event, but you may want to take a look at the
control's 'Layout' event and 'OnLayout' method.
 
M

Mr. X.

A little problem, and I shall explain the meaning of my program :
----------------------------------------------------------------------------------
I am creating a min-ide screen (I can drag and drop on it some components,
with no events on them : only design).
1. I don't want that the components will act as a real component (there
should not be event on it, when pressing mouse-click, for example).
(disable the events, when clicking - If I knew that, it would solve many
problems).
2. I want to capture some layout events.

It may be a bad solution, but far as I have found yet is :
1. I have created an ide with handles, so I can drag and drop component,
move them, stretch them, etc.
For that I made two controls :
First one is the "visual control" (that has no parent control, but "knows"
how to draw the control (but is not paintable on screen).
Second one is an "active control", which is a pictureBox, that is paintable.
When changing some properties on the first one, such as color, text, etc. I
move the "image" from the first one to second control,
by DrawToBitmap method.
2. For some layout (only layout), because the first control is invisible
one, I capture its event (such as DockChanged, resizing and moving),
and checking the delta of width, height, etc. (For DockChanged - when this
occurred on first control, I do the same for the second one, and do some
changes on width, height, top, left, by checking the deltas).
3. Also, I am using propertyGrid control, that is connected to the first
control (named "active control").
4. Everything works fine, and good, but the only thing that I cannot handle
when anchor is changed.
Solution for that, I think :
4.1. Giving up the anchor property and hide it on property grid (how
doing that ?).
4.2. Changing the first control behavior, that it should act as a
control for design time only.

Summary (Major questions) :
==================
1. Need an elegant solution, and knowing whether my solution is bad or good.
2. How can I "disable" the events on the control (and alow only some
events), so the events won't occur when running my code (such as button
click. There are 10 different components I can drop on screen) ?
3. How can I give up and hide some properties on the propertyGrid control?

Thanks :)
 
M

Mr. X.

O.K.
Solved !!!
I have solved the above, by keeping the old anchor, and every time when the
event :
properyGrid.SelectedGridItemChanged, I am checking whether the old anchor
has changed.
If it is changed, then I move the same anchor from the first control to the
second one.
That's solved the problem, and I can keep on ...
If you have any other comments on my approach, I would like to know about.

Thanks, anyway :)
 
C

Cor Ligthert[MVP]

Why then do you set that anchor.
You can remove it completely in my idea.

In my idea is for your solution panels and docking more used.
 
M

Mr. X.

Cor Ligthert said:
Why then do you set that anchor.
You can remove it completely in my idea.

In my idea is for your solution panels and docking more used.
I am using the PropertyGrid panel, and it has anchor on it - can I hide it ?
Besides, I have overcomed the anchor problem.
I need the propertyGrid for the most of the propeties (colors, borders, etc
....).
All the layout, and position - I am doing by hands in the hard way.

Now - I try to overcome setting parent-child relations as it should be set
(panel that has some controls on it).

Thanks :)
 
O

Onur Güzel

Hello.
How can I capture the event, when changing the anchor property of the
control ?

I.e :
myPanel.anchor = AnchorStyles.Left & AnchorStyles.Bottom
...
How can I know, when the Anchor property is changed (which event?) ?

Thanks :)

Hi,

Though there's no built-in event like OnAnchorChanged, however you can
still write your own event and sync it with the original anchor
property behind the scenes. The key part is that you have to assign
anchor property to your own property like named "PropGrid1Anchor",
rather than native Control.Anchor at runtime, like in the example
below.

Place this into your existing Form class code:

'--------------------------------------------------------------------
Private Sub Button1_Click _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

' Change our custom anchor property to see it raised
Me.PropGrid1Anchor = AnchorStyles.Right _
Or AnchorStyles.Bottom

End Sub

Public Event PropGrid1AnchorChanged(ByVal sender As Object)

Public Property PropGrid1Anchor() As AnchorStyles
Get
Return Me.PropertyGrid1.Anchor
End Get
Set(ByVal value As AnchorStyles)
' It will also set built-in anchor property
Me.PropertyGrid1.Anchor = value

' Let it raise
RaiseEvent PropGrid1AnchorChanged(Me)

End Set
End Property

Public Sub PropGrid_AnchorChanged _
(ByVal sender As Object) _
Handles Me.PropGrid1AnchorChanged
' Test it
MsgBox("Raised!")
End Sub

'--------------------------------------------------------------------
Note that you're assigning anchor property to specific instance of
PropertyGrid control, that is PropertyGrid1. You can customize it in
order to your needs. There may be other thoughts, but also it can be
an option to evaluate.

HTH,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top