Friday, March 20, 2020

Create a Mouseover Color Highlight Using Delphi

Create a Mouseover Color Highlight Using Delphi Have you ever seen a menu or table column or row highlight to a different color when your mouse hovers over it? Thats what our goal is here: to have a row become highlighted when the mouse pointer is within range. The TDBGrid Delphi component is one of the jewels of the VCL. Designed to enable a user to view and edit data in a tabular grid, the DBGrid provides various ways of customizing the way it represents its own data. For example, adding color to your database grids will enhance the appearance and differentiate the importance of certain rows or columns within the database. However, dont be fooled by over-simplistic tutorials on this topic. It might seem easy enough to just set the dgRowSelect property, but remember that when dgRowSelect is included in Options, the dgEditing flag is ignored, meaning that editing the data using the grid is disabled. What youll find below is an explanation on how to enable the OnMouseOver type of event for a DBGrid row, so that the mouse is recorded and located, making the record active so as to highlight the corresponding row in a DBGrid. How to Work With OnMouseOver and Delphi Components The first order of business is writing code for the OnMouseMove event in a TDBGrid component so that it can locate the DBGrids row and column (cell) that the mouse is hovering over. If the mouse is over the grid (handled in the OnMouseMove event handler), you can use the MoveBy method of a DataSet component to set the current record to the one displayed below the mouse cursor. type THackDBGrid class(TDBGrid);...procedure TForm1.DBGrid1MouseMove (Sender: TObject; Shift: TShiftState; X, Y: Integer);var gc: TGridCoord;begin gc: DBGrid1.MouseCoord(x, y); if (gc.X 0) AND (gc.Y 0) thenbegin DBGrid1.DataSource.DataSet.MoveBy (gc.Y - THackDBGrid(DBGrid1).Row); end;end; Similar code can be used to show which cell the mouse hovers over and to change the cursor when its over the title bar. In order to correctly set the active record, you need to hack a DBGrid and get your hands on the protected Row property. The Row property of a TCustomDBGrid component holds the reference to the currently active row. Many Delphi components have useful properties and methods that are marked invisible, or protected, to a Delphi developer. Hopefully, to access such protected members of a component, a simple technique called the protected hack can be used. With the code above, when you move the mouse over the grid, the selected record is the one displayed in the grid below the mouse cursor. Theres no need to click the grid to change the current record. Have the active row highlighted to enhance the users experience: procedure TForm1.DBGrid1DrawColumnCell (Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);beginif (THackDBGrid(DBGrid1).DataLink.ActiveRecord 1 THackDBGrid(DBGrid1).Row) or (gdFocused in State) or (gdSelected in State) thenbegin DBGrid1.Canvas.Brush.Color : clSkyBlue; DBGrid1.Canvas.Font.Style : DBGrid1.Canvas.Font.Style [fsBold]; DBGrid1.Canvas.Font.Color : clRed; end;end; The OnDrawColumnCell event is used to handle the need for a customized drawing for the data in the cells of the grid. You can use a little trick to differentiate the selected row from all the other rows. Consider that the Row property (integer) is equal to the ActiveRecord (1) property of the DataLink object that the selected row is about to be painted. Youll probably want to disable this behavior (the MoveBy method in OnMouseMove event handler) when DataSet connected to a DBGrid is in Edit or Insert mode.​

Wednesday, March 4, 2020

Correlation Analysis in Sociological Research

Correlation Analysis in Sociological Research Correlation is a term that refers to the strength of a relationship between two variables where a strong, or high, correlation means that two or more variables have a strong relationship with each other while a weak or low correlation means that the variables are hardly related. Correlation analysis is the process of studying the strength of that relationship with available statistical data. Sociologists can use statistical software like SPSS to determine whether a relationship between two variables is present, and how strong it might be, and the statistical process will produce a correlation coefficient that tells you this information. The most widely used type of  correlation coefficient  is the Pearson r. This analysis assumes that the two variables being analyzed are measured on at least  interval scales, meaning they are measured on a range of increasing value. The coefficient is calculated by taking the  covariance  of the two variables and dividing it by the product of their  standard deviations. Understanding the Strength of Correlation Analysis Correlation coefficients can range from -1.00 to 1.00 where a value of -1.00 represents a perfect negative correlation, which means that as the value of one variable increases, the other decreases while a value of 1.00 represents a perfect positive relationship, meaning that as one variable increases in value, so does the other. Values like these signal a perfectly linear relationship between the two variables, so that if you plot the results on a graph it would make a straight line, but a value of 0.00 means that there is no relationship between the variables being tested and would be graphed as separate lines entirely. Take for example the case of the relationship between education and income, which is demonstrated in the accompanying image. This shows that the more education one has, the more money they will earn in their job. Put another way, these data show that education and income are correlated  and that there is a strong positive correlation between the two- as education rises, so too does income, and the same kind of correlation relationship is found between education and wealth as well. The Utility of Statistical Correlation Analyses Statistical analyses like these are useful because they can show us how different trends or patterns within society might be connected, like unemployment and crime, for example; and they can shed light on how experiences and social characteristics shape what happens in a persons life. Correlation analysis lets us say with confidence that a relationship does or does not exist between two different patterns or variables, which allows us to predict the probability of an outcome among the population studied. A recent study of marriage and education found a strong negative correlation between the level  of education and the divorce rate. Data from the National Survey of Family Growth show that  as education level increases among women, the divorce rate for first marriages decreases. Its important to keep in mind, though, that correlation is not the same as causation, so while there exists a strong correlation between education and divorce rate, that does not necessarily mean the decrease in divorce among women is caused by the amount of education received.