delphi - How to simplify setting a variable multiple times? -
i want program sql query filters dates, using contents of combobox, in latter want months in letters, have use numbers query, used code:
procedure tadh_filter.radiobutton3click(sender: tobject); var param : integer; begin if combobox1.text = 'january' begin param := 1; end else if combobox1.text = 'february' begin param := 2; end else if combobox1.text = 'march' begin param := 3; end else if combobox1.text = 'april' begin param := 4; end else if combobox1.text = 'may' begin param := 5; end else if combobox1.text = 'june' begin param := 6; end else if combobox1.text = 'july' begin param := 7; end else if combobox1.text = 'august' begin param := 8; end else if combobox1.text = 'september' begin param := 9; end else if combobox1.text = 'october' begin param := 10; end else if combobox1.text = 'november' begin param := 11; end else if combobox1.text = 'december' begin param := 12; end end;
i know if it's possible simplify this.
you should using combo box csdropdownlist
style. means values add combo can selected. in case can read value need out of itemindex
.
procedure tadh_filter.radiobutton3click(sender: tobject); var param : integer; begin param := combobox1.itemindex + 1; end;
if using other style of combo box user able input invalid months typing in combo. doesn't sound though that's desirable you. however, if reason not want use csdropdownlist
need convert month text index, , include validity check. if need that, can simplify matters in comparison code in question using code @bummi provided here: convert month name number in delphi?
note code runs risk of failing set param
. piece of general advice i'd suggest include else
clause in if
statement that, , else
clause raises exception. way @ least discover if, somehow, month not being recognised.
Comments
Post a Comment