保存动态创建的按钮 [英] Save a dynamically created button
问题描述
结果
当我点击添加,我就可以任意位置单击,并在运行时添加按钮(点击我)。结果
我怎么可以保存按钮,以便下一次我运行应用程序时,这4会出现在那里?另外,希望每个按钮有不同的事件,但是这不是我的首要任务是现在。结果
谁能给一个办法吗?
When I click add, I will be able to click anywhere, and add button (Click Me) at run time.
How can I save the button so that next time I run the app, those 4 will appear there? Also, would want each of the button to have different events but that's not my priority as of now.
Can anyone give a way?
推荐答案
你真的不能保存按钮,但你可以保存,让您创建一个新的按钮出现在相同的位置的信息。一种方式是设置 - 只保存每一个的大小和位置。另一种方法是序列:
you really cant save a button, but you can save the information that allows you to create a new button to appear in the same location. One way might be Settings - just save the Size and Location of each. Another way is serialization:
Friend _ctrls As List(Of ControlSettings)
<Serializable>
Public Class ControlSettings
Public Property ctrlLocation As Point
Public Property CtrlSize as Size
' likely other things:
Public Property CameraID As Integer
Public Property CtrlText As String
... etc
End Class
要在表单按钮保存设置,收获所有相关的道具:
To save settings, harvest all the relevant props from the form buttons:
Dim cs As New ControlSettings
With cs
.ctrlLocation = thisButton.Location
.CtrlSize = thisButton.Size
' etc
End With
_ctrls.Add(cs)
当你完成后, _ctrls
将所有所有按钮的信息。接下来,序列化:
When you are done, _ctrls
will have all the info for all the buttons. Next, serialize it:
Dim mUserFile As String = UsersFolder_Location_And_Name
Dim fs As New FileStream(mUserFile, FileMode.Create, FileAccess.Write)
Serializer.Serialize(fs, _ctrls)
fs.Close()
fs.Dispose()
下一次的应用程序运行,你的价值观加载顺序相反的信息,并创建新的按钮:
Next time the app runs, you load the info in reverse order, and create new buttons from the values:
Dim Btn as New Button
btn.Location = _ctrls(n).ctrlLocation
btn.Size = _ctrls(n).CtrlSize
theForm.Controls.Add(btn)
' rehook the event handler(s)
AddHandler btn.Click, AddressOf btn_Click
这篇关于保存动态创建的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!