|
-
Feb 5th, 2003, 08:20 PM
#1
Thread Starter
Addicted Member
create object
I want to make it so that when the user of my program clicks a button on the tooldbar, it adds a button (or whatever i want it to be, label, panel, textbox, etc) but i dont know how to make it so that it will create a button or w/e in the panel box when i press that button on the toolbar. Thanks!
-
Feb 6th, 2003, 11:45 AM
#2
Lively Member
Insert components using the developer studio tools, look at the code it generates and try to learn with that.
Essentialy, you need to know about event handling and understand how .net components work.
There are several sites with good information about that.
Good luck!
-
Feb 6th, 2003, 12:29 PM
#3
Lively Member
Well...
Code:
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 ToolBar1 As System.Windows.Forms.ToolBar
Friend WithEvents ToolBarButton_NewToolBarButton As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton_AddFormButton As System.Windows.Forms.ToolBarButton
Friend WithEvents Button_Hidden As System.Windows.Forms.Button
Friend WithEvents ToolBarButton_Toggle As System.Windows.Forms.ToolBarButton
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ToolBar1 = New System.Windows.Forms.ToolBar()
Me.ToolBarButton_NewToolBarButton = New System.Windows.Forms.ToolBarButton()
Me.ToolBarButton_AddFormButton = New System.Windows.Forms.ToolBarButton()
Me.Button_Hidden = New System.Windows.Forms.Button()
Me.ToolBarButton_Toggle = New System.Windows.Forms.ToolBarButton()
Me.SuspendLayout()
'
'ToolBar1
'
Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton_NewToolBarButton, Me.ToolBarButton_Toggle, Me.ToolBarButton_AddFormButton})
Me.ToolBar1.DropDownArrows = True
Me.ToolBar1.Name = "ToolBar1"
Me.ToolBar1.ShowToolTips = True
Me.ToolBar1.Size = New System.Drawing.Size(440, 39)
Me.ToolBar1.TabIndex = 0
'
'ToolBarButton_NewToolBarButton
'
Me.ToolBarButton_NewToolBarButton.Text = "Add toolbar button"
'
'ToolBarButton_AddFormButton
'
Me.ToolBarButton_AddFormButton.Text = "Add form buttom"
'
'Button_Hidden
'
Me.Button_Hidden.Location = New System.Drawing.Point(8, 48)
Me.Button_Hidden.Name = "Button_Hidden"
Me.Button_Hidden.Size = New System.Drawing.Size(88, 23)
Me.Button_Hidden.TabIndex = 1
Me.Button_Hidden.Text = "Hidden button"
Me.Button_Hidden.Visible = False
'
'ToolBarButton_Toggle
'
Me.ToolBarButton_Toggle.Text = "Toggle hiden button"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(440, 221)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button_Hidden, Me.ToolBar1})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick
Select Case ToolBar1.Buttons.IndexOf(e.Button)
Case 0
Dim NewToolbarButton As New System.Windows.Forms.ToolBarButton()
NewToolbarButton.Text = "New!"
Me.ToolBar1.Buttons.Add(NewToolbarButton)
Case 1
If Me.Button_Hidden.Visible = True Then
Me.Button_Hidden.Visible = False
Else
Me.Button_Hidden.Visible = True
End If
Case 2
Dim NewButton As New System.Windows.Forms.Button()
Dim R As New Random()
NewButton.Location = New System.Drawing.Point(R.Next(0, Me.Width), R.Next(0, Me.Height))
NewButton.Size = New System.Drawing.Size(40, 23)
NewButton.Text = "New1"
Me.Controls.Add(NewButton)
Case Else
MessageBox.Show("I'm a new toolbar button!")
End Select
End Sub
Private Sub Button_Hidden_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Hidden.Click
MessageBox.Show("I'm visible, now!")
End Sub
End Class
Note that handling events for dinamic generated buttons is more complex, so you is more simple for you if you use the visible property of the components.
-
Feb 6th, 2003, 03:36 PM
#4
Thread Starter
Addicted Member
thank you that will help me later but i wanted to know how to create a new button like the ones that you click "ok" on, so it would appear not on the toolbar but on the for space below it.
-
Feb 6th, 2003, 03:39 PM
#5
Thread Starter
Addicted Member
in other words it would be like the person is trying to re-arrange the interface of the prog. (like having the exit button to the right instead of left...)
-
Feb 7th, 2003, 10:39 AM
#6
Thread Starter
Addicted Member
Hmmm anyone?
snowday!
-
Feb 7th, 2003, 05:43 PM
#7
Thread Starter
Addicted Member
-
Feb 7th, 2003, 07:33 PM
#8
Addicted Member
Is this what you want?
Code:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim x As System.Windows.Forms.Button
x = New System.Windows.Forms.Button()
x.Text = "New Button"
x.Left = 20
x.Top = 20
Me.Controls.Add(x)
End Sub
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Feb 7th, 2003, 08:12 PM
#9
Addicted Member
I just figured this part out, but here is how to connect it to a sub:
Code:
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
Dim x As System.Windows.Forms.Button
x = New System.Windows.Forms.Button()
x.Text = "New Button"
x.Left = 20
x.Top = 120
AddHandler x.Click, AddressOf cmd_Click
Me.Controls.Add(x)
End Sub
Private Sub cmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Works!")
End Sub
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Feb 8th, 2003, 10:57 AM
#10
Thread Starter
Addicted Member
Thank you that works, but how can i make it so that if he clickes it again another new button will appear? also how can i make it resizable and relocationable?
Thanks so much
-
Feb 9th, 2003, 04:24 AM
#11
Addicted Member
Every time you user presses it a new one does appear, it's just that each one has the same Top, and Left Posisitons so you can't see the others.
Drag And Drop to move the button is easy, making it resizeable is not impossible, but a little more difficult. I suggest having a pop-up window that allows you user to adjust that, but If you What it so he just clicks on the sides and you get the little cross hairs with the mouse I suggest you use the "Panel" Control. Put a small Panel on each side of the button, set each of the "Cursor" properties to the proper cursor, and handing the resizing through the Mouse Down and Mouse Up properties. (I know it can be done, but I'm not going to tackle that.) Here is a simple way to drag and drop the button around:
Code:
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
e.Effect = DragDropEffects.Move
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
Button1.Left = e.X - Form1.ActiveForm.Location.X - 4
Button1.Top = e.Y - Form1.ActiveForm.Location.Y - 30
End Sub
Private Sub Button1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Button1.DoDragDrop(Button1.Text, DragDropEffects.Move)
End Sub
-Daryl
"Two More Rolls of Duct tape, and the world is mine!"
VB.NET Guru
-
Feb 9th, 2003, 08:37 AM
#12
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|