OnClick event occurs when the user clicks the control.
We use the OnClick event handler to respond when the user clicks the control. If the control has an associated action, and that action has an OnExecute method, the action’s OnExecute method responds to click events unless it is superseded by an OnClick event handler.
Usually, OnClick occurs when the user presses and releases the left mouse button with the mouse pointer over the control. This event can also occur when:
- The user selects an item in a grid, outline, list, or combo box by pressing an arrow key.
- The user presses the SPACEBAR while a button or checkbox has focus.
- The user presses ENTER when the active form has a default button (specified by the Default property).
- The user presses ESC when the active form has a cancel button (specified by the Cancel property).
- The user presses the accelerator key for a button or checkbox. For example, if the value of the Caption property of a checkbox is ‘&Bold’, the B is underlined at run time and the OnClick event of the check box is triggered when the user presses Alt+B. However, the focus does not move to the control in these instances.
- The Checked property of a radio button is set to True.
- The value of the Checked property of a checkbox is changed.
- The Click method of a menu item is called.
For a form, an OnClick event occurs when the user clicks a disabled component or in a blank area of the form.
OnClick is an event handler of type TNotifyEvent.
Here is the working example of the implementation of an OnClick event:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#Button creation btnOK = Button(pgOne) btnOK.SetProps(Parent=pgOne,Caption = 'Submit',Name = 'btnOK') btnOK.SetBounds(145,540,60,30) #Memo creation memoEvent = Memo(pgOne) memoEvent.SetProps(Parent=pgOne) memoEvent.SetBounds(300,14,350,300) def ClickHandler(Sender): memoEvent.Lines.Add(Sender.Name + ' clicked') btnOK.OnClick = ClickHandler |
To see the result, let’s run the complete script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
from DelphiVCL import * class MainForm(Form): def __init__(self, Owner): self.Caption = "Components Overview Sample" self.Name = "BaseForm" self.SetBounds(10, 10, 700, 650) # pnlMain = CreateComponent('TPanel',Owner) pnlMain.SetProps(Parent=self, Caption="",align = "alClient", Name = "MainPanel") # page control creation pgConMain = PageControl(pnlMain) pgConMain.Name = "MyPageControl" pgConMain.Parent = pnlMain pgConMain.Align = "alClient" # Tabsheet one pgOne = TabSheet(pnlMain) pgOne.PageControl = pgConMain pgOne.Caption = "StandardControls" # label Creation lblHello = CreateComponent('TLabel',pgOne) lblHello.Parent = pgOne lblHello.Caption = 'FirstName' lblHello.Name = 'Name' lblHello.Left = 20 lblHello.Top = 14 lblHello.Width = 121 lblHello.Height = 30 # Edit box creation edtHello = CreateComponent('TEdit',pgOne) edtHello.Parent = pgOne edtHello.Text = 'Jim' edtHello.Name = 'edtFirstName' edtHello.Left = 145 edtHello.Top = 14 edtHello.Width = 121 edtHello.Height = 30 chkSingle = CheckBox(self) chkSingle.Parent = pgOne chkSingle.Caption = 'Single ?' chkSingle.Alignment = 'taLeftJustify' chkSingle.SetBounds(20, 44, 141, 30) # label Creation lblCountry = CreateComponent('TLabel',pgOne) lblCountry.SetProps(Parent=pgOne,Caption='Country') lblCountry.SetBounds(20,88,121,30) #listbox lboxCountry = ListBox(pgOne) lboxCountry.SetProps(Parent=pgOne) lboxCountry.SetBounds(145,88,121,60) lboxCountry.Items.Add('RUSSIA') lboxCountry.Items.Add('USA') lboxCountry.Items.Add('INDIA') lboxCountry.Items.Add('AUSTRALIA') # label Creation lblDateOfIssue = CreateComponent('TLabel',pgOne) lblDateOfIssue.SetProps(Parent=pgOne,Caption='Date Of Birth') lblDateOfIssue.SetBounds(20,162,121,30) dtpDateofIssue = CreateComponent('TDateTimePicker',pgOne) dtpDateofIssue.SetProps(Parent=pgOne) dtpDateofIssue.SetBounds(145,162,121,30) # label Creation lblCity = CreateComponent('TLabel',pgOne) lblCity.SetProps(Parent=pgOne,Caption='City') lblCity.SetBounds(20,206,121,30) #Combobox cbxCity = ComboBox(pgOne) cbxCity.SetProps(Parent=pgOne,Name='ComboBox') cbxCity.SetBounds(145,206,121,60) cbxCity.Items.Add('New York') cbxCity.Items.Add('Sydney') cbxCity.Items.Add('Banglore') cbxCity.Items.Add('Tokyo') def ChangeHandler(sender): memoEvent.Lines.Add('changed') cbxCity.OnChange = ChangeHandler # Radiogroup Creation rbgRadioBox = RadioGroup(pgOne) rbgRadioBox.SetProps(Parent=pgOne,Caption='Gender') rbgRadioBox.SetBounds(20,250,242,90) rbgRadioBox.Items.Add('Male') rbgRadioBox.Items.Add('Female') #Button btnOK = Button(pgOne) btnOK.SetProps(Parent=pgOne,Caption = 'Submit',Name = 'btnOK') btnOK.SetBounds(145,540,60,30) #Memo memoEvent = Memo(pgOne) memoEvent.SetProps(Parent=pgOne) memoEvent.SetBounds(300,14,350,300) def ClickHandler(Sender): memoEvent.Lines.Add(Sender.Name + ' clicked') btnOK.OnClick = ClickHandler # Tabsheet two pgTwo = TabSheet(pnlMain) pgTwo.PageControl = pgConMain pgTwo.Caption = "Ext controls" shpRectangle = Shape(pgTwo) shpRectangle.SetProps(Parent=pgTwo,Shape = 'stRectangle') shpRectangle.SetBounds(140,14,400,200) clbSelect = ColorBox(pgTwo) clbSelect.SetProps(Parent=pgTwo) clbSelect.SetBounds(20,14,100,30) def ColorChangeHandler(Sender): shpRectangle.Brush.Color = clbSelect.Selected clbSelect.Onchange = ColorChangeHandler grdTest = DrawGrid(pgTwo) grdTest.Parent = pgTwo grdTest.SetBounds(20, 220, 520, 180) def grdTestDrawCell(Sender, Col, Row, Rect, State): if gdSelected in State: Sender.Canvas.Brush.Color = clBlue # 0x00ff0000 # blue Sender.Canvas.TextRect(Rect, Rect.Left+2, Rect.Top+2, "%d @ %d" % (Col, Row)) def grdTestSelectCell(Sender, Col, Row, CanSelect): if Col == 2 and Row == 2: CanSelect.Value = False grdTest.OnDrawCell = grdTestDrawCell grdTest.OnSelectCell = grdTestSelectCell self.OnClose = self.MainFormClose def MainFormClose(self, Sender, Action): Action.Value = caFree def btnOKClick(self, Sender): btnOK.Caption = "Clicked" def main(): Application.Initialize() Application.Title = "MyDelphiApp" f = MainForm(Application) f.Show() FreeConsole() Application.Run() main() |
Check out DelphiVCL which easily allows you to build GUIs for Windows using Python.