如何使用组合框或列表框并为1:M选择附加百分比?

时间:2014-02-19 19:58:12

标签: vb.net winforms combobox

我不确定在这种情况下哪种情况更好,因为我之前从未使用过ListBox。

背景:我将开发一个WinForm来为不同的基金(股票)增加某人奖金(某些货币数量)的百分比。 Funds Table包含以下字段:基金ID,基金代码,基金名称,基金描述和员工ID为FK。

例如,Joe Smith的奖金为1,000美元。目前有6个活跃的基金选择他可以将他的奖金分成,并且总百分比必须= 100%,并且每个基金可以是0-100%。 (他必须推迟他的全部奖金,但每个基金不能推迟超过100%)。

组合框或列表框,无论对此最好的控制,都将由当前“活动”(基金表内的布尔字段)基金选项填充。 (基本上,就像投入资金的股票期权一样。)控制权将显示基金名称。

因此,Joe Smith将25%投入一个基金期权(250美元)。用户可以在“下拉列表”中选择基金,并在文本框中输入25,以将25%委托给该特定基金。

乔史密斯还有750美元可以推迟到其他基金。 这是我对用户该怎么做感到困惑的地方。

在不清除之前输入的字段的情况下,为用户提供另一个“条目”的最佳方法是什么?

我会发布一张我所拥有的屏幕截图,也许会有所帮助。 enter image description here

我可以想象,如果百分比文本框中的值不是100%或让用户单击“添加延迟”按钮,则“复制”奖励区域(在屏幕截图中)并将其添加到当前下方位置。我不认为这可能是使用Winforms,除非我根据情况使用Visible = True或False,这会使屏幕大小调整等等。

2 个答案:

答案 0 :(得分:1)

以下是这个概念。

要测试它,请在新表格上放置一个文本框(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

基本上为您的其他控件复制此内容。它应该向您展示如何动态添加控件。您还可以为许多其他事物添加事件处理程序。

答案 1 :(得分:0)

执行此操作的一种方法是使用自定义面板,其中包含基金名称的标签,数字注册框以设置百分比,可能还有标签将其标识为百分比,或者您认为必要的任何控件组合对于每个确定的基金。您可以使用面板列表并设置常见的valuechanged事件处理程序,以确保总百分比不超过100.每次用户选择基金时,声明并添加/突出显示新面板。

面板基本上是控件的容器。如果按照所需的方式设置面板,请在设计器中创建一个继承自Panel的类。您可以添加所需的控件并在New构造函数中设置属性。现在添加一个新面板意味着将一个abject声明为类的新实例,设置位置并将其添加到表单的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 Class