Results 1 to 12 of 12

Thread: create object

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216

    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!

  2. #2
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    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!

  3. #3
    Lively Member hbandarra's Avatar
    Join Date
    Aug 2002
    Location
    Lisbon, PT
    Posts
    66
    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    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...)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    Hmmm anyone?

    snowday!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216

  8. #8
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    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

  9. #9
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    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

  11. #11
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    216
    ok, thank you so much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width