CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2005
    Posts
    180

    How do you add content to a tab control dynamically in wcf -- at run time?

    How do you add content to a tab control dynamically in wcf -- at run time? I mean add content to the display area that comes visibile when clicking on the tab control.

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How do you add content to a tab control dynamically in wcf -- at run time?

    very simple example. this simply adds a new TextBlock to the selected tab each time the tab is changed...

    XAML
    Code:
    <Window x:Class="WpfApplication6.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <TabControl 
                Height="212" 
                HorizontalAlignment="Left" 
                Margin="58,35,0,0" 
                Name="tabControl1" 
                VerticalAlignment="Top" 
                Width="332" 
                SelectionChanged="tabControl1_SelectionChanged">
                
                <TabItem 
                    Header="tabItem1" 
                    x:Name="tabItem1" >
                    
                    <Grid>
                        <StackPanel 
                            x:Name="tab1StackPanel" 
                            Orientation="Vertical">
                            
                        </StackPanel>
                    </Grid>
                </TabItem>
                <TabItem 
                    Header="tabItem2" 
                    x:Name="tabItem2">
                    
                    <Grid>
                        <StackPanel 
                            x:Name="tab2StackPanel" 
                            Orientation="Vertical">
                            
                        </StackPanel>
                    </Grid>
                </TabItem>
            </TabControl>
        </Grid>
    </Window>

    Code:
    Code:
    public partial class MainWindow : Window
    {
        int counter = 1;
    
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            TabControl tc = e.OriginalSource as TabControl;
                
            TextBlock tb = new TextBlock();
            tb.Width = 100;
            tb.Height = 30;
            tb.Text = counter.ToString();
    
            if (tc.SelectedIndex == 0)
            {
                tab1StackPanel.Children.Add(tb);
            }
            else
            {
                tab2StackPanel.Children.Add(tb);
            }
    
            counter++;
        }
    }
    ===============================
    My Blog

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How do you add content to a tab control dynamically in wcf -- at run time?

    I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.

  4. #4

    Re: How do you add content to a tab control dynamically in wcf -- at run time?

    Quote Originally Posted by BigEd781 View Post
    I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.
    It bugs me just a little when I see people do that too. I guess it just comes down to which exception you want to hunt down null object or InvalidCast, either one provide about the same information I guess, but I prefer getting an InvalidCast if I made an invalid cast. Actually for most UI callbacks I use the as operator and then if it is null I return doing nothing but writing an error into the application log. My theory is whatever the user was trying to do was not going to work out to well anyway so I might as well just do nothing.
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: How do you add content to a tab control dynamically in wcf -- at run time?

    Quote Originally Posted by BigEd781 View Post
    I am curious as to why you use a safe cast instead of an explicit cast when you are not che3cking for null afterward? Just wondering.
    because this is sample code and not something going into a production application. When I post code samples, I normally let the people I am doing it for deal with catching exceptions. Do I have to do everything for them?

    I also don't have the time to catch every single exception that might occur when I am simply giving them sample code that will do what they need it to do.
    Last edited by eclipsed4utoo; July 1st, 2010 at 06:35 AM.
    ===============================
    My Blog

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured