我不知道在这个场景中有什么更好,因为我以前从未使用过ListBox。
背景:我将开发一个WinForm,将某人的奖金百分比(一些货币数字)添加到不同的基金(股票)中。Funds Table有以下字段:基金ID、基金提存、基金名称、基金描述和作为FK的EmployeeID。
例如,乔·史密斯有1,000美元的奖金。目前,他可以将自己的奖金分成6种,总比例必须为100%,每只基金的总比例为0%-100%。(他不得不推迟发放全部奖金,但每只基金不能推迟超过100% )。
无论这方面的最佳控制是什么,combobox或listbox都将由当前的“Active”( Fund表中的布尔字段)基金选项填充。(基本上,就像把钱投入的股票期权一样。)控件将显示基金名称。
因此,Joe将25%的投资投给了一家基金公司(250美元)。用户将在“下拉列表”中选择基金,并在文本框中键入25,将25%委托给该特定基金。
乔·史密斯仍有750美元需要转入其他基金。这就是我不知道该为用户做什么的地方。
在不清除先前输入的字段的情况下,给用户另一个“条目”的最佳方法是什么?
我会发布一个屏幕截图,我拥有的,也许这会有所帮助。

我可以想象,如果百分比文本框中的值不是100%,或者让用户单击“add Deferrel”按钮,那么“复制”奖金区域(在屏幕截图中)并将其添加到当前位置的下方。不过,我认为使用Winforms是不可能的,除非我根据具体情况使用了Visible = True或False,而且屏幕大小会变得有些混乱,等等。
发布于 2014-03-12 19:01:30
这是一个概念。
要测试它,在一个新的表单上,放置一个文本框(TextBox1)和两个按钮(Button1,Button2)。
粘贴此代码:
Dim index As Integer = 1
Dim yMargin As Integer = 10 ' the vertical spacing between rows of controls relative to the textboxes
Private Sub addEntry()
Dim txt As New TextBox With
{.Size = TextBox1.Size,
.Location = New Point(
TextBox1.Location.X,
TextBox1.Location.Y + index * (TextBox1.Height + yMargin)),
.Visible = True,
.Text = index.ToString,
.Name = "txt" & index.ToString()} ' , etc., other visual features
Me.Controls.Add(txt)
index += 1
End Sub
Private Sub removeEntry()
If index = 1 Then Exit Sub
index -= 1
Me.Controls.Remove(Me.Controls("txt" & index.ToString()))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
addEntry()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
removeEntry()
End Sub基本上把这个复制到其他控件上。它将向您展示如何动态添加控件。此外,还可以为每个事件添加事件处理程序。
发布于 2014-02-19 21:15:05
要做到这一点,一种方法是使用一个自定义面板,其中包含一个基金名称的标签(一个数字下拉框)来设置百分比,或者使用一个标签将其标识为百分比,或者使用您认为对所识别的每只基金都有必要的控制组合。您可以使用面板列表并设置公共的值事件处理程序,以确保总百分比不超过100。每次用户选择基金时,都要声明并添加/突出显示新的面板。
面板基本上是控制的容器。如果以所需的方式设置面板,则在设计器中创建继承自Panel的类。可以添加所需的控件,并在新建构造函数中设置属性。现在,添加一个新的面板意味着将一个对象声明为类的一个新实例,设置位置并将其添加到窗体的Controls集合中。下面是如何设置类的示例:
Class BonusPanel
Inherits Panel
Friend PercentUsedLabel As New System.Windows.Forms.Label()
Friend FundLabel As New System.Windows.Forms.Label()
Private TotalLabel As New System.Windows.Forms.Label()
Private PercentLabel As New System.Windows.Forms.Label()
Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
Public Sub New()
FundLabel.Anchor = System.Windows.Forms.AnchorStyles.Left
FundLabel.AutoSize = True
FundLabel.BackColor = System.Drawing.Color.White
FundLabel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
FundLabel.Location = New System.Drawing.Point(3, 20)
PercentLabel.Anchor = System.Windows.Forms.AnchorStyles.Top
PercentLabel.Location = New System.Drawing.Point(145, 2)
PercentLabel.Size = New System.Drawing.Size(62, 13)
PercentLabel.Text = "Percentage"
TotalLabel.Anchor = System.Windows.Forms.AnchorStyles.Top
TotalLabel.AutoSize = True
TotalLabel.Location = New System.Drawing.Point(234, 2)
TotalLabel.Size = New System.Drawing.Size(31, 13)
TotalLabel.Text = "Total"
PercentUsedLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
PercentUsedLabel.Size = New System.Drawing.Size(53, 18)
PercentUsedLabel.Location = New System.Drawing.Point(222, 20)
Anchor = System.Windows.Forms.AnchorStyles.None
Controls.Add(PercentUsedLabel)
Controls.Add(TotalLabel)
Controls.Add(FundLabel)
Controls.Add(PercentLabel)
Controls.Add(NumericUpDown1)
Size = New System.Drawing.Size(293, 42)
End Sub
End Classhttps://stackoverflow.com/questions/21891054
复制相似问题