Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a component which I want to be able to reposition the form on which it is placed, when the form loads.

 

I was thinking of going about this by repositioning the form when the component is created (New method), but I need to be able to access the form in someway to set its bounds

 

If anyone could tell me how this is done I would be ever so grateful, or if anyone can think of a better way, that would be awesome, because i think resizing it on creation of the control may move the form in the designer too

Posted

The components have a property that's called: FindForm.

It retrieves the form that owns the object...

 

Other thing... if you ever want to retrieve the parent of the component (may not be the form, could be a Panel or a GroupBox for example) you can use a property that is: Parent

 

Dim mForm As Form = Me.gridContacts.FindForm

Dim obj As Panel = Me.gridContacts.Parent

Software bugs are impossible to detect by anybody except the end user.
Posted
This is a class which inherits from System.ComponentModel.Component, not from System.Windows.Forms.Control or System.Windows.Forms.UserControl, so doesn't have the properties and methods you mentioned. The only thing it has is a variable called components, which is a System.ComponentModel.IContainer
Posted

It's a way ...

 

The idea is similar as you said but don't modify things on the New...

On the New of you inherited component model pass the reference to the parent form... then you'll always have that object to modify anywhere in your componentmodel...

 

 

 

   Public Class myCM
       Inherits System.ComponentModel.Component

       Dim pForm As System.Windows.Forms.Form

       Public Sub New(ByVal parentForm As System.Windows.Forms.Form)
           pForm = parentForm
       End Sub

   End Class

Software bugs are impossible to detect by anybody except the end user.
  • *Gurus*
Posted

You can't have a constructor like that, the designers won't know what to do with it.

 

The key to getting this done is to take advantage of the designer architecture to get a reference to the form at design time, and making it set a property on your component.

 

Make yourself a public (not necessarily browsable) property on the Component of type Form. You're going to need to override the Site property, and simply call back to the base site for implementation.

 

In the Site setter after you've run the base implementation, call GetService on the site you got (assuming you got one) to get an instance of IDesignerHost. When you've got this you can use the RootComponent property on that interface to get the root component of the design surface. At this stage it's important to remember that this won't necessarily be a form - someone could put your component on the UserControl designer or even the Component designer too.

 

Once you've got this RootComponent and established it is of type Form, just assign it to your property (use IComponentChangeService to notify the design environment you've done this, but it's not the end of the world if you don't). That should sort you out - the property will now be set on loading the form, and you can do what you like with it.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted (edited)

This is what I have for property Site

 

Public Overrides Property Site() As System.ComponentModel.ISite

Get

Return MyBase.Site

End Get

Set(ByVal Value As System.ComponentModel.ISite)

Dim mySite As System.ComponentModel.ISite = MyBase.Site

Dim dh As System.ComponentModel.Design.IDesignerHost

dh = Me.GetService(mySite)

Try

pForm = DirectCast(dh.RootComponent, Form)

SetPosition()

Catch ex As Exception

MsgBox(ex.Message)

MessageBox.Show("This component is designed to be used with Windows forms", "Use with forms!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

End Try

End Set

End Property

 

and for property ParentForm

 

<System.ComponentModel.Browsable(False)> Public Property ParentForm() As System.Windows.Forms.Form

Get

Return pForm

End Get

Set(ByVal Value As System.Windows.Forms.Form)

pForm = Value

End Set

End Property

 

 

Something is wrong with it, because when I try to start the program it gives me a NullReferenceException, on dh.RootComponent, which I have investigated, and it turns out dh is null. So when you said 'assuming you got one', what do I do if i don't get one

 

Please help. Your answer sounds like the way to go, but I am clearly not skilled enough (yet), to carry out your instructions

 

Thanks so much for what you have given me so far

Edited by willbailie
Posted

Set(ByVal Value As System.ComponentModel.ISite)

MyBase.Site = Value

Dim dh As System.ComponentModel.Design.IDesignerHost

dh = Value.GetService(?)

Try

pForm = DirectCast(dh.RootComponent, Form)

SetPosition()

Catch ex As Exception

MsgBox(ex.Message)

MessageBox.Show("This component is designed to be used with Windows forms", "Use with forms!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

End Try

End Set

 

 

I don't know what to put in the ? for the type to supply as the argument

Posted

Set(ByVal Value As System.ComponentModel.ISite)

MyBase.Site = Value

Dim dh As System.ComponentModel.Design.IDesignerHost

* dh = Value.GetService(GetType(System.ComponentModel.Design.IDesignerHost))

Try

pForm = DirectCast(dh.RootComponent, Form)

SetPosition()

Catch ex As Exception

MsgBox(ex.Message)

MessageBox.Show("This component is designed to be used with Windows forms", "Use with forms!", MessageBoxButtons.OK, MessageBoxIcon.Warning)

End Try

End Set

 

On loading the form, still dh is Nothing. The form still appears, but when I close it something different comes up, it gives a NullReferenceException at the line marked *, I can only assume it is setting site to Nothing on disposal?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...