Bra49er

im new to this so bear with me. i am trying to make a calculator that averages 4 numbers, 3 numbers and 2 numbers. I have a form set up with four textboxes, a calculate button and a fifth textbox for the outcome. My problem is i cant figure out how to make it so that if i want to just average three textboxes and leave the fourth blank or two boxes and the second set blank that my fifth textbox will still show the average of those when the button is clicked. I've tried a few things with selectcase but i think i may be far off of what i actually need to do.



Dim paycheck1 As Integer
Dim paycheck2 As Integer
Dim paycheck3 As Integer
Dim paycheck4 As Integer
Dim paychecktotals As Integer
Dim paycheckaverage As Integer
paycheck1 = TextBox1.Text
paycheck2 = TextBox2.Text
paycheck3 = TextBox3.Text
paycheck4 = TextBox4.Text

paychecktotals = paycheck1 + paycheck2 + paycheck3 + paycheck4
paycheckaverage = paychecktotals / 4
TextBox5.Text = paycheckaverage


thanks



Re: Visual Basic Express Edition Making an average calculator

Dave299

In pseudo code:

Declare another variable, say "Items", to hold the number of boxes filled in.

In your CalculateButton.Click event

if textbox1.text is not "" then convert textbox1.text to an integer and add it to paychecktotals. Add 1 to Items

Repeat for the other boxes.

Divide paychecktotal by items, convert to string and assign to textbox5.text

And turn Option Strict ON. It will help you to avoid mistakes.





Re: Visual Basic Express Edition Making an average calculator

Solitaire

First of all, your code will generate a type mismatch error because paycheck1 is an integer and TextBox1.Text is a string. You need to convert the text into an integer, using the TryParse method. Also, the average may not be an integer. But first, you need to make sure the textbox is not blank. You also don't need all those variables for each paycheck, as one will work for all of them and the paychecktotals variable is used as an accumulator.

You can shorten the code by using a For Each loop and assigning a Tag property to the first 4 textboxes. Here is the working code for you to study, showing both the full and the condensed versions. They both work identically. Don't forget to assign the Tag property of "pay" to the first 4 textboxes for the condensed code to work.

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim paycheck, paychecktotals, paycheckaverage As Integer

Dim paycheckavg As Double

Dim count As Integer = 0

If TextBox1.Text <> "" Then

count += 1

Integer.TryParse(TextBox1.Text, paycheck)

paychecktotals += paycheck

End If

If TextBox2.Text <> "" Then

count += 1

Integer.TryParse(TextBox2.Text, paycheck)

paychecktotals += paycheck

End If

If TextBox3.Text <> "" Then

count += 1

Integer.TryParse(TextBox3.Text, paycheck)

paychecktotals += paycheck

End If

If TextBox4.Text <> "" Then

count += 1

Integer.TryParse(TextBox4.Text, paycheck)

paychecktotals += paycheck

End If

paycheckavg = paychecktotals / count

paycheckaverage = Convert.ToInt32(paycheckavg)

TextBox5.Text = paycheckaverage.ToString

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim paycheck, paychecktotals, paycheckaverage As Integer

Dim paycheckavg As Double

Dim count As Integer = 0

For Each ctl As Control In Controls

If TypeOf ctl Is TextBox And ctl.Tag Is "pay" Then

If ctl.Text <> "" Then

count += 1

Integer.TryParse(ctl.Text, paycheck)

paychecktotals += paycheck

End If

End If

Next ctl

paycheckavg = paychecktotals / count

paycheckaverage = Convert.ToInt32(paycheckavg)

TextBox5.Text = paycheckaverage.ToString

End Sub

Code Snippet





Re: Visual Basic Express Edition Making an average calculator

Solitaire

Here is a variation of the program without using multiple textboxes. It uses a Do loop with InputBoxes and continues until the user enters nothing or clicks Cancel. This way you can have as many or as few paychecks as needed.

By the way, a paycheck should be Double, not Integer, if you are including any cents along with the dollars. I kept the Integer type here in order to be consistent with my previous example.

Code Snippet

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim paycheck, paychecktotals, paycheckaverage As Integer

Dim paycheckavg As Double, reply As String

Dim count As Integer = 0

Do

count += 1

reply = InputBox("Enter amount of paycheck # " & count, "Cancel or nothing to stop")

Integer.TryParse(reply, paycheck)

paychecktotals += paycheck

Loop Until reply = ""

paycheckavg = paychecktotals / count

paycheckaverage = Convert.ToInt32(paycheckavg)

MessageBox.Show("Average paycheck is " & paycheckaverage.ToString)

TextBox5.Text = paycheckaverage.ToString

End Sub






Re: Visual Basic Express Edition Making an average calculator

ssta

And now for something complely different. I know this isn't exactly what you asked for but it gives you some more flexability.


Add the following Controls to a form:

  • DatagridView1

  • TextBox1

  • TextBox2

  • TextBox3

Code Snippet

Public Class Form1

Private WithEvents _dtCheques As New DataTable


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim dc As DataColumn

dc = New DataColumn("Cheque #", GetType(Integer))

dc.AutoIncrement = True

dc.AutoIncrementSeed = 1

_dtCheques.Columns.Add(dc)


dc = New DataColumn("Amount", GetType(Decimal))

dc.AllowDBNull = True

_dtCheques.Columns.Add(dc)


DataGridView1.DataSource = _dtCheques

End Sub


Private Sub Recalc()

Dim strFilter As String = "ISNULL(Amount,-1) >= 0"

Dim value As Object

value = _dtCheques.Compute("Sum(Amount)", strFilter)

TextBox1.Text = IIf(IsNumeric(value), value.ToString, "")

value = _dtCheques.Compute("Count(Amount)", strFilter)

TextBox2.Text = IIf(IsNumeric(value), value.ToString, "")

value = _dtCheques.Compute("Avg(Amount)", strFilter)

TextBox3.Text = IIf(IsNumeric(value), value.ToString, "")

End Sub


Private Sub _dtCheques_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles _dtCheques.RowChanged

Recalc()

End Sub


Private Sub _dtCheques_RowDeleted(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles _dtCheques.RowDeleted

Recalc()

End Sub

End Class



Or the more Complex Version:



Code Snippet

Public Class Form1

Private _ds As New DataSet


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim dc As DataColumn


With _ds.Tables.Add("Totals")

dc = New DataColumn("ID", GetType(Integer))

dc.AutoIncrement = True

dc.AutoIncrementSeed = 1

.Columns.Add(dc)


dc = New DataColumn("Sum", GetType(Decimal))

.Columns.Add(dc)


dc = New DataColumn("Count", GetType(Decimal))

.Columns.Add(dc)


dc = New DataColumn("Avg", GetType(Decimal))

.Columns.Add(dc)

End With


With _ds.Tables.Add("Cheques")

dc = New DataColumn("ID", GetType(Integer))

dc.DefaultValue = 1

.Columns.Add(dc)


dc = New DataColumn("Cheque #", GetType(Integer))

dc.AutoIncrement = True

dc.AutoIncrementSeed = 1

.Columns.Add(dc)


dc = New DataColumn("Amount", GetType(Decimal))

dc.AllowDBNull = True

.Columns.Add(dc)

End With


_ds.Relations.Add("ChequeTotals", _ds.Tables("Totals").Columns("ID"), _ds.Tables("Cheques").Columns("ID"), True)


With _ds.Tables("Totals")

.Columns("Sum").Expression = "Sum(Child(ChequeTotals).Amount)"

.Columns("Count").Expression = "Count(Child(ChequeTotals).Amount)"

.Columns("Avg").Expression = "Avg(Child(ChequeTotals).Amount)"

.Rows.Add(.NewRow)

End With


With DataGridView1

.DataSource = _ds

.DataMember = "Cheques"

.Columns("ID").Visible = False

.Columns("Cheque #").AutoSizeMode = _

DataGridViewAutoSizeColumnMode.AllCells

End With


With DataGridView1.Columns("Amount")

.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

.DefaultCellStyle.Format = "N2"

End With


TextBox1.DataBindings.Add("Text", _ds.Tables("Totals"), "Sum", True, DataSourceUpdateMode.Never, "-", "N2")


TextBox2.DataBindings.Add("Text", _ds.Tables("Totals"), "Count", True, DataSourceUpdateMode.Never, "-", "N2")


TextBox3.DataBindings.Add("Text", _ds.Tables("Totals"), "Avg", True, DataSourceUpdateMode.Never, "-", "N2")

End Sub


End Class






Re: Visual Basic Express Edition Making an average calculator

Bra49er

Thank you all soooo much for your answers they were all incredibly helpful!