2013-04-03 31 views
1

我是VB 2010的新用戶。我正在做一個簡單的程序作爲我的Uni項目的一部分 該程序的想法是幫助用戶找到適合他/她的卡路里。 計算卡路里需要考慮許多因素。 這些都是年齡,體重,身高和活動 水平那麼用戶應該決定他/她想要保持他/她的體重或基於所有這些輸入..膳食應顯示將列表框項目綁定到公式

其實減肥 ,我構建了程序的形式......並且我編寫了計算BMR的公式,這是計算卡路里之前的前一步驟。儘管取得了這些進展,但我發現很難將「列表框」中的活動級別與方程式相聯繫,因爲每個級別都表示一個數字以將其添加到方程中。我不知道該怎麼辦。我應該首先確定列表框的項目嗎?

我發現很多的材料在網站和YouTube,但有沒有什麼我需要 我需要你的幫助

這是我做了什麼

If RadioButton1.Checked = True Then 
      TextBox1.Text = (66 + (13.7 * MaskedTextBox1.Text) + (5 * MaskedTextBox2.Text) - (6.8 * MaskedTextBox3.Text)) 
     ElseIf RadioButton2.Checked = True Then 
      TextBox1.Text = (665 + (9.6 * MaskedTextBox1.Text) + (1.8 * MaskedTextBox2.Text) - (4.7 * MaskedTextBox3.Text)) 
     End If 
+0

我沒有得到什麼列表框項目包含。但是,讓我們說他們包含卡路里的數量。您應該檢查ListboxName.SelectedIndexChanged上特定項目所包含的數據。告訴我你是否需要持續的幫助! – Alex

+0

Thanx Alex,實際上,我的listbox內容3項......所以,當用戶輸入他/她的細節時,他/她必須選擇其中一個項目。但是,這些項目是作爲文本編寫的。所以,我需要程序將所選文本轉換爲數字,以便將其添加到等式中。 – user2239468

+0

例如,在我的程序中...如果用戶輸入他/她的性別,年齡,體重,身高和他/她選擇了列表框中的一個項目,結果應該是(上面的等式)列表框中的項目 – user2239468

回答

0

你做的是以下幾點:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    If ListBox1.SelectedItem = "This is item 1" Then 
     'Do your math here - If you want the first item to be 1 
     'then you simply use the number 1 here 

     'Example: (Will display 2) 
     MsgBox(1 + 1) 
    ElseIf ListBox1.SelectedItem = "This is the 2:nd item" Then 
     'Do your math here - If you want the first item to be 1 
     'then you simply use the number 1 here 

     'Example: (Will display 3) 
     MsgBox(1 + 2) 
    ElseIf ListBox1.SelectedItem = "This is item 3" Then 
     'Do your math here - If you want the first item to be 1 
     'then you simply use the number 1 here 

     'Example: (Will display 4) 
     MsgBox(1 + 3) 
    End If 
End Sub 

所以基本上,如果所選擇的項目是calcutate項目1 + 1 - 代碼會做「等式」

如果你想要項目被動態添加,有另一種方式來做到這一點:)

+0

非常感謝你,,,,這有助於我很多 – user2239468

相關問題