How to Output Cell Sizes
Data is frequently displayed in tables that consists of rows and columns. Sitting at every intersection between a row and a column is a cell. You can write a program that outputs the size of a cell in pixels. This can be useful when you are designing user interfaces and need to know the specific size of cells. A simple programming framework that provides a quick way to make such a program is the .Net framework. You can download this for free as well as the Visual Studio Express Integrated Development Environment.
Instructions
-
-
1
Open Visual Studio 2010 by clicking on its program icon. When it loads, select “File/New/Project “and click “Visual C#/Windows Forms Application”. A new Windows Form Application project is created, and a blank Windows Form appears in the main editor window.
-
2
Click on the “Toolbox” panel, which is located to the right of the main editor window. This panel displays all of the tools that are available to Windows Form applications.
-
-
3
Click on “DataGridView” and drag this tool over onto the Windows Form in the main editor window. When you release the mouse button, you will place the grid onto the Windows Form.
-
4
Click the small black arrow in the upper-right corner of the “DataGridView” to open a menu. Select the menu item labeled “Add Column” to open an “Add Column” interface. Press the “OK” button once to add one column and then press the “Close” button to close the interface.
-
5
Click the “Properties” panel, which is adjacent to the “Toolbox.” Click on the small lightning bolt symbol to display all of the events available to the “DataGridView.”
-
6
Double-click the event labeled “Cell Mouse Enter,” which is an event that occurs whenever a cell is clicked on by the user. The Windows Form disappears from the main editor window and a source code file appears. The following code is displayed in the source code file:
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{} -
7
Insert the following lines of code in between the curly brackets of the “CellMouseEnter” event. These lines of code grab the row and column location for the cell clicked.
int x = e.ColumnIndex;
int y = e.RowIndex; -
8
Write the following statements below the previous two lines to find out the cell size for the clicked cell. The following lines of code find the cell size and print the dimensions to the output window:
DataGridViewCell cell = dataGridView1[x, y];
Size cellsize = cell.Size;
System.Console.WriteLine("{0}", cellsize); -
9
Run the program by pressing the green “Play” button. A Windows Form appears and a table appears inside it. Click on any cell. The output window displays the size of the row, and produces output that looks something like this:
{Width=100, Height=22}
-
1