如何将返回值从用户定义的函数打印到ListBox - VB 2017 [英] How to print a return value from a user-defined function to a ListBox - VB 2017
问题描述
让我先说一下我是VB新手;我正在参加一个项目管理课程,这个课程需要课程,虽然我没有任何编码背景,而且这是一个自学,教授的支持很少。 因为我正在尽力利用像StackOverflow.com和其他文档网站这样的资源,请与我一起支付
,但是,我*非常*新手...
挑战:
我的任务是创建一个工资单应用程序,该应用程序接收用户输入并使用它们来计算总薪酬,净工资,税收等输出这些计算的过程存储在一个函数中,该函数通过点击事件和第二次点击事件("添加到列表")进行调用。按钮)应该将返回的值打印到ListBox。
这是我的代码:
Let me preface this by saying that I am new to VB; I am taking a project management course that requires course this class although I have no background in coding whatsoever, and it's a self-study with minimal support from the professor. Please bare
with me as I am doing my best to leverage resources like StackOverflow.com and other documentation sites, however, I am *very* novice...
The challenge:
I am tasked to create a payroll application that takes in user inputs and uses them to calculate outputs such as gross pay, net pay, taxes, et al.
The procedure for these calculations is stored in a function that is invoked with a click event, and a 2nd click event ("Add to List" button) should print the returned values to a ListBox.
Here is my code:
Public Class frmPayroll
Public Property txtDisplay As Object
Public Sub lstPayrollList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstPayrollList.SelectedIndexChanged
Dim Listitem As String = "{1,7:N2}{2,5:c5}{1,7:N2}{1,7:N2}{1,7:N2}{1,7:N2}{1,7:N2}"
Dim txtDisplay As Object = Nothing
txtDisplay.Text = String.Format(Listitem, "Gross Pay", "Dependents", "Contributions", "State Tax", "FICA Tax", "Federal Tax", "Net Pay")
End Sub
Public Function CalculatePay()
Dim GrossPay, txtHours, txtPayRate, txtDependents, DependentAmt, txtContributions, txInc, StaTax, FICA, FedTxAmnt, TotalTxs, NetPay As Integer
Dim FICARt = 0.0765
Dim DependentRate = 0.1975
Dim StTxRt = 0.0375
Dim FedTxRt = 0.1128
DependentAmt = txtDependents * DependentRate
txInc = GrossPay - DependentAmt - txtContributions
StaTax = txInc * StTxRt
FICA = GrossPay * FICARt
FedTxAmnt = txInc
TotalTxs = StaTax + FICA + FedTxAmnt
NetPay = GrossPay - TotalTxs - txtContributions
If txtHours > 40 Then
GrossPay = 40 * txtPayRate + (txtHours - 40) * txtPayRate * 1.5
ElseIf txtHours <= 40 Then
GrossPay = txtHours * txtPayRate
End If
Return GrossPay
Return DependentAmt
Return txInc
Return StaTax
Return FICA
Return FedTxAmnt
Return TotalTxs
Return NetPay
End Function
Private Sub BtnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
CalculatePay()
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
lstPayrollList.Items.Add(CalculatePay)
End Sub
End Class
我的问题: 我在btnAdd_Click事件中做错了什么?当我运行它时,它打印"0"。到列表框
$
到目前为止我尝试过的内容:我查看了几个资源,例如:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items(v=vs.110).aspx
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-call-a-procedure-that-returns-a-value
https://stackoverflow.com/questions/47025502/invoking-a-function-with-a-click-event-in-form-vb
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-return-a-价值来自程序
http://www.vbtutor.net/index.php/visual-basic-2017-lesson-17-functions/
My Question: What am I doing wrong in the btnAdd_Click event? When I run it, it prints "0" to the ListBox
What I have tried so far: I have looked at several resources, such as:
https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.items(v=vs.110).aspx
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-call-a-procedure-that-returns-a-value
https://stackoverflow.com/questions/47025502/invoking-a-function-with-a-click-event-in-form-vb
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/how-to-return-a-value-from-a-procedure
http://www.vbtutor.net/index.php/visual-basic-2017-lesson-17-functions/
---
这些链接都没有解决从返回值打印到列表框的具体问题是用户定义的过程。 &NBSP;请告诉我,如果我错误地提出这个问题,我可能会误解/误用这些条款。
感谢您提前付款!
None of these links address the specific question which is to print to a Listbox from a returned value is a user-defined procedure. Please tell me if I asking this incorrectly, it is possible that I am misunderstanding/mis-using the terms.
THANK YOU LOTS IN ADVANCE!
推荐答案
b $ b $
函数是否为"CalculatePay"按照你的期望工作? 我担心只要它被调用就会返回GrossPay。
没有定义返回值,没有参数。这是一个功能吗? 这不是一个子例程/处理,而不是功能吗?
问候,
Hi,
Does a function "CalculatePay" work as you expect? I'm afraid it returns nothing but GrossPay whenever it is called.
No return value is defined, no parameters are in it. Is it a function? Isn't it a sub routine/procesure, instead of function?
Regards,
这篇关于如何将返回值从用户定义的函数打印到ListBox - VB 2017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!