It’s common to display data that you don’t want users to change. I’ve seen developers deal with primary key values this way, although some developers argue that users shouldn’t even see primary key values. One thing is for sure, if a form displays those values, you won’t want users to change them without forcing them to jump through a few hoops. In fact, some administrators might refuse to allow users to have this kind of permission at all, opting to make those changes personally. Of course, that takes us down the road of primary key stability, but that’s not what this entry’s about, so we’ll table that discussion for another day.
There are a number of ways to alert users that a specific value isn’t updatable. The most common way is to set the control’s Back Color property to transparent. That way, the control seems to blend into the form’s background. For example, the following continuous form shows three text controls.
Using the following property settings, the Company Name control disappears — at least mostly:
- Locked: Yes
- BackStyle: Transparent
- BorderStyle: Transparent
There’s a small problem though. As you can see, when the control gets the focus, the background turns white. That behavior negates the other properties to a degree. You could use the color palette to try to match the control’s background color to the Detail section, but there’s an easier way. Simply set the control’s BackColor property to the same as the Detail section’s. That way, the control’s background always matches the form’s background exactly, even when the control has the focus.
You don’t have to know the color’s value. In the Properties sheet, select the Detail section and highlight the Back Color setting (a value most likely). Press Ctrl+C to copy it to the Clipboard. Then, select the control, click inside the Back Color property, and press Ctrl+V.