Spread Windows Forms 8.0 Product Documentation > Developer's Guide > Customizing Interaction in Cells > Using Edit Mode and Focus > Locking a Cell |
You can lock a cell or range of cells and make it unavailable for editing by the end user. You can make the appearance of locked cells different so that the locked cells are noticeable by the user.
You can lock cells using the Locked property in the Cell, Column, Row, or AlternatingRow objects. You can also set the Locked property for the StyleInfo object and apply that style to the cells you want locked. You also need to set the Protect property of the SheetView object to lock the cells. The Locked property marks the cells to be locked, and the Protect property sets whether to lock those cells. For cells marked as locked to be locked from user input, the Protect property of the sheet must be set to True, which is its default value. If it is set to False, the user can still interact with the cells.
Another way to lock cells is to make them text cells (using the TextCellType) and set the ReadOnly property. This makes the cells non-editable.
You can also specify a different color (for background or for text) or font in locked cells using the LockBackColor, LockForeColor, and LockFont properties of the SheetView or Appearance objects.
Locking a cell does not lock any shapes (floating objects) over that cell. A protected sheet only means that all the cells in that sheet marked as locked are locked; it does not apply to shapes over that sheet. For locking shapes, refer to Customizing Drawing.
Making sure that the Protect property is true for the sheet, you can lock specified columns of cells and then unlock some of the cells in one row, as shown in the following example.
C# |
Copy Code
|
---|---|
fpSpread1.ActiveSheet.Protect = true; fpSpread1.ActiveSheet.LockBackColor = Color.LightCyan; fpSpread1.ActiveSheet.LockForeColor = Color.Green; FarPoint.Win.Spread.Column columnobj; columnobj = fpSpread1.ActiveSheet.Columns[0, 3]; columnobj.Locked = true; FarPoint.Win.Spread.Cell cellobj; cellobj = fpSpread1.ActiveSheet.Cells[1,1,1,2]; cellobj.Locked = false; fpSpread1.ActiveSheet.Cells[1,0,1,4].Text = "First Five"; |
VB |
Copy Code
|
---|---|
FpSpread1.ActiveSheet.Protect = True FpSpread1.ActiveSheet.LockBackColor = Color.LightCyan FpSpread1.ActiveSheet.LockForeColor = Color.Green Dim columnobj As FarPoint.Win.Spread.Column columnobj = fpSpread1.ActiveSheet.Columns(0, 3) columnobj.Locked = True Dim cellobj As FarPoint.Win.Spread.Cell cellobj = fpSpread1.ActiveSheet.Cells(1,1,1,2) cellobj.Locked = False FpSpread1.ActiveSheet.Cells(1,0,1,4).Text = "First Five" |
Making sure that the Protect property is True for the sheet, you can lock specific columns of cells and then unlock some of the cells in one row, as shown in the following example.
C# |
Copy Code
|
---|---|
fpSpread1.ActiveSheet.Protect = true; fpSpread1.ActiveSheet.LockBackColor = Color.LightCyan; fpSpread1.ActiveSheet.LockForeColor = Color.Green; fpSpread1.ActiveSheet.Columns[0, 3].Locked = true; fpSpread1.ActiveSheet.Cells[1,1,1,2].Locked = false; |
VB |
Copy Code
|
---|---|
FpSpread1.ActiveSheet.Protect = True FpSpread1.ActiveSheet.LockBackColor = Color.LightCyan FpSpread1.ActiveSheet.LockForeColor = Color.Green FpSpread1.ActiveSheet.Columns(0, 3).Locked = True FpSpread1.ActiveSheet.Cells(1,1,1,2).Locked = False |
(Another way of doing this is to select the Cells property, click on the button to call up the Cell, Column, and Row editor, and select the cells in that editor.)