Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
QuestionAvoid space at the Start of the textbox Pin
smh13927-Sep-13 10:21
smh13927-Sep-13 10:21 
AnswerRe: Avoid space at the Start of the textbox Pin
Dave Kreskowiak7-Sep-13 12:55
mveDave Kreskowiak7-Sep-13 12:55 
AnswerRe: Avoid space at the Start of the textbox Pin
Abhinav S7-Sep-13 18:05
Abhinav S7-Sep-13 18:05 
GeneralRe: Avoid space at the Start of the textbox Pin
Ron Beyer7-Sep-13 18:11
professionalRon Beyer7-Sep-13 18:11 
GeneralRe: Avoid space at the Start of the textbox Pin
Abhinav S7-Sep-13 19:09
Abhinav S7-Sep-13 19:09 
GeneralRe: Avoid space at the Start of the textbox Pin
BillWoodruff7-Sep-13 19:48
professionalBillWoodruff7-Sep-13 19:48 
GeneralRe: Avoid space at the Start of the textbox Pin
Pete O'Hanlon8-Sep-13 2:33
mvePete O'Hanlon8-Sep-13 2:33 
AnswerRe: Avoid space at the Start of the textbox Pin
BillWoodruff7-Sep-13 19:27
professionalBillWoodruff7-Sep-13 19:27 
Please post specific coding questions, like this one, in the Q&A forum, thanks.

Since (to the best of my knowledge) you cannot now delete this question here, and repost it in Q&A, I'll respond.

First, if you want to create a re-usable TextBox because: you want to use many TextBoxes that behave like this; my esteemed colleague Dave K. is absolutely right when he advises you to make a TextBox Control Class that inherits from Windows Forms TextBox.

If you don't know how to effectively sub-class WinForms Controls, please research Codeproject, the VS 201#x documentation, and MSDN, for information on creating your own Components, or UserControls. If you have problems with understanding, or implementing, them: ask them here on Q&A.

Here's some sample code that just modifies the behavior of one specific TextBox, 'textBox1:
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (textBox1.SelectionStart == 0)
    {
        // is the current character entered white-space:
        // white space will include tabs, spaces, and cr/lf
        if
        // method #1: FrameWorks 1.1~4,5: simple, probably best
        (Char.IsWhiteSpace(e.KeyChar))
        // method #2: FrameWorks 4~4.5: costs us a conversion
        // (string.IsNullOrWhiteSpace(e.KeyChar.ToString()))
        {
            e.Handled = true;
        }
    }
}

Note: that you don't need to explicitly set e.Handled to 'false because 'false is the default value.

If you only want to suppress the space-character, and you want to allow tabs, or cr/lf's to be typed at position #0 in the TextBpx: then change the code above.

And, something you might think about: what happens if the user pastes text in, at position #0 in the TextBox, and the pasted-in content begins with spaces, tabs, etc. ? Try that, using the code above, and observe Smile | :)

Resources you should review: [^],[^],[^].

bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview

GeneralRe: Avoid space at the Start of the textbox Pin
smh13927-Sep-13 22:17
smh13927-Sep-13 22:17 
GeneralRe: Avoid space at the Start of the textbox Pin
Pete O'Hanlon7-Sep-13 22:30
mvePete O'Hanlon7-Sep-13 22:30 
GeneralRe: Avoid space at the Start of the textbox Pin
BillWoodruff8-Sep-13 4:08
professionalBillWoodruff8-Sep-13 4:08 
GeneralRe: Avoid space at the Start of the textbox Pin
Pete O'Hanlon8-Sep-13 4:46
mvePete O'Hanlon8-Sep-13 4:46 
GeneralRe: Avoid space at the Start of the textbox Pin
BillWoodruff8-Sep-13 20:09
professionalBillWoodruff8-Sep-13 20:09 
GeneralRe: Avoid space at the Start of the textbox Pin
Pete O'Hanlon8-Sep-13 23:11
mvePete O'Hanlon8-Sep-13 23:11 
GeneralRe: Avoid space at the Start of the textbox Pin
Dave Kreskowiak8-Sep-13 3:34
mveDave Kreskowiak8-Sep-13 3:34 
GeneralRe: Avoid space at the Start of the textbox Pin
BillWoodruff8-Sep-13 4:15
professionalBillWoodruff8-Sep-13 4:15 
AnswerRe: Avoid space at the Start of the textbox Pin
blitzkrieged7-Sep-13 20:57
blitzkrieged7-Sep-13 20:57 
GeneralRe: Avoid space at the Start of the textbox Pin
Dave Kreskowiak8-Sep-13 3:37
mveDave Kreskowiak8-Sep-13 3:37 
AnswerRe: Avoid space at the Start of the textbox Pin
sep_exambo8-Sep-13 15:07
professionalsep_exambo8-Sep-13 15:07 
AnswerRe: Avoid space at the Start of the textbox Pin
Eddy Vluggen9-Sep-13 8:15
professionalEddy Vluggen9-Sep-13 8:15 
QuestionHow to check the activated Soundoutput device? Pin
Member 101332676-Sep-13 22:28
Member 101332676-Sep-13 22:28 
AnswerRe: How to check the activated Soundoutput device? Pin
Richard MacCutchan6-Sep-13 23:10
mveRichard MacCutchan6-Sep-13 23:10 
Questionneed online hotel managment project in .net Pin
teena chundavat6-Sep-13 8:34
teena chundavat6-Sep-13 8:34 
AnswerRe: need online hotel managment project in .net Pin
Eddy Vluggen6-Sep-13 8:39
professionalEddy Vluggen6-Sep-13 8:39 
AnswerRe: need online hotel managment project in .net Pin
jschell6-Sep-13 8:40
jschell6-Sep-13 8:40 

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.