簡體   English   中英

如何使用組合框或列表框並為1:M選擇附加百分比?

[英]How can I use a combobox or listbox and attach a percent for 1:M selections?

我不確定在這種情況下哪個更好,因為我以前從未使用過ListBox。

背景:我將開發一個WinForm,將某人的獎金(某些貨幣)的百分比添加到不同的基金(股票)中。 Funds Table具有以下字段:基金ID,基金股票行情,基金名稱,基金描述和雇員ID(以FK表示)。

例如,喬·史密斯(Joe Smith)的獎金為1,000美元。 他目前可以將獎金分為6個活躍基金選項,總百分比必須= 100%,並且每個基金可以為0-100%。 (他必須推遲支付全部獎金,但每個基金不能超過100%)。

組合框或列表框,無論對此有何最佳控制,都將由當前的“活動”(“基金”表中的布爾字段)基金選項填充。 (基本上,就像是將錢放入股票的購股權。)該控件將顯示“基金名稱”。

因此,喬·史密斯(Joe Smith)將25%的股票期權(250美元)投入其中。 用戶可以在“下拉菜單”中選擇基金,然后在文本框中輸入25,以將25%的資金分配給該特定基金。

喬·史密斯(Joe Smith)仍有$ 750可以用於其他資金。 這是我為用戶做什么感到困惑的地方。

在不清除先前輸入的字段的情況下給用戶另一個“輸入”的最佳方法是什么?

我將發布我所擁有的屏幕截圖,也許會有所幫助。 在此處輸入圖片說明

我可以想象,如果“百分比”文本框中的值不是100%或讓用戶單擊“添加延遲”按鈕,然后“復制”獎金區域(在屏幕快照中)並將其添加到當前位置下方。 不過,我認為使用Winforms不可能做到這一點,除非我根據情況使用了Visible = True或False,並且這在屏幕尺寸調整等方面會有些混亂。

這是概念。

要對其進行測試,請以新的形式放置一個文本框(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

基本上將其復制給其他控件。 它應該向您展示如何動態添加控件。 您還可以為其他每個事件添加事件處理程序。

一種方法是使用自定義面板,其中包含基金名稱的標簽,numericupdownbox來設置百分比,也許還有一個標簽將其標識為百分比,或者您認為每種基金都需要使用控件的任意組合鑒定。 您可以使用面板列表並設置一個通用的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM