A.6. Chapter 8
A.6.1. Exercise 1 Solution
The code has a user interface with two textboxes for getting the starting and ending heights for the table and a listbox or ListView object to present the results. The button click event code does most of the work and one solution using a listbox is shown here:
const double MININCHES = 36; const double MAXINCHES = 96; private void btnCalc_Click(object sender, EventArgs e) { bool flag; int i; int j; double start; double end; double male; double female; double[,] idealWeights; string buff; //=================== Input ========================== flag = double.TryParse(txtStart.Text, out start); // Table start if (flag == false) { MessageBox.Show("Numeric only."); txtStart.Focus(); return; }
flag = double.TryParse(txtEnd.Text, out end); // Table end if (flag == false) { MessageBox.Show("Numeric only."); txtEnd.Focus(); return; } //=================== Validate Inputs ================ if (start < MININCHES || start > MAXINCHES) // Check table limits { MessageBox.Show("Table can only span " + MININCHES.ToString() + " to " + MAXINCHES + " inches."); txtStart.Focus(); return; } if (end < MININCHES || end > MAXINCHES) { MessageBox.Show("Table can only span " + MININCHES.ToString() + " to " + MAXINCHES + " inches."); txtStart.Focus(); return; } if (end <= start) // Can we display anything? { MessageBox.Show("Starting value must be less than ending value"); txtStart.Focus(); return; } // Define the array for table data idealWeights = new double[2, ...
Get Beginning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.