Click here to Skip to main content
16,022,060 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team

I have textbox and want to only be enabled when its admin and superadmin for security reason, when the page loads after login, this logic is not working and did set the debug does goes in but the problem is textbox is not not editable for these roles.

What I have tried:

TextBox 
                		VerticalAlignment="Center"
                		Foreground="Black"
                        Background="White"
                        Width="268"
                		FontWeight="SemiBold"
                		FontSize="36" TextAlignment="Right" Height="55" Margin="1,1,1,1" Padding="2,2,2,2" HorizontalAlignment="Center" x:Name="txtWeighbridgeWeight" TextChanged="txtWeighbridgeWeight_TextChanged"/>
// front end for textbox name

public MainWindow()
        {
            try
            {
                InitializeComponent();

                if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                {
                    lblPath.Content = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsolutePath;
                }

                dispatcherTimer = new DispatcherTimer();
                dispatcherTimer.Tick += dispatcherTimer_Tick;
                dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
                dispatcherTimer.Start();
                panelWidth = sidepanel1.Width;
                panelWidth = sidePanel.Width;

                User user = new User();
                LabAssistant user1 = new LabAssistant();

                if (Properties.Settings.Default.Quality == "Q")
                {
                    user1 = Managers.ManageLabAssistant.GetUserByPK(Properties.Settings.Default.LoggedInUserID);
                    txtLoggedInUser.Text = user1.UserName;
                }
                else
                {
                    user = Managers.UserManager.GetUserByPK(Properties.Settings.Default.LoggedInUserID);
                    txtLoggedInUser.Text = user.Username;
                }

                txtStation.Text = Properties.Settings.Default.Station;

                if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
                {
                    this.txtVersion.Text = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
                }

                var menuWB = new List<SubItem>();
                menuWB.Add(new SubItem("Raw Material", new UCRaw_Material()));
                menuWB.Add(new SubItem("Finished Product", new UCFinishedProduct()));
                var item0 = new ItemMenu("Weighbridge", menuWB, PackIconKind.Truck);


                //var menuSchedule = new List<SubItem>();
                //var item1 = new ItemMenu("Finished Product", menuSchedule, PackIconKind.TruckDelivery);

                var menuReports = new List<SubItem>();
                menuReports.Add(new SubItem("Customer", new UCManageCustomer()));
                menuReports.Add(new SubItem("Client", new UCManageClients()));
                menuReports.Add(new SubItem("Bin", new UCManageBin()));
                menuReports.Add(new SubItem("Drivers", new UCManageDriver()));
                menuReports.Add(new SubItem("Farm", new UCManageFarm()));
                menuReports.Add(new SubItem("Material", new UCManageMaterial()));
                menuReports.Add(new SubItem("Product", new UCManageProduct()));
                menuReports.Add(new SubItem("Supplier", new UCManageSupplier()));
                menuReports.Add(new SubItem("Vehicle", new UCManageVehicle()));
                menuReports.Add(new SubItem("Batch Material", new UCManageBatchMaterial()));
                menuReports.Add(new SubItem("Feed Production Schedule", new UCFeedproductionSchedule()));
                menuReports.Add(new SubItem("Feed Production Requirements", new UCOrderRequirements()));

                menuReports.Add(new SubItem("Batch Feed Format", new UCManageBatchFeedFormat()));
              //  menuReports.Add(new SubItem("Users", new UCManageUsers()));

                //menuReports.Add(new SubItem("RM Code to Stock Code", new UCManageRM_Stock()));
                menuReports.Add(new SubItem("Status", new UCStatusScreen()));
                menuReports.Add(new SubItem("Recipes", new UCManageRecipe()));
                menuReports.Add(new SubItem("Batch Recipes", new UCManageBatchRecipe()));
                menuReports.Add(new SubItem("Configuration", new UCConfigScreen()));
                // menuReports.Add(new SubItem("Scale Settings", new UCScaleSettings()));
                menuReports.Add(new SubItem("Reports", new UCManageReports()));
                var item2 = new ItemMenu("Administration", menuReports, PackIconKind.AccountCog);


                var menuQC = new List<SubItem>();
                menuQC.Add(new SubItem("Crumble", new UCManageQcCrumbleData()));
                menuQC.Add(new SubItem("Pellet", new UCManagePelletData()));
                menuQC.Add(new SubItem("Grist", new UCManageGrist()));
                //       menuQC.Add(new SubItem("Users", new UCLabAssistant()));
                menuQC.Add(new SubItem("Reports", new UCQualityReports()));


                var item3 = new ItemMenu("Quality Control", menuQC, PackIconKind.Controller);




                if (user.UserTypeID == Convert.ToInt32(Enum.UserType.Admin))
                {
                    Menu.Children.Add(new UserControlMenuItem(item0, this));
                    Menu.Children.Add(new UserControlMenuItem(item2, this));
                    Menu.Children.Add(new UserControlMenuItem(item3, this));
                    menuQC.Add(new SubItem("Users", new UCLabAssistant()));
                    menuQC.Add(new SubItem("Supervisor", new UCQCManageSupervisor()));
                    menuWB.Add(new SubItem("Reports", new UCManageSupervisorReports()));
                    txtWeighbridgeWeight.IsEnabled = true; // this not working

                }
                if(user.UserTypeID == Convert.ToInt32(Enum.UserType.Admin))
                {
                    txtWeighbridgeWeight.IsEnabled = true;// this is not working, cant edit any value when page is loaded as an admin and Super-User
                    txtWeighbridgeWeight.IsReadOnly = false;
                }

                if (user.UserTypeID == Convert.ToInt32(Enum.UserType.SuperUser))
                {
                    Menu.Children.Add(new UserControlMenuItem(item0, this));
                    Menu.Children.Add(new UserControlMenuItem(item2, this));
                    Menu.Children.Add(new UserControlMenuItem(item3, this));
                    menuReports.Add(new SubItem("Users", new UCManageUsers()));
                    menuQC.Add(new SubItem("Users", new UCLabAssistant()));
                    menuQC.Add(new SubItem("Supervisor", new UCQCManageSupervisor()));
                    menuWB.Add(new SubItem("Reports", new UCManageSupervisorReports()));
                    txtWeighbridgeWeight.IsEnabled = true; // this gets called first



                } if(user.UserTypeID == Convert.ToInt32(Enum.UserType.SuperUser))
                {
                    txtWeighbridgeWeight.IsEnabled = true; // here is not working
                }

                if (user.UserTypeID == Convert.ToInt32(Enum.UserType.User))
                {
                    Menu.Children.Add(new UserControlMenuItem(item0, this));
                }


                if (user.UserTypeID == Convert.ToInt32(Enum.UserType.Supervisor))
                {
                    Menu.Children.Add(new UserControlMenuItem(item0, this));
                    menuWB.Add(new SubItem("Reports", new UCManageSupervisorReports()));
                }

                // inventory controller logic implemented
                if(user.UserTypeID == Convert.ToInt32(Enum.UserType.InventController))
                {
                    Menu.Children.Add(new UserControlMenuItem(item0, this));
                    menuWB.Add(new SubItem("Reports", new UCManageReports()));
                }
                else
                {
                    txtWeighbridgeWeight.IsReadOnly = true;
                }

                if (user1.UserLevelID == Convert.ToInt32(Enum.UserType.Admin))
                {
                   // Menu.Children.Add(new UserControlMenuItem(item0, this));
                   // Menu.Children.Add(new UserControlMenuItem(item2, this));
                    Menu.Children.Add(new UserControlMenuItem(item3, this));
                    menuQC.Add(new SubItem("Users", new UCLabAssistant()));

                }
                else
                {

                    // txtWeighbridgeWeight.IsReadOnly = true;
                }
                if (user1.UserLevelID == Convert.ToInt32(Enum.UserType.User))
                {
                    Menu.Children.Add(new UserControlMenuItem(item3, this));

                }
                else
                {
                    txtWeighbridgeWeight.IsReadOnly = true; 
                }

                if (user.UserTypeID == Convert.ToInt32(Enum.UserType.Admin) || user.UserTypeID == Convert.ToInt32(Enum.UserType.User)|| user.UserTypeID == Convert.ToInt32(Enum.UserType.Supervisor) || user.UserTypeID == Convert.ToInt32(Enum.UserType.SuperUser) || user.UserTypeID == Convert.ToInt32(Enum.UserType.InventController))
                {
                    if (Properties.Settings.Default.Station.ToUpper() != "BOTTOM")
                    {
                        UCRaw_Material uc = new UCRaw_Material();

                        SwitchScreen(uc);
                    }
                    else
                    {
                        UCFinishedProduct uc = new UCFinishedProduct();
                        SwitchScreen(uc);
                    }
                }
                if (user1.UserLevelID == Convert.ToInt32(Enum.UserType.Admin) || user1.UserLevelID == Convert.ToInt32(Enum.UserType.User))
                {
                    if (Properties.Settings.Default.Station.ToUpper() != "BOTTOM")
                    {
                        UCManageQcCrumbleData uc = new UCManageQcCrumbleData();
                        SwitchScreen(uc);
                    }
                    else
                    {
                        UCManagePelletData uc = new UCManagePelletData();
                        SwitchScreen(uc);
                    }
                }

                //UCManageRecipe uc = new UCManageRecipe();
                //SwitchScreen(uc);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "/n" + ex.InnerException);
            }
        }
