let btnCalculateClick(e) = let mutable pricePerCCF = 0.00 let mutable monthlyCharges = 0.00 let consumption = float txtConsumption.Text pricePerCCF <- 50.00 if consumption >= 0.50 then begin pricePerCCF <- 35.00 end txtPricePerCCF.Text <- sprintf "%0.02f" pricePerCCF pricePerCCF <- float txtPricePerCCF.Text monthlyCharges <- consumption * pricePerCCF txtMonthlyCharges.Text <- sprintf "%0.02f" monthlyCharges
The .NET Framework provides two Windows controls that are ready for Boolean values.
The Check Box Control: The most fundamental control for Boolean values is the check box. It is created using the CheckBox class. Here is an example of creating a check box:
open System open System.Windows.Forms let exercise = new Form() exercise.Text <- "Exercise" let isVisible = new CheckBox() isVisible.Left <- 12 isVisible.Top <- 18 isVisible.Width <- 120 isVisible.Text <- "Control is Visible" exercise.Controls.Add(isVisible) Application.Run(exercise)
The CheckBox class is equipped with a property named Checked. If/when the control is checked, its Checked property holds a value of true. If not, the property is set to false. At any time, you can programmatically find out what value that property is holding. Here is an example:
open System open System.Windows.Forms let exercise = new Form() exercise.Text <- "Exercise" exercise.Width <- 250 exercise.Height <- 80 // Check Box: Is Visible let chkIsVisible = new CheckBox() chkIsVisible.Left <- 12 chkIsVisible.Top <- 18 chkIsVisible.Width <- 110 chkIsVisible.Text <- "Control is Visible" exercise.Controls.Add(chkIsVisible) // Text Box let txtContinent = new TextBox() txtContinent.Left <- 130 txtContinent.Top <- 18 txtContinent.Visible <- false exercise.Controls.Add(txtContinent) let chkIsVisibleClick(e) = if chkIsVisible.Checked then txtContinent.Visible <- true else txtContinent.Visible <- false chkIsVisible.Click.Add(chkIsVisibleClick) Application.Run(exercise)
Here is an example of running the program:
Radio Buttons: The radio button is another valuable control for Boolean values. Like the check box, the radio button is equipped with the Checked property. If/when the radio button is filled, the property holds a true value. If/when it is empty, the property holds a false value.
Unlike the check box, the radio button is hardly used by itself. It is usually accompanied by other radio buttons and they function has a group. Consequently, you can use a variable, usually integral-based so that, when a radio button is clicked, you set the value of that variable. If another radio button is clicked, you update the value of that variable. Then, when it is time, you can get the value of that variable to find out what radio button is currently checked in the group.
A property is said to be Boolean if it can be true or false. For example, controls have a property named Visible that specifies whether a control can be seen or hidden.
Practically all Windows controls can be used in conditional statements. Some controls are text based. You can enquire about the contents of such controls and validate something about their values or behaviors.
Many controls display text. These include the text box, the masked text box, etc. By using a conditional statement, you can enquire about the text of a control to compare it to some value and decide what to do.
Some controls display text but their items are stored in a list. These include the combo box, the list box, the domain up-down control, etc. Those controls have a Text property whose value can be enquired the same way it is done for a text-based control.
Collection-based controls are not directly made to display text. Instead, they include objects that subsequently display text. These include the list view, the tree view, the data grid control, etc. The items of those controls have a Text property. That property may not be directly available from the control itself. This means that you must first get to the desired item. You can then apply a conditional statement on that property the same way you would on a text-based property.
Although the items in a combo box or a list box are text, their positions, called indexes, are stored in a 0-based list. The first item on top of the list has an index of 0. The second from top has an index of 1, and so on. Based on this, you can use a conditional statement to find out what item is currently selected. This means that you can base your statement on either text or index of the item and decide what to do.
A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. Here is an example:
open System open System.Windows.Forms let payroll = new Form() payroll.Text <- "Payroll Preparation" payroll.Width <- 290 payroll.Height <- 140 let lblHourlySalary = new Label() lblHourlySalary.Left <- 18 lblHourlySalary.Top <- 18 lblHourlySalary.Width <- 82 lblHourlySalary.Text <- "Hourly Salary:" payroll.Controls.Add(lblHourlySalary) let txtHourlySalary = new TextBox() txtHourlySalary.Left <- 102 txtHourlySalary.Top <- 16 txtHourlySalary.Width <- 64 txtHourlySalary.Text <- "0.00" payroll.Controls.Add(txtHourlySalary) let chkPaidOvertime = new CheckBox() chkPaidOvertime.Left <- 175 chkPaidOvertime.Top <- 14 chkPaidOvertime.Text <- "Paid Overtime" payroll.Controls.Add(chkPaidOvertime) let lblWeeklyTime = new Label() lblWeeklyTime.Left <- 18 lblWeeklyTime.Top <- 46 lblWeeklyTime.Width <- 82 lblWeeklyTime.Text <- "Weekly Time:" payroll.Controls.Add(lblWeeklyTime) let txtWeeklyTime = new TextBox() txtWeeklyTime.Left <- 102 txtWeeklyTime.Top <- 44 txtWeeklyTime.Width <- 64 txtWeeklyTime.Text <- "0.00" payroll.Controls.Add(txtWeeklyTime) let btnCalculate = new Button() btnCalculate.Left <- 176 btnCalculate.Top <- 43 btnCalculate.Width <- 88 btnCalculate.Text <- "Calculate" let lblWeeklySalary = new Label() lblWeeklySalary.Left <- 18 lblWeeklySalary.Top <- 76 lblWeeklySalary.Width <- 82 lblWeeklySalary.Text <- "Weekly Salary:" payroll.Controls.Add(lblWeeklySalary) let txtWeeklySalary = new TextBox() txtWeeklySalary.Left <- 102 txtWeeklySalary.Top <- 74 txtWeeklySalary.Width <- 64 txtWeeklySalary.Text <- "0.00" payroll.Controls.Add(txtWeeklySalary) let btnClose = new Button() btnClose.Left <- 176 btnClose.Top <- 73 btnClose.Width <- 88 btnClose.Text <- "Close" let btnCalculateClick(e) = let weeklySalary = ref 0.00 let hourlySalary = float txtHourlySalary.Text let weeklyTime = float txtWeeklyTime.Text weeklySalary := hourlySalary * weeklyTime if chkPaidOvertime.Checked then if weeklyTime > 40.00 then let overtime = weeklyTime - 40.00 let overtimePay = hourlySalary * 1.50 * overtime weeklySalary := (hourlySalary * 40.00) + overtimePay let strWeeklySalary = sprintf "%0.02f" !weeklySalary txtWeeklySalary.Text <- strWeeklySalary btnCalculate.Click.Add(btnCalculateClick) payroll.Controls.Add(btnCalculate) let btnCloseClick(e) = payroll.Close() btnClose.Click.Add(btnCloseClick) payroll.Controls.Add(btnClose) Application.Run(payroll)
Here is an example of running the program:
In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on.
Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression. Consider the following example:
(* This program assists a clerk in a hotel to check the availability of rooms. The customer must specify how many people need a stay.*) open System open System.Windows.Forms let hotelManagement = new Form() hotelManagement.Width <- 340 hotelManagement.Height <- 110 hotelManagement.Text <- "Hotel Management" // Label: Number of Guests let lblGuests = new Label() lblGuests.Left <- 23 lblGuests.Top <- 25 lblGuests.Width <- 102 lblGuests.Text <- "Number of Guests:" hotelManagement.Controls.Add lblGuests // Text Box: Guests let txtGuests = new TextBox() txtGuests.Left <- 126 txtGuests.Top <- 22 txtGuests.Width <- 27 txtGuests.Text <- "0" hotelManagement.Controls.Add txtGuests // Label: Daily Rate let lblDailyRate = new Label() lblDailyRate.Left <- 157 lblDailyRate.Top <- 25 lblDailyRate.Width <- 33 lblDailyRate.Text <- "Rate:" hotelManagement.Controls.Add lblDailyRate // Text Box: Daily Rate let txtDailyRate = new TextBox() txtDailyRate.Left <- 192 txtDailyRate.Top <- 22 txtDailyRate.Width <- 42 txtDailyRate.Text <- "0.00" hotelManagement.Controls.Add txtDailyRate // Label: Per Night let lblPerNight = new Label() lblPerNight.Left <- 234 lblPerNight.Top <- 25 lblPerNight.Width <- 37 lblPerNight.Text <- "/Night" hotelManagement.Controls.Add lblPerNight // Label: Available Rooms let lblAvailableRooms = new Label() lblAvailableRooms.Left <- 23 lblAvailableRooms.Top <- 52 lblAvailableRooms.Width <- 95 lblAvailableRooms.Text <- "Available Rooms:" hotelManagement.Controls.Add lblAvailableRooms // Text Box: Available Rooms let txtAvailableRooms = new TextBox() txtAvailableRooms.Left <- 126 txtAvailableRooms.Top <- 50 txtAvailableRooms.Width <- 180 hotelManagement.Controls.Add txtAvailableRooms let txtGuestsLostFocus e = let guests = int txtGuests.Text let singleRate = 98.85 let availableRooms = ref "" // If one or two people (only) will occupy the room, apply the daily/nightly rate if (guests = 1) || (guests = 2) then txtDailyRate.Text <- string singleRate txtAvailableRooms.Text <- "103, 107, 108, 211, 212, 215, 218" // If 3 to 5 people will occupy the room, add half to the daily rate elif (guests >= 3) && (guests <= 5) then let appliedRate = singleRate + (singleRate * 0.50) txtDailyRate.Text <- sprintf "%0.02f" appliedRate txtAvailableRooms.Text <- "104, 106, 210, 214, 216, 313" (* If there are more than 5 people, there is no sigle bedroom t hat can accommodate such a number. As a result, get more rooms. *) else txtDailyRate.Text <- "0.00" txtAvailableRooms.Text <- "Please get more than one room." txtGuests.Leave.Add txtGuestsLostFocus do Application.Run hotelManagement
This would produce:
As seen previously, one way you can combine conditional statements is by nesting them.