Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / C#
Tip/Trick

Untabstoppify Readonly Textboxes

Rate me:
Please Sign up or sign in to vote.
4.89/5 (6 votes)
28 Jan 2015CPOL 22K   4   17
Add a bit of code to a form to make all ReadOnly TextBoxes non-tabbable

You Can't Get Here From There

It makes no sense to me to have ReadOnly Textboxes that you can tab into - yet, you can. Here's some code you can add to your forms to prevent that:

C#
foreach (Control control in this.Controls)
{
    var txtbx = control as TextBox;
    if (null != txtbx)
    {
        txtbx.TabStop = (!txtbx.ReadOnly);
    }
}

Now all Textboxes that are ReadOnly are non-tabstoppable, and vice versa: all non-ReadOnly Textboxes are TabStoppable.

This is not Your Father's Oldfangled Code

Or, a more concise/spiffy way of doing the same thing is the suggestion from Fred below:

C#
foreach (var txtbx in Controls.OfType<textbox>())
{
    txtbx.TabStop = (!txtbx.ReadOnly);
}

Immer Besser, Was?

For a better approach to this (if you're going to put this sort of code on more than a couple of forms, or even more than one, depending on how worried you are about getting this snippet out of sync with that snippet), see prasy's answer here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
SuggestionAlternate Solution Pin
Raxdiam31-Jan-15 10:44
Raxdiam31-Jan-15 10:44 
QuestionCan be converted to Linq Pin
fredatcodeproject28-Jan-15 2:23
professionalfredatcodeproject28-Jan-15 2:23 
AnswerRe: Can be converted to Linq Pin
B. Clay Shannon28-Jan-15 3:38
professionalB. Clay Shannon28-Jan-15 3:38 
AnswerActually it makes sense Pin
Paw Jershauge27-Jan-15 20:06
Paw Jershauge27-Jan-15 20:06 
GeneralRe: Actually it makes sense Pin
freakyit27-Jan-15 22:05
freakyit27-Jan-15 22:05 
GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 3:37
professionalB. Clay Shannon28-Jan-15 3:37 
GeneralRe: Actually it makes sense Pin
fredatcodeproject28-Jan-15 5:39
professionalfredatcodeproject28-Jan-15 5:39 
GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 5:40
professionalB. Clay Shannon28-Jan-15 5:40 
GeneralRe: Actually it makes sense Pin
fredatcodeproject29-Jan-15 1:19
professionalfredatcodeproject29-Jan-15 1:19 
GeneralRe: Actually it makes sense Pin
Philippe Mori28-Jan-15 6:13
Philippe Mori28-Jan-15 6:13 
GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 6:17
professionalB. Clay Shannon28-Jan-15 6:17 
GeneralRe: Actually it makes sense Pin
Philippe Mori28-Jan-15 6:31
Philippe Mori28-Jan-15 6:31 
GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 6:43
professionalB. Clay Shannon28-Jan-15 6:43 
GeneralRe: Actually it makes sense Pin
Philippe Mori28-Jan-15 7:19
Philippe Mori28-Jan-15 7:19 
If they don't need to copy the content of the text box then why not disable it and avoid any confusion to your users.
Philippe Mori

GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 7:32
professionalB. Clay Shannon28-Jan-15 7:32 
GeneralRe: Actually it makes sense Pin
Philippe Mori28-Jan-15 7:40
Philippe Mori28-Jan-15 7:40 
GeneralRe: Actually it makes sense Pin
B. Clay Shannon28-Jan-15 7:43
professionalB. Clay Shannon28-Jan-15 7:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.