Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6652029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:04:59+00:00 2026-05-26T01:04:59+00:00

I am doing something like the following to prevent a tab selection from changing:

  • 0

I am doing something like the following to prevent a tab selection from changing:

 tabControl.Items.CurrentChanging += new CurrentChangingEventHandler(Items_CurrentChanging);

 void Items_CurrentChanging(object sender, CurrentChangingEventArgs e)
 {
     if( dataIsInvalid )
     {
          // Show some popup message
          var item = ((ICollectionView)sender).CurrentItem;
          e.Cancel = true;
          tabControl.SelectedItem = item; // !! This causes the CurrentChanging event to happen twice !! 
                                          // But without this the visual tree does not update! :( 
     }
}

However the problem that I am seeing right now is that the last line above causes a CurrentChanging event to happen a second time, and my popup message is displayed twice. Not only that, it is displayed the second time after bringing another window into focus first, and then putting focus back on my tab control’s window.
Any ideas why this could be happening?

–EDIT–
It looks like I should really only need e.Cancel and not require updating the SelectedItem again. However the visual tree is not updated unless I do this. Is there any way I can ensure the visual tree of the tab control is updated after e.Cancel occurs, without having to update the SelectedItem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:05:00+00:00Added an answer on May 26, 2026 at 1:05 am

    Yes this is a little wierd… you can deterministically unhook and hook the current changing event handler in the PreviewMouseDown on tab control.

    And in CurrentChanging itself unhook the handler so this way it will not be called multiple times atleast.

        private void MyTabControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var tabControl = sender as TabControl;
    
            if (tabControl != null)
            {
                var temp = new CurrentChangingEventHandler((sender1, e1) => { });
                var handler = new CurrentChangingEventHandler(
                    (sender1, e1) =>
                        {
                            var item = ((ICollectionView) sender1).CurrentItem;
                            if (item != null && dataIsInvalid)
                            {
                                e1.Cancel = true;
                                Dispatcher.BeginInvoke(
                                    new Action(() =>
                                       {
                                           tabControl.SelectedItem = item;
                                       }));
                            }
    
                            tabControl.Items.CurrentChanging -= temp;
                        });
    
                temp = handler;
                tabControl.Items.CurrentChanging -= handler;
                tabControl.Items.CurrentChanging += handler;
            }
        }
    

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume you are doing something like the following List<string> myitems = new List<string> {
I'm doing something like the following: SELECT * FROM table WHERE user='$user'; $myrow =
I'm doing something like following to transfer value from PHP to javascript: var str
If you are doing something like the following: var i = $('input[@name=some_field]'); if (i.val()
I am doing something like this: class Class(object): def __init__(self): self.var=#new instance name string#
In rails is there a way of doing something like the following... if @var
I have seen many references to doing something like the following to insert a
I have a program doing something like the following: class SomeObject{} { void function(int
Let's say I'm doing something like the following, and I want to test to
I'm doing something like the following: Bitmap mBitmap; mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.myimage); In onDraw

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.