dcsimg
Results 1 to 9 of 9

Thread: [RESOLVED] Highlighted textboxes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    459

    Resolved [RESOLVED] Highlighted textboxes

    The form below is a display of a change request. So what the form actually represents is a newly created record in a table adapter with all of the boxes data bound to a specific field in the table adapter. Some of the fields have already been filled with data (although not yet updated). As can be seen, there are four text boxes that have a white back ground, with three of them already filled with data. What is notable is that the three text boxes with data are all highlighted.

    I would prefer that they not be highlighted and have looked through a large amount of information in an effort to discern why they are highlighted and to disable that property, without success. Can anyone guide me as to exactly what property is involved here and how to deal with that?

    Name:  ChangeRequest.jpg
Views: 183
Size:  35.8 KB

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,791

    Re: Highlighted textboxes

    Don't know how the information gets in the textboxes when you add a new record but you can always set the SelectLength property of a textbox.
    "Me.Field1TextBox.SelectionLength = 0"

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,057

    Re: Highlighted textboxes

    It's the HideSelection property you're looking for. The highlighting means that that text is selected as defined by the SelectionStart and SelectionLength properties. The HideSelection property determines whether selected text is highlighted only when that TextBox has focus (True) or all the time (False).

    You could set the SelectionLength to 0 but whether that's a good idea depends on what you want to happen when the user starts typing. If the whole text is selected then anything they type will automatically replace the existing value. Otherwise, they will have to select the text themselves first, or else Delete or Backspace to erase the existing text. Of course, if you don't want the existing text erased by default then you probably should set SelectionLength to 0, but you might also want to set SelectionStart to TextLength so that the caret will ne positioned at the end of the text instead of the beginning.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    459

    Re: Highlighted textboxes

    The information is input like so

    'Create new Change Request
    Dim ChangeRequestRow As _MasterBase5_0DataSet.tblChangeRequestRow
    ChangeRequestRow = _MasterBase5_0DataSet.tblChangeRequest.NewtblChangeRequestRow()
    ChangeRequestRow.intSiTechID = glbintSiTechID
    ChangeRequestRow.intChangeID = glbintChangeID
    ChangeRequestRow.strTitle = glbstrTitle
    ChangeRequestRow.strBaseObject = glbstrObject
    ChangeRequestRow.strOwner = glbstrOwner
    ChangeRequestRow.strWhere = glbstrWhereCategory
    ChangeRequestRow.dteOpen = CDate(CStr(DateTime.Today))
    ChangeRequestRow.strRevision = glbstrRevision
    _MasterBase5_0DataSet.tblChangeRequest.Rows.Add(ChangeRequestRow)
    The Selection Length = 0 worked quite well. That was one of the things I previously read about, but I obviously did not understand what it really did. I also found that it did not work for the title name textbox. It turns out that I had the tabstop = True and that textbox TabIndex = 0. If I set the tabstop = False, the text box would then work with Selection Length = 0. However, the highlight would then show at the next place the tabstop was set to true. I believe I can see why that is so. Thanks for the information.

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Highlighted textboxes

    I think it's important to know the mechanics behind this. The cause is more complicated than the TabStop and TabIndex properties, and if you go about believing that's the magic solution you'll be sabotaging your Form in ways you don't understand.

    By default, .NET controls select themselves when they receive focus. This is sort of complex in TextBoxes: if you CLICK on the TextBox, the caret is placed where you clicked. If you TAB into a TextBox, the entirety of the text is selected. It makes sense from a UX perspective: if you click you might want to do a precise edit and it's easy to select all with another click. If you're tabbing, you might want to just replace the value and that's a more complex series of keystrokes so they try to help.

    The reason you see this in your TextBox is a Form has a behavior at startup that finds the "first" available input control and selects it. This acts like it's been tabbed into via the keyboard.

    That's why TabStop and TabIndex seem to influence it: when TabStop is true it can be a target for focus from the keyboard. When TabIndex is 0, it's likely the "first" available control. So if you change TabStop to False and/or change the TabIndex property, the problem is "fixed" because now a different control is first. It seems like you figured that out. Worse: if a user wanted to use the keyboard to navigate around your form, they can't get to that control anymore. That stinks.

    Sadly, I find it's routinely difficult to make this dang feature go away. I figured handling Enter and setting SelectionLength to 0 would be sufficient, but it wasn't. It probably works from the Load/Shown handler, that's really the only place I like.

    I tend to agree with the first paragraph in JMC's post #3, but the problem is you don't want the dang thing selected at startup.

    So probably the best solution is to leave TabStop/TabIndex alone, handle the Shown event, and set SelectionLength on each control to 0. HOWEVER. That only works if you set the values before you display the darned thing, I have a bad feeling a different order causes it to behave a different way.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    459

    Re: Highlighted textboxes

    I have already sabotaged my form in so many different ways that it is no longer possible for me to keep track. This particular form is the most important form in the whole application, since almost everything that happens is through this form. I have cleaned up most of the sabotage I have inflicted on myself and the form is getting pretty clean.

    I no longer believe in magic solutions to any problem. My experience, even before doing this, was that any solution (magic or otherwise) always comes with a cost. In playing around with this I have pretty much figured out what is going on, and more importantly, why it is going on. Additionally, I have looked at some other applications (not the code) available to me and have observed that the standard is that in a focused control the data in the control is highlighted. While that was not my preference, I now understand the purpose (it allows the user to type into the focused textbox and just type over and replace the highlighted text) and accept that it is probably a better way.

    Bottom line, is that for this exercise I did end up going with the SelectionLength = 0, for every control that is available for the user to change. That way the only control that is highlighted is the control that is in focus, which I believe to be closer to how most forms would operate. Based on operating the form, after setting SelectionLength = 0 in all the controls that the user has access to, it appears that only the control in focus (and already containing data) is highlighted, which seems to be as everything should be. For those controls the tabstop = True and each has a TabIndex value based on the order I want tabbing to occur. All other controls are enabled = False (or readOnly = True for text boxes), with TabStop = False

    I still have a couple of questions about comboboxes, but I need to gird my loins prior to going through the pain of finding out how wrong all my assumptions and usage are and will save that for another day.

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: [RESOLVED] Highlighted textboxes

    Yeah the most curious thing to me in the screenshot is seeing three controls with a highlight at once, that makes the concept of focus confusing. I tried to recreate it and couldn't.

    But I swear I've seen it in apps before so either you haven't done anything wrong or it's an easy trap to fall into.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    459

    Re: [RESOLVED] Highlighted textboxes

    Yeah, I hated that. I think it relates to the comboboxes. Textboxes seem to be pretty straight forward. I have always had all sorts of problems with formatting, and frequently, working with comboboxes. I do not usually have TabStops/TabIndexes set for comboboxes, but even then, they do funny things with highlights, etc. Most of my property settings are done in the code, rather than in the properties windows and I am sure that plays into these kinds of things showing up.

    The only thing easy about comboboxes is setting them up to be data bound to work with a table. Beyond that, they are absolutely NOT my favorite control.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,624

    Re: [RESOLVED] Highlighted textboxes

    Its also interesting the way it normally works by default.
    I'm fairly used to tabbing with my left hand to a textbox, and if the contents are autoselected, hitting an arrow key with my right hand to deselect it before appending text (assuming appending text is what I want).

    But the autoselecting of the text when you tab to it is based on whether the user has ever done a keypress in the textbox or not.
    If the textbox text is some default value when the form comes up, then when you tab to it, the text will be autoselected, if you tab around and come back to the same box, the text will still be autoselected. But if you hit the arrow key, as I usually do, to unselect the text, then when you tab around the text will no longer be autoselected. Likewise, if the user edited the text, when they came around through tabbing it would not be autoselected, the cursor would of course still be displayed where it was the last. If you updated the text from code, e.g. textbox1.text = "A test", then the next time you tabbed to the textbox it would autoselect the text again, ready to replace what the computer put in the box with what the user wants.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width