Search code examples
c#wpftabscontrolstabitem

Login and tab control visibility


I am working a program in C#. I am trying to make a simple login program. For example when the details of the user are correct then show the content of tab item, if are not correct then don't show the content of tab item. the code is under

    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="60"/>
        <RowDefinition Height="60"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="1" Orientation="Horizontal">
        <Label Name="lbluser" Content="User" Height="30" Width="50" />
        <TextBox Name="txtuser" Width="180" Height="30"/>

    </StackPanel>
    <StackPanel Grid.Row="2" Orientation="Horizontal">
        <Label Name="lblpass" Content="Password" Height="30"/>
        <PasswordBox Name="psw" Height="30" Width="180"/>
    </StackPanel>
    <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center">
        <Button Name="btnclick" Content="Click" Width="80" Height="30" Click="btnclick_Click" />
        <Button Name="btncancel" Content="Cancel" Width="80" Height="30" Margin="10,0,0,0" Click="btncancel_Click" />
        <Button Name="btnclose" Content="Close" Width="80" Height="30" Margin="10,0,0,0" Click="btnclose_Click" />
    </StackPanel>
    <StackPanel Grid.Row="4" >
        <TextBox Name="txtres" Height="30" Width="200"/>
    </StackPanel>
    <StackPanel Grid.Row="5">
        <TabControl Margin="0,10,0,0">
            <TabItem Header="Tab I" >
                <StackPanel>
                    <TextBox Name="txt1" Width="250" Height="30"/>
                    <Button Name="btn1" Width="80" Height="30" Content="Display"/>
                </StackPanel>
            </TabItem>
        </TabControl>
    </StackPanel>

the behind code

     {
        InitializeComponent();
    }

    private void btnclick_Click(object sender, RoutedEventArgs e)
    {
        {
            if (txtuser.Text == "TEST" && psw.Password == "TEST")
            {
                txtres.Text = "   You are logged in";
                txtres.Foreground = Brushes.Green;
                txtres.FontSize = 14;
                MessageBox.Show("You are logged in");
            }

            else
            {
                txtres.Text = "    You are not logged in";
                txtres.Foreground = Brushes.Red;
                txtres.FontSize = 14;
                MessageBox.Show("You are not logged in");
            }
        }
    }

    private void btncancel_Click(object sender, RoutedEventArgs e)
    {
        txtuser.Text = "";
        psw.Password = "";
    }

    private void btnclose_Click(object sender, RoutedEventArgs e)
    {
        Application.Current.Shutdown()
    }
}
    }

Solution

  • If you are having logic in code-behind, you can easily access instances of UI elements.

    You can set a name to your Tab Item:

    <TabItem x:Name="LoginSuccessTab" Header="Tab I"  >
    

    And then, hide it when login fails

    private void btnclick_Click(object sender, RoutedEventArgs e)
        {
            {
                if (txtuser.Text == "TEST" && psw.Password == "TEST")
                {
                    txtres.Text = "   You are logged in";
                    txtres.Foreground = Brushes.Green;
                    txtres.FontSize = 14;
                    MessageBox.Show("You are logged in");
                    LoginSuccessTab.Visibility = Visibility.Visible;
                }
    
                else
                {
                    txtres.Text = "    You are not logged in";
                    txtres.Foreground = Brushes.Red;
                    txtres.FontSize = 14;
                    MessageBox.Show("You are not logged in");
                    LoginSuccessTab.Visibility = Visibility.Collapsed;
                }
            }
        }