otherside Posted July 14, 2003 Posted July 14, 2003 Hello guys, When creating a user control to add it in the toolbox and use it in several programs as a dll, you can handle the events of the controls inside it when you creating the new user control. How can you handle the events from the from that you insert them , let's say You make an new windows control library project. you add two buttons in the user control and set several properties on them. now you can put handles inside this control for these buttons, click mousenter etc.. You compile the project and make the dll, then you add it to the toolbox and use it to another form. how can i handle the events of the buttons from this new form ? Q2 say i create a component which inherits from windows.forms.form.button, so i can set some properties etc and use it in several places. How can i make this component available on the toolbox, but also visible to the form designer ? thanks Quote
Leaders dynamic_sysop Posted July 14, 2003 Leaders Posted July 14, 2003 you can make a new Class project , remove the class form , then go to project, add Component. add a new Component Class. then you can add controls / functions etc... compile the new class open a new standard project right-click on the toolbox and customise toolbox click the .net framework components tab click browse , to locate your new class . dll it will be added to your toolbox , then you can add it like any other items ( such as MainMenu etc... ) here's an example of a class i quickly bunged together : in the class project : Public Class Component1 Inherits System.ComponentModel.Component #Region " Component Designer generated code " Public Sub New(Container As System.ComponentModel.IContainer) MyClass.New() 'Required for Windows.Forms Class Composition Designer support Container.Add(me) End Sub Public Sub New() MyBase.New() 'This call is required by the Component Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Component overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Component Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Component Designer 'It can be modified using the Component Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Button1 = New System.Windows.Forms.Button() ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(17, 17) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" End Sub #End Region Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("test!") End Sub End Class in your form ( once you've added the class to the toolbox ) Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Component11 As ClassLibrary3.Component1 <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container() Me.Button1 = New System.Windows.Forms.Button() Me.Component11 = New ClassLibrary3.Component1(Me.components) Me.SuspendLayout() ' 'Button1 ' Me.Button1.Name = "Button1" Me.Button1.TabIndex = 0 Me.Button1.Text = "Button1" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(544, 286) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Component11.Button1_Click(sender, e) End Sub End Class if you want to use a "user control" you can do pretty much the same. Quote
otherside Posted July 14, 2003 Author Posted July 14, 2003 dynamic_sysop thanks, i've already done that, that's not my problem, what i need is to handle the events of the buttons of the component form the new form. any ideas for Q2 Quote
*Gurus* divil Posted July 14, 2003 *Gurus* Posted July 14, 2003 You can't make them directly available. If you want to expose events of child controls, you'll have to wrap them in your own events. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
otherside Posted July 14, 2003 Author Posted July 14, 2003 thanks divil , any idea how i can do Q2 ? make a component class (by inheritance from button) viewable in the designer. Quote
*Gurus* divil Posted July 14, 2003 *Gurus* Posted July 14, 2003 I don't understand the question. If you make a class inherit from button, it'll be a control not a component. It should be visible in the toolbox if you build a control library then add it manually. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
otherside Posted July 14, 2003 Author Posted July 14, 2003 Say i make a control class Public Class BlueButton Inherits system.windows.forms.form.button public sub new() me.backcolor = color.blue end sub end class i compile it to a dll, and then add it in the toolbox, but when i add it to the form it not shown in the form as a control but like a component at the bottom, like timer .. the only way that is displayied is if i inherit System.ComponentModel.Component Quote
*Gurus* divil Posted July 14, 2003 *Gurus* Posted July 14, 2003 You should be inheriting System.Windows.Forms.Button Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
otherside Posted July 14, 2003 Author Posted July 14, 2003 sorry mistake on the thread i'm inheriting System.Windows.Forms.Button Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.