Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi All,

I am using a wpf TabControl, in which I have several TabItems and I added the Display Text property to the TabItem with mnemonics. But In a TabItem am having WebBrowser Control in the TabContent.

The problem is, while pressing a key in the webbrowser's page textbox, it triggers the accelerator key and navigating to the tabitem. For Example, See the code below...


<tabcontrol>

<tabitem displaytext="_Veritas"></tabitem>

<tabitem displaytext="_Zeus"></tabitem>

<tabitem displaytext="_Sparta"></tabitem>

</tabcontrol>




In Sparta TabItem I have a webbrowser control and lets say I have a google search page in it... While am pressing the key V or Z its navigating to the tab Veritas or Zeus respectively with out pressing Alt Key. So I could not able to type in the google search textbox effectively.

How can I restrict this?

Thanks and Regards
PMM :)
Posted

This is perfectly accepted behavior which should not prevent access to any other control. The behavior However, you need to resolve key characters (prefixed with "_") used in labels.

Please understand, there is no such thing as miracle. Imagine that you want "Alt+Z" to activate "Zeus" when it is visible, and "Z" to select "Zeus" when some other tab in the same TabControl is selected. This is standard and expected behavior. Is, at the same time, you need "Alt+Z" or "Z" to focus some control like a text box, you should understand that a computer cannot know you real intent: you should choose between focusing a control and selection of the tab.

Let me illustrate this on a XAML sample:

XML
<TabControl>
   <TabItem header="_Veritas">
      <DockPanel LastChildFill="True">
         <Label
             Target="{Binding ElementName=tb}"
             DockPanel.Dock="Top">_Access text box:</Label>
         <TextBox name="tb"></TextBox>
      </DockPanel>
   </TabItem>
   <TabItem header="_Zeus"></TabItem>
   <TabItem header="_Sparta"></TabItem>
</TabControl>


(By the way: why would you post code which does not compile? I know one reason: you cannot use <> in HTML, should replace them with HTML character entities, but why "displaytext"?)

The code above works perfectly: You have Alt+Z, Alt+S, Alt+V and Alt+A to navigate between tabs. If one of the tabs is not only selected but the TabControl is also focused (know the difference between selection and focusing!), you can also use just "Z", "S", or "V" for tab selection.

However, if you replace "_Access text box" with "Let's test _Z" you will loose the effect of "Alt+Z" on focusing of the text box, because this key stroke will shift tab selection (this is also indirectly shifts focus if the text box was focused, because it gets invisible). You cannot expect the same key stroke to do different things depending on what the user wants :-).

If you think that the action of shifting of the tab selection by a key stroke is redundant or less important compared to focusing on search pattern edit box, think again. This is very logical and standard behavior. If you manage to modify it, you only confuses the user. The best solution is finding the accurate set of the characters used the the "_" prefix.

Of course, you can disable tab selection on keystroke simply by removing the "_" prefix before the "conflicting" character, but I would not recommend it. You can achieve the best UI design if you provide logical standard navigation via keyboard with really quick access to all the control via just the keyboard. Any key combinations which are not intuitively obvious should be avoided.

—SA
 
Share this answer
 
v7
I solved it by adding a eventhandler and

<window x:class="TabTest.MainWindow" xmlns:x="#unknown"><br />
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"<br />
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"<br />
        Title="MainWindow" Height="350" Width="525"<br />
         AccessKeyManager.AccessKeyPressed="AccessKeyPressed"></window>


and handle all events which don't have a alt modifier.

        <br />
private void AccessKeyPressed(object sender, AccessKeyPressedEventArgs e) {<br />
                if ((Keyboard.Modifiers & ModifierKeys.Alt) != ModifierKeys.Alt) {<br />
                    e.Target = null;<br />
                    e.Handled = true;<br />
                }<br />
        }
 
Share this answer
 
C#
Hi,

 I tried this solution with my application , it works with single underscore like  'test_1'

But if i put two underscore like 'test__1' in tab name , it only displays one underscore. For three underscore it displays two underscore.


please help me out.
 
Share this answer
 
Comments
CHill60 1-Feb-16 10:24am    
If you have a question of your own then use the "Ask a Question" link. Alternatively use the "Have a Question or Comment" link next to a solution so that the poster is notified of your response.
Don't post questions or comments as solutions to old posts.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900