Hidden add sheets button in C1FlexSheet

Posted by: andrew on 25 November 2019, 2:51 am EST

    • Post Options:
    • Link

    Posted 25 November 2019, 2:51 am EST

    Hi,

    I am using C1 FlexSheet to display my excel file. Everything is fine but i want to hide the “add sheet” button at the bottom and the content menu after clicking the right click on the sheet tool bar. Also, i would like to disable users to rename the sheet name.

    Would you please introduce some ways to do that?

    Best regards,

    Andrew

  • Posted 27 November 2019, 4:18 am EST

    Hello Andrew,

    the content menu after clicking the right click on the sheet tool bar

    Please use the following lines of code:

     c1FlexSheet1.EditOptions = C1.WPF.FlexGrid.FlexSheetEditOptions.None;
    

    i want to hide the “add sheet” button at the bottom

    Please use the following line of code:

    private void C1FlexSheet1_Loaded(object sender, RoutedEventArgs e)
          {
              foreach (NewSheet item in FindVisualChildren<NewSheet>(c1FlexSheet1))
              {
                  item.Visibility = Visibility.Collapsed;
                  TextBlock tsl;
                
    
              }
             
           
          }
    
    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
          {
              if (depObj != null)
              {
                  for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                  {
                      DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                      if (child != null && child is T)
                      {
                          yield return (T)child;
                      }
    
                      foreach (T childOfChild in FindVisualChildren<T>(child))
                      {
                          yield return childOfChild;
                      }
                  }
              }
          }
    

    i would like to disable users to rename the sheet name.

    Please use the following lines of code:

     private void C1FlexSheet1_Loaded(object sender, RoutedEventArgs e)
            {
                
              ContentPresenter itemTab = FindChild<ContentPresenter>(c1FlexSheet1, "TabPanelPresenter");
    
                foreach (TextBlock item in FindVisualChildren<TextBlock>(itemTab))
                {
                    item.IsEnabled = false;
                  
    
    
                }
            }
    
    public static T FindChild<T>(DependencyObject parent, string childName)
        where T : DependencyObject
            {
                // Confirm parent and childName are valid. 
                if (parent == null) return null;
                T foundChild = null;
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    // If the child is not of the request child type child
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        foundChild = FindChild<T>(child, childName);
                        // If the child is found, break so we do not overwrite the found child. 
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        // If the child's name is set for search
                        if (frameworkElement != null && frameworkElement.Name == childName)
                        {
                            // if the child's name is of the request name
                            foundChild = (T)child;
                            break;
                        }
                    }
                    else
                    {
                        // child element found.
                        foundChild = (T)child;
                        break;
                    }
                }
                return foundChild;
            }
    

    Hope it helps.

    Thanks,

    Mohit

  • Posted 28 November 2019, 12:00 am EST

    Dear Mohit,

    Thanks for your help! Those are useful. Hidden the content menu and Hidden the Add Sheet Button works for me. However, i could only disable editing name at the first Sheet. The other sheet are still able to rename.

    My steps:

    1. Initiating a empty c1FlexSheet
    2. Importing a excel by c1FlexSheet.Load(stream, imortFormat) function.
    3. then do call DisableRename() as a method but not a Event handler
    
    public MainWindow(){
    InitializeComponent();
    c1FlexSheet.Load(stream, imortFormat);
    this.DisableRename();
    }
    
    private void DisableRename() {
    			ContentPresenter itemTab = FindChild<ContentPresenter>(this.excelViewer, "TabPanelPresenter");
    
    			foreach(TextBlock item in FindVisualChildren<TextBlock>(itemTab)) {
    				item.IsEnabled = false;
    			}
    		}
    
    

    And i have several question here :

    1. Dose after c1FlexSheet.Load(stream, imortFormat) trigger C1FlexSheet1_Loaded event?

    2. Does the code you mention above about disabling rename have to do in C1FlexSheet1_Loaded event?

    Is there anything I should be aware of?

    Best regards,

    Andrew

  • Posted 28 November 2019, 3:14 am EST

    Hello,

    You can use the following code to disable the rename for only one first sheet:

    foreach (Sheet item in FindVisualChildren<Sheet>(test))
               {
                   if( !item.GetType().Equals(typeof(NewSheet)))
                   {
                       foreach (TextBlock textbox in FindVisualChildren<TextBlock>(item))
                       {
                           textbox.IsEnabled = false;
                       }
                       break;
                   }
                 
               }
    

    Dose after c1FlexSheet.Load(stream, imortFormat) trigger C1FlexSheet1_Loaded event?

    Loaded event trigger when FlexSheet is fully loaded. It has nothing to do with “Load” method

    Does the code you mention above about disabling rename have to do in C1FlexSheet1_Loaded event?

    Yes, because the loaded event trigger when flexsheet is fully rendered. Hence, to iterate the visual tree, it is necessary that flexsheet should be fully loaded.

    Is there anything I should be aware of?

    No, I don’t think so.

    Thanks,

    Mohit

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels