-
Jan 25th, 2012, 05:43 PM
#1
Thread Starter
Addicted Member
Dynamic Buttons
Hi all! this one is driving me crazy!.
Got this code (assigned to a button in my form) to create a new button:
Code:
Dim btn As New ToolStripMenuItem(Me.tbTemplateName.Text)
Form1.ProgrammeTemplatesToolStripMenuItem.DropDownItems.Add(btn)
With btn
.Visible = True
End With
Form1.TS.Visible = True
The code is working fine. However, on closing my form, and re launching...the button i've added is not there. How can i store it for when i reopen my form?
Huge thanks in advance! Help and advice is great.
-
Jan 25th, 2012, 05:53 PM
#2
Re: Dynamic Buttons
use a my.settings stringcollection to store information about your buttons + add to it as you add buttons.
at startup, you can read the stringcollection + recreate your dynamic buttons
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 25th, 2012, 05:55 PM
#3
Thread Starter
Addicted Member
Re: Dynamic Buttons
Thanks Paul. Would you be able to guide me through the process?
-
Jan 25th, 2012, 06:06 PM
#4
Re: Dynamic Buttons
Project-->Properties-->Settings + add a new stringcollection setting
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.dynamicButtons Is Nothing Then
My.Settings.dynamicButtons = New Specialized.StringCollection
End If
For Each s As String In My.Settings.dynamicButtons
addDynamicButton(s)
Next
End Sub
Private Sub addDynamicButton(ByVal text As String)
'your button adding code here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add new button at runtime
addDynamicButton("button text")
My.Settings.dynamicButtons.Add("button text")
My.Settings.Save()
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 26th, 2012, 06:15 AM
#5
Thread Starter
Addicted Member
Re: Dynamic Buttons
Hi Paul! Thanks for this.
I've got a problem in that I'd like the text displayed on the button to be taken from a text box: "tbTemp"
And indeed, this seems to work at runtime. The button is added with the correct text. And it loads the data. however when i close and re open the form, i have a massive list of empty buttons and none of them seem to do anything.
Any advice?
-
Jan 26th, 2012, 06:54 AM
#6
Addicted Member
Re: Dynamic Buttons
This works for adding buttons
Code:
Public Class Form1
Dim btnx As Integer = 100
Dim btny As Integer = 100
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.dynamicButtons Is Nothing Then
My.Settings.dynamicButtons = New Specialized.StringCollection
End If
For Each s As String In My.Settings.dynamicButtons
addDynamicButton(s)
Next
End Sub
Private Sub addDynamicButton(ByVal text As String)
Dim dynamicBtn As New Button
dynamicBtn.Left = btnx
dynamicBtn.Top = btny
btnx = btnx + 20
btny = btny + 20
dynamicBtn.Name = "dbtn"
dynamicBtn.Text = "Dynamic button"
dynamicBtn.Visible = True
Me.Controls.Add(dynamicBtn)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add new button at runtime
addDynamicButton("does nothing")
My.Settings.dynamicButtons.add("Button text")
My.Settings.Save()
End Sub
End Class
-
Jan 26th, 2012, 07:12 AM
#7
Thread Starter
Addicted Member
Re: Dynamic Buttons
IKdekker, that works great...how would i transfer it to a toolstrip menu item?
-
Jan 26th, 2012, 02:39 PM
#8
Addicted Member
Re: Dynamic Buttons
Thank me, lol
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.dynamicButtons Is Nothing Then
My.Settings.dynamicButtons = New Specialized.StringCollection
End If
For Each s As String In My.Settings.dynamicButtons
addDynamicButton(s)
Next
End Sub
Private Sub addDynamicButton(ByVal text As String)
Dim dynamicBtn As New ToolStripMenuItem
ToolStripDropDownButton1.DropDownItems.Add("Your item")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add new button at runtime
addDynamicButton("does nothing")
My.Settings.dynamicButtons.Add("new")
My.Settings.Save()
End Sub
End Class
-
Jan 26th, 2012, 03:22 PM
#9
Thread Starter
Addicted Member
Re: Dynamic Buttons
Thank you very much indeed! I honestly cannot thank the people on here enough for all the help provided! It's awesome!
Cheers
-
Jan 26th, 2012, 03:28 PM
#10
Re: Dynamic Buttons
Originally Posted by ikdekker
Thank me, lol
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Settings.dynamicButtons Is Nothing Then
My.Settings.dynamicButtons = New Specialized.StringCollection
End If
For Each s As String In My.Settings.dynamicButtons
addDynamicButton(s)
Next
End Sub
Private Sub addDynamicButton(ByVal text As String)
Dim dynamicBtn As New ToolStripMenuItem
ToolStripDropDownButton1.DropDownItems.Add("Your item")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add new button at runtime
addDynamicButton("does nothing")
My.Settings.dynamicButtons.Add("new")
My.Settings.Save()
End Sub
End Class
the text parameter in addDynamicButton is there for a reason, which you have completely failed to grasp.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 27th, 2012, 05:21 AM
#11
Addicted Member
Re: Dynamic Buttons
I am not familiar with these such things so I'm not sure what it does.
Tags for this Thread
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
|