Stopping A Control From Getting Focus

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1617 Views - Last Post: 14 April 2013 - 08:35 PM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Stopping A Control From Getting Focus

Posted 30 March 2013 - 03:17 AM

So I have a richtextbox and I have a combobox where I want the user to be able to change the fontsize of the selected text within the RTB.

The problem is that as soon as you click the COMBOBOX the RTB immediately loses focus AND un-selects whatever text was selected in the RTB to begin with and highlights what's in the drop down.

How can you prevent this from happening? Modern text editors like Wordpad etc. keep this from happening all the time. Does anyone know the "secret"? Thanks guys

Is This A Good Question/Topic? 0
  • +

Replies To: Stopping A Control From Getting Focus

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 03:38 AM

Take a look here:

http://stackoverflow...ol-unfocussable

I think that's what you're looking for.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 03:59 AM

I'm not sure that there is a straight-forward solution, or property, for this - the page linked-to in the previous post seems to refer specifically to a user- (custom) control. (Of course, you might consider creating such a custom-control.)

Otherwise, I would suggest using the Leave event for the RTB to store the Selectionstart and SelectionLength properties of this RTB.
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 04:09 AM

Added: Actually, Selectionstart and SelectionLength are available anyway, even though the text isn't shown as highlighted in the RTB. You can prove this by tabbing through all the controls, after selecting something from the ComboBox. So I think this shouldn't really be an issue.

BTW I'm not sure that WordPad would qualify as a modern text editor :dontgetit:
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 06:43 AM

Actually, it would qualify if you think in terms for "freshness". The code was written from scratch for Win95, and rewritten from for Vista when the Ribbon UI came along. Along the way it has picked up more features than the previous version.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 08:21 AM

View PostSkydiver, on 30 March 2013 - 01:43 PM, said:

Actually, it would qualify if you think in terms for "freshness". The code was written from scratch for Win95, and rewritten from for Vista when the Ribbon UI came along. Along the way it has picked up more features than the previous version.

I've been using WordPad for a while now, in preference to Word: nice and straight-forward :balloon:
Was This Post Helpful? 1
  • +
  • -

#7 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 12:14 PM

View Postandrewsw, on 30 March 2013 - 04:09 AM, said:

Added: Actually, Selectionstart and SelectionLength are available anyway, even though the text isn't shown as highlighted in the RTB. You can prove this by tabbing through all the controls, after selecting something from the ComboBox. So I think this shouldn't really be an issue.

BTW I'm not sure that WordPad would qualify as a modern text editor :dontgetit:/>


You're correct it's not an issue if a user just selects a new index but if they type in a new size in the combo box it immediately un-selects what was selected in the RTB
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 12:26 PM

View Postadn258, on 30 March 2013 - 07:14 PM, said:

You're correct it's not an issue if a user just selects a new index but if they type in a new size in the combo box it immediately un-selects what was selected in the RTB

Then you might revert to my first suggestion: use the Leave event of the RTB to store class-level variables for the Selectionstart and SelectionLength, that you can then refer to in combobox events.
Was This Post Helpful? 1
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 12:59 PM

The following kinda works, but it probably needs a little effort to find the right combination of events:

    public partial class Form1 : Form {
        private int rtbSelStart = 0;
        private int rtbSelLength = 0;

        private void richTextBox1_Leave(object sender, EventArgs e) {
            rtbSelStart = richTextBox1.Selectionstart;
            rtbSelLength = richTextBox1.SelectionLength;

        }

        private void comboBox1_TextChanged(object sender, EventArgs e) {
            
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e) {
            richTextBox1.Select(rtbSelStart, rtbSelLength);
            richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.Name, 
                Convert.ToSingle(comboBox1.Text));
        }

Was This Post Helpful? 1
  • +
  • -

#10 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 01:05 PM

I used the TextChanged event as well, but this changes for every single character change, so I haven't quite found the perfect combination.
Was This Post Helpful? 1
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 03:03 PM

When I had to deal with this issue, I just set HideSelection to false and decided not to fight the way Windows works.

http://msdn.microsof...eselection.aspx

Notice that WordPad does the same thing. Select some text, hit the Start button (notice that the selection stays visible), and then start up calc. Again notice that the focus is in calc, but the selection remains visible.
Was This Post Helpful? 1
  • +
  • -

#12 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 03:23 PM

View PostSkydiver, on 30 March 2013 - 10:03 PM, said:

When I had to deal with this issue, I just set HideSelection to false and decided not to fight the way Windows works.

http://msdn.microsof...eselection.aspx

Notice that WordPad does the same thing. Select some text, hit the Start button (notice that the selection stays visible), and then start up calc. Again notice that the focus is in calc, but the selection remains visible.

Thanks for pointing out HideSelection, but is this really fighting against Windows behaviour? A lot of applications remember what was selected; admittedly, a lot of the time this is while another dialog is in use, but still..
Was This Post Helpful? 1
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 09:17 PM

I don't have the Windows UI style guide handy, but what I recall is that the original Windows 3.x style guide says that programs should hide the focus caret, and any current selection indicators when when the focus is placed elsewhere. Notice that this is why the default edit control behavior is to hide the selection when the focus moves away. The softening of that rule came along when Word was published. Word broke the rules, but it was okay because it was a cash cow for MS and it could make up its own UI rules.
Was This Post Helpful? 0
  • +
  • -

#14 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: Stopping A Control From Getting Focus

Posted 30 March 2013 - 10:10 PM

View PostSkydiver, on 30 March 2013 - 09:17 PM, said:

I don't have the Windows UI style guide handy, but what I recall is that the original Windows 3.x style guide says that programs should hide the focus caret, and any current selection indicators when when the focus is placed elsewhere. Notice that this is why the default edit control behavior is to hide the selection when the focus moves away. The softening of that rule came along when Word was published. Word broke the rules, but it was okay because it was a cash cow for MS and it could make up its own UI rules.



Your method here works best. This works fine but for some odd reason and this is also an annoying problem. There are a list of font sizes of course but when a user types in a custom font size in that combo box it isn't using that font size. The event of textchanged seems to do nothing and I don't know why because the event definitely fires.

The problem I have is very annoying because I have to take the value in the toolstrip combobox and convert it to an integer and cast it to a float for the stupid text size property of the Richtext box. You have to be careful using something like

if (this.tlstrp_cmbobx_font_size.SelectedItem != null)
            {
                string strCmbobx = this.tlstrp_cmbobx_font_size.Text;
                int new_size = Convert.ToInt32(strCmbobx);
                this.Get_Current_AdvRTB().SelectionFont = new Font(this.rtb1.SelectionFont.FontFamily.Name, (float)new_size);
            }



because even if I use the conditional statement like if
(this.tlstrp_cmbobx_font_size.Text.Length > 0)




incorrect types happen and the program crashes. In any case it's a confusing problem trying to get the program to accept values as font size that are typed into the combobox.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: Stopping A Control From Getting Focus

Posted 31 March 2013 - 02:20 AM

You might use TryParse to prevent it from crashing, or other error-handling/checking techniques.

However, it would be better initially to use debugging techniques to discover what the values are, that are causing the crash (see my signature). It is tricky, particularly with comboboxes, to get the right combination of events and properties.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2