我正在使用Windows窗体应用程序并在表单加载时动态创建一些控件。
创建的控件是Label,UltraDateTimeEditor和Button。
我使用以下代码来实现它。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim n = 5
For i As Integer = 0 To n - 1
'Create label
Dim label As New Label()
label.Text = [String].Format("Label {0}", i)
'Position label on screen
label.Left = 50
label.Top = (i + 1) * 30
label.Width = 70
'Create UltraDateTimeEditor
Dim dtpicker = New UltraDateTimeEditor
dtpicker.Name = "Date" + i.ToString()
dtpicker.MaskInput = "{date} hh:mm"
dtpicker.MaskDisplayMode = MaskMode.IncludeBoth
dtpicker.SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
dtpicker.Left = 120
dtpicker.Top = (i + 1) * 30
Dim button As New Button()
'Position textbox on screen
button.Text = "Now"
button.Left = 290
button.Top = (i + 1) * 30
'Add controls to form
Me.Panel1.Controls.Add(label)
Me.Panel1.Controls.Add(dtpicker)
Me.Panel1.Controls.Add(button)
Next
End Sub
屏幕看起来像这样
现在当点击相应datetimeeditor旁边的Now按钮时,我需要将当前日期和时间放入相应的datetimeeditor。 请建议我该怎么做。
答案 0 :(得分:2)
您可以将AddHandler
用于Button
的动态控件,以便点击它们时可以运行一些代码...请注意:AddHandler
部分...
代码已经过试用和测试
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim n = 5
For i As Integer = 0 To n - 1
'Create label
Dim label As New Label()
label.Text = [String].Format("Label {0}", i)
'Position label on screen
label.Left = 50
label.Top = (i + 1) * 30
label.Width = 70
Dim dtpicker = New UltraDateTimeEditor
dtpicker.Name = "Date" + i.ToString()
dtpicker.MaskInput = "{date} hh:mm"
dtpicker.MaskDisplayMode = MaskMode.IncludeBoth
dtpicker.SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
dtpicker.Left = 120
dtpicker.Top = (i + 1) * 30
dtpicker.Tag = i 'Set this as we will use this for comparison
Dim button As New Button()
'Position textbox on screen
button.Text = "Now"
button.Tag = i 'Set this as we will use this for comparison
button.Left = 290
button.Top = (i + 1) * 30
'Add controls to form
Me.Controls.Add(label)
Me.Controls.Add(dtpicker)
Me.Controls.Add(button)
AddHandler button.Click, AddressOf UpdateTime
Next
End Sub
此方法处理更改DateTime
对象...我们使用Button.Tag
属性并针对DateTime.Tag
进行检查。然后我们可以设置与Button
相关联的时间。
Private Sub UpdateTime(sender As System.Object, e As System.EventArgs)
Dim nButton As Button = DirectCast(sender, Button)
If nButton IsNot Nothing AndAlso nButton.Tag >= 0 Then
For Each ctrl In Me.Controls.OfType(Of UltraDateTimeEditor)()
If ctrl.Tag = nButton.Tag Then
ctrl.Value = DateTime.UtcNow
Exit Sub
End If
Next
End If
End Sub
答案 1 :(得分:0)
您可以使用Label,DatePicker和Button作为包含的控件创建UserControl,然后处理UserControl中的按钮事件,将DateTime.Now值分配给UserControl中的DatePicker。
然后用创建新UserControl的实例替换原始代码。
您将需要UserControl的属性,它们是按钮文本和实例中的标签。
答案 2 :(得分:-1)
要获取当前DateTime
,只需使用两个属性中的一个(具体取决于您的使用案例):
DateTime.Now
DateTime.UtcNow
然后,当您拥有当前DateTime
后,您可以设置UltraDateTimeEditor的.Value
属性,该属性将显示的DateTime
控制为 now 属性。
要为每个按钮分配EventHandler
,您需要创建一个处理特定按钮的.Click
事件的Sub。但是,为了做到这一点,我们首先必须在Form Load事件局部范围之外移动按钮声明。
Dim button(5) As Button
Dim dtpicker(5) As UltraDateTimeEditor
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i As Integer = 0 To n - 1
'Create label
Dim label As New Label()
label.Text = [String].Format("Label {0}", i)
'Position label on screen
label.Left = 50
label.Top = (i + 1) * 30
label.Width = 70
'Create UltraDateTimeEditor
dtpicker(i) = New UltraDateTimeEditor
dtpicker(i).Name = "Date" + i.ToString()
dtpicker(i).MaskInput = "{date} hh:mm"
dtpicker(i).MaskDisplayMode = MaskMode.IncludeBoth
dtpicker(i).SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
dtpicker(i).Left = 120
dtpicker(i).Top = (i + 1) * 30
button(i) = New Button()
'I assigned the index to the name so we can handle all 5 buttons with one event handler
button(i).Name = i.ToString()
'Position textbox on screen
button(i).Text = "Now"
button(i).Left = 290
button(i).Top = (i + 1) * 30
'Add controls to form
Me.Panel1.Controls.Add(label)
Me.Panel1.Controls.Add(dtpicker(i))
Me.Panel1.Controls.Add(button(i))
Next
End Sub
现在是按钮的事件处理程序代码。这将处理所有5个按钮
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles buttons(0).Click, buttons(1).Click, buttons(2).Click, buttons(3).Click, buttons(4).Click
Dim button As Button = DirectCast(sender, Button)
dtpicker(CInt(button.Name)).Value = DateTime.Now
End Sub
这里我们访问相关UltraDateTimeEditor数组元素的索引,并将其.Value
属性设置为当前DateTime