Use skills in Delphi ondrawcell

zhaozj2021-02-08  263

Use skills in Delphi ondrawcell

Liang Yun, Southeast University

When using Delphi to develop database applications, you often use the table control to make a report. Flexible use of the onDrawcell event for the table control, you can complete some special effects, which can better meet user needs. This article introduces three techniques that flexibly use the onDrawcell event. Dynamic update table is sometimes necessary to dynamically update the color of the table in the report. For example, in the supplier list, the supplier of priority supply is green, and other suppliers are displayed in red. At this time, the following can be implemented by OnDrawCell event code: if Table1.FieldByName ( 'CustNo') AsInteger> 1500 then DBGrid1.Canvas.Font.Color:. = ClRed; DBGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State) above The code can be expanded according to the needs of the application. For example, when you need to display a black body, you will change the corresponding row to: dbgrid1.canvas.font.style: = [fsbold]; insert other visual controls in the table In database programming, foreign key constraint is to ensure data in the database The user requested an effective way to store. For example, in a order processing environment, there is a foreign key constraint between the order table and the client table, that is, each record in the order table should have a corresponding item in the client table. This way, when filling out the order table, if the user selection is provided in the input focus, the correctness of the data will be effectively guaranteed. Both DBGRID controls and StringGrid controls are derived from TCUSTOMGRID, where DBGrid controls can enter data. Delphi's internal mechanism for DBGRID is floating on a grid a DBEDIT control. Ability to get focus, the grid of entering data is actually a floating DBEDIT control. When the focus shifts to display the text in the edit box to the table, there is no difference between the other unfolded places and StingGrid, which reaches the input data on the table. the goal of. Therefore, the method of inserting other visual controls in the table is to float the visual control on the grid. So in principle, any control can be inserted in the table. This article is inserted into a drop-down frame in a table as an example. The operation results are shown in Figure 1:

figure 1

1. Set the attribute to read the Items property of the client list to the drop-down box. 2. Set the onDrawcell event onDrawcell event to be triggered when drawing the cell's cell. When the field corresponding to the focus is consistent with the fields corresponding to the drop-down box, the drop-down frame is moved to the grid that the focus is obtained, and the drop-down frame is visible, thereby reaching the function of displaying the down-pull frame on the table specified column. Set the onDrawcell event of the table as follows: if (gdfocused in state) The begin // The corresponding field is consistent, execute if (Grid.cells [Acol, 0] = 'Company') THEN BEGIN / / Move Downlink box to the specified location ComboBox1.Left : = Rect.Left Grid.Left; ComboBox1.top: = Rect.top Grid.Top; ComboBox1.Width: = Rect.right-Rect.Lect; ComboBox1.Height: = Rect.Bottom-Rect.top; / / Make the pull-down box and get focus comBOBOX1.Visible: = true; comboBoX1.Setfocus; end; end; 3. Setting the OnClick event If the field corresponding to the unit of the focus is inconsistent with the fields corresponding to the pull-down box, the pull-down frame is not visible. To this end, the onclick event of the table is set as follows: IF (grid.cells [grid.col, 0]

转载请注明原文地址:https://www.9cbs.com/read-2371.html

New Post(0)