在Form窗体中生成m个按钮(按钮的Text值不同),然后显示窗体。做到当点击某按钮时,可以将字符串R的值设置成按钮的Text值。情人节祝福网页
12345678910111213141516171819202122232425262728293031 Public Class ChooseForm Public R As String Public Sub Choose (ByVal n As Integer) Dim btn(n) As System.Windows.Forms.Button Dim m As Integer For m = 1 To n btn(m) = New System.Windows.Forms.Button With btn(m) .Name = "btn" & CStr(m) .Text = T(m) .Location = New Point(12 + (m - 1) * 47, 123) .Width = 41 .Height = 29 .FlatStyle = FlatStyle.Standard .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left .Visible = True .Enabled = True End With Controls.Add(btn(m)) AddHandler btn(m).Click, AddressOf btn_Click Next Me.MinimumSize = New System.Drawing.Size(m * 47 - 12, 232) Me.ShowDialog() End Sub Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) '这里我不知道添加什么好 End Sub
现在我只能做到动态生成n个按钮,并且从数组T(元素0闲置不用)中抽取第m个字符串作为Text属性。可是我不能在按钮的公共事件中添加判断单击的是什么按钮,或者将字符串R设置为被单击按钮的Text值。
try
Dim btn As Button = TryCast(sender, Button)
btn.Text =R
只需要把btn.Text =R改成R=btn.Text就行了。
这段代码的确可行。
其实可以缩减成一行:R=TryCast(sender, Button).Text