English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here's what I have. I don't know what I'm supposed to do to get it working.

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Results As System.Windows.Forms.ListBox
Friend WithEvents txtFirstInteger As System.Windows.Forms.TextBox
Friend WithEvents txtSecondInteger As System.Windows.Forms.TextBox
Friend WithEvents txtThirdInteger As System.Windows.Forms.TextBox
Friend WithEvents btnEnter As System.Windows.Forms.Button
Friend WithEvents lblInstructions As System.Windows.Forms.Label
Friend WithEvents lblFirstInteger As System.Windows.Forms.Label
Friend WithEvents lblSecondInteger As System.Windows.Forms.Label
Friend WithEvents lblThirdInteger As System.Windows.Forms.Label
Private Sub InitializeComponent()
Me.txtFirstInteger = New System.Windows.Forms.TextBox
Me.txtSecondInteger = New System.Windows.Forms.TextBox
Me.txtThirdInteger = New System.Windows.Forms.TextBox
Me.btnEnter = New System.Windows.Forms.Button
Me.Results = New System.Windows.Forms.ListBox
Me.lblInstructions = New System.Windows.Forms.Label
Me.lblFirstInteger = New System.Windows.Forms.Label
Me.lblSecondInteger = New System.Windows.Forms.Label
Me.lblThirdInteger = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'txtFirstInteger
'
Me.txtFirstInteger.Location = New System.Drawing.Point(144, 104)
Me.txtFirstInteger.Name = "txtFirstInteger"
Me.txtFirstInteger.Size = New System.Drawing.Size(136, 20)
Me.txtFirstInteger.TabIndex = 0
Me.txtFirstInteger.Text = "0"
'
'txtSecondInteger
'
Me.txtSecondInteger.Location = New System.Drawing.Point(144, 144)
Me.txtSecondInteger.Name = "txtSecondInteger"
Me.txtSecondInteger.Size = New System.Drawing.Size(136, 20)
Me.txtSecondInteger.TabIndex = 1
Me.txtSecondInteger.Text = "0"
'
'txtThirdInteger
'
Me.txtThirdInteger.Location = New System.Drawing.Point(144, 184)
Me.txtThirdInteger.Name = "txtThirdInteger"
Me.txtThirdInteger.Size = New System.Drawing.Size(136, 20)
Me.txtThirdInteger.TabIndex = 2
Me.txtThirdInteger.Text = "0"
'
'btnEnter
'
Me.btnEnter.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnEnter.Location = New System.Drawing.Point(66, 216)
Me.btnEnter.Name = "btnEnter"
Me.btnEnter.Size = New System.Drawing.Size(160, 48)
Me.btnEnter.TabIndex = 3
Me.btnEnter.Text = "Enter"
'
'Results
'
Me.Results.Location = New System.Drawing.Point(24, 272)
Me.Results.Name = "Results"
Me.Results.Size = New System.Drawing.Size(248, 173)
Me.Results.TabIndex = 6
'
'lblInstructions
'
Me.lblInstructions.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblInstructions.Location = New System.Drawing.Point(8, 16)
Me.lblInstructions.Name = "lblInstructions"
Me.lblInstructions.Size = New System.Drawing.Size(272, 72)
Me.lblInstructions.TabIndex = 7
Me.lblInstructions.Text = "Input Three Integers To Find The Sum, Average, Product, Smallest, and Largest"
Me.lblInstructions.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblFirstInteger
'
Me.lblFirstInteger.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblFirstInteger.Location = New System.Drawing.Point(8, 104)
Me.lblFirstInteger.Name = "lblFirstInteger"
Me.lblFirstInteger.Size = New System.Drawing.Size(104, 24)
Me.lblFirstInteger.TabIndex = 8
Me.lblFirstInteger.Text = "First Integer"
Me.lblFirstInteger.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lblSecondInteger
'
Me.lblSecondInteger.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblSecondInteger.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.lblSecondInteger.Location = New System.Drawing.Point(8, 144)
Me.lblSecondInteger.Name = "lblSecondInteger"
Me.lblSecondInteger.Size = New System.Drawing.Size(120, 24)
Me.lblSecondInteger.TabIndex = 9
Me.lblSecondInteger.Text = "Second Integer"
'
'lblThirdInteger
'
Me.lblThirdInteger.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblThirdInteger.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.lblThirdInteger.Location = New System.Drawing.Point(8, 184)
Me.lblThirdInteger.Name = "lblThirdInteger"
Me.lblThirdInteger.Size = New System.Drawing.Size(104, 24)
Me.lblThirdInteger.TabIndex = 10
Me.lblThirdInteger.Text = "Third Integer"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 470)
Me.Controls.Add(Me.lblThirdInteger)
Me.Controls.Add(Me.lblSecondInteger)
Me.Controls.Add(Me.lblFirstInteger)
Me.Controls.Add(Me.lblInstructions)
Me.Controls.Add(Me.Results)
Me.Controls.Add(Me.btnEnter)
Me.Controls.Add(Me.txtThirdInteger)
Me.Controls.Add(Me.txtSecondInteger)
Me.Controls.Add(Me.txtFirstInteger)
Me.Name = "Form1"
Me.Text = "Input Three Integers"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim number1, number2, number3 As Integer ' Declares number1, number2, number3
Dim sum, average, product As Integer ' Declares sum, average, product
Dim smallest, largest As Integer ' Declares largest, smallest

number1 = Me.txtFirstInteger.Text
number2 = Me.txtSecondInteger.Text
number3 = Me.txtThirdInteger.Text

sum = number1 + number2 + number3
average = sum / 3
product = number1 * number2 * number3

If number1 < number2 & number1 < number3 Then
smallest = number1
End If

If number2 < number1 & number2 < number3 Then
smallest = number2
End If

If number3 < number1 & number3 < number2 Then
smallest = number3
End If

If number1 > number2 & number1 > number3 Then
largest = number1
End If

If number2 > number1 & number2 > number3 Then
largest = number2
End If

If number3 > number1 & number3 > number2 Then
largest = number3
End If

Me.Results.Text = "number1"
Me.Results.Text = "number2"
Me.Results.Text = "number3"
Me.Results.Text = "sum"
Me.Results.Text = "average"
Me.Results.Text = "product"
Me.Results.Text = "smallest"
Me.Results.Text = "largest"



End Sub

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

End Sub
End Class

2007-03-21 17:33:47 · 2 answers · asked by bloodyhell1981 1 in Computers & Internet Programming & Design

2 answers

I'm a Visual FoxPro programmer and not familiar enough with VB code to assist if this is a VB-specific issue. However, the way you are selecting the smallest and largest values will fail if there is a tie, because your code will not return a value for these variables. At the very least, you need to use <= and >= to handle ties.

Here is an improved solution (syntax is for Visual FoxPro, but you should get the idea):

smallest=number1
largest=number1
IF number2 smallest=number2
ENDIF
IF number2>largest
largest=number2
ENDIF
IF number3 smallest=number3
ENDIF
IF number3>largest
largest=number3
ENDIF

Please note that I almost never do things the way I describe above with a series of repeated IF/ENDIF statements, because normally there aren't a fixed, small number of possibilities. Usually I am looping through a table that can have any number of records, or a longer list of possibilities. So ideally you would want to run this through some kind of loop, and within the loop you would have one IF/ENDIF statement to check for smallest, and another to check for largest.

2007-03-21 18:55:40 · answer #1 · answered by Alan S 6 · 0 0

I'm not a VB programmer, as I use C++ and C#, but VB code, fortunately for you, is easy to read for me. =P

It looks as if you're trying to calculate the result when the form loads. "Form1_Load" is used for the initial startup of the form. I guess you could say it's similar to a class constructor.

You need to process the data in the "Button1_Click" event handler.

You didn't specify what was not working, so I'm assuming it just isn't displaying any results.

Another suggestion would be error-checking, as well as setting a default value for each integer. That way if the user doesn't enter a number, the program does not crash, or read in a bogus number.

And when you do "Me.Results.Text = "number1"", I don't believe that is actually adding the strings to the listbox "Results".

If you did something like "Me.Results.Add("number1")", it would add "number1", and not the actual 'number1' variable. There should be some sort of function to convert integers to a string value. In C#, it's 'someInt.ToString()'.

That's all I can come up with, based on the code, and the information that you did NOT give. =) But at least I tried.

If you're not sure, check the MSDN Library for adding items to a listbox, converting integers to strings, etc.

2007-03-22 02:02:12 · answer #2 · answered by the_dadd_from_tn2005 4 · 0 0

fedest.com, questions and answers