Posted
Comments
Richard Deeming 11-Jun-24 3:29am    
205 lines of unexplained, and mostly irrelevant code, and you couldn't even tell us which line(s) you're having problems with?

Edit your question and remove the irrelevant code. Show just the parts that deal with enabling or disabling the control, and explain precisely what you have tried and where you are stuck.
Gcobani Mkontwana 14-Jun-24 7:37am    
Richard Deeming, the trick was for me to use infinite loop for those who can have access and those who may likely not to have access and enable txt to have full right to true, vice versa and its working
HobbyProggy 19-Jun-24 9:02am    
That still really doesn't answer or clarify what Richard asked for, sorry to say but that code is a mess and to me it seems the problem is because you verify each user id with an if else, hence enabling the texbox and later on disabling it because another check overwrites what the admin check has set.

Best guess, clean the code up and show us what your problem is.

1 solution

The easiest thing to do:

0) Create a bool EnabledForUser property.

1) Bind the TextBox.IsEnabled attribute (in the XAML) to the EnabledForUser property.

IsEnabled={Binding Path=EnabledForUser}


2) Add a Loaded event handler that checks the role of the logged in user, and sets the EnabledForUser property.

WPF should do everything for you if you follow these steps. I'm assuming you know how to use WPF's DataContext/binding, as well as how to wire p properties.

Keep in mind that I did not wade through your posted code, and I'm not going to.
 
Share this answer
 
v3

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