RadToggleButton User vs. Programatic StateChanged Event

6 posts, 1 answers
  1. southwynd
    southwynd avatar
    11 posts
    Member since:
    Jul 2009

    Posted 10 Aug 2009 Link to this post

    I have a RadGridView that is being used to edit data.  Some of the cells contain bold text. 

    I am attempting to add a RadToggleButton that will be used to bold or unbold text.  When the user selects a row with bold text, I toggle the RadToggleButton to ON.  When the user selects a row not formatted with bold text, I toggle the RadToggleButton to OFF.

    I need to handle the ToggleStateChanged event differently when the button is clicked than when I toggle the state programatically.

    Any suggestions on how to do this?
  2. Robert
    Robert avatar
    82 posts
    Member since:
    Jun 2009

    Posted 10 Aug 2009 Link to this post

    Hey Dwight,

    You could probably handle that situation by doing something like this. My "ToggleStateChangedByButton" event handler is a simulation of what your GridView would be doing when it detects a bold/non-bold cell.
            public Form1() 
            { 
                InitializeComponent(); 
     
                radToggleButton1.ToggleStateChanged += radToggleButton1_ToggleStateChanged; 
            } 
     
            private void radButton1_Click(object sender, EventArgs e) 
            { 
                radToggleButton1.ToggleStateChanged -= radToggleButton1_ToggleStateChanged; 
                radToggleButton1.ToggleStateChanged += radToggleButton1_ToggleStateChangedByButton; 
     
                if (radToggleButton1.ToggleState == ToggleState.On) 
                    radToggleButton1.ToggleState = ToggleState.Off; 
                else 
                    radToggleButton1.ToggleState = ToggleState.On; 
     
                radToggleButton1.ToggleStateChanged += radToggleButton1_ToggleStateChanged; 
                radToggleButton1.ToggleStateChanged -= radToggleButton1_ToggleStateChangedByButton; 
            } 
     
            private void radToggleButton1_ToggleStateChangedByButton(object sender, Telerik.WinControls.UI.StateChangedEventArgs args) 
            { 
                MessageBox.Show("Button Toggled Me!"); 
            } 
     
            private void radToggleButton1_ToggleStateChanged(object sender, Telerik.WinControls.UI.StateChangedEventArgs args) 
            { 
                MessageBox.Show("Click Toggled Me!"); 
            } 

    I hope this helps.

    - Robert
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 13 Aug 2009 Link to this post

    Hi Shoerob,

    You can use the following pattern:

    1. Use a hidden column that determines whether the row fond should be bold or not. Then handle the CellFormatting event to change the font:

    Font boldFont = new Font(SystemFonts.DefaultFont, FontStyle.Bold); 
    Font regularFont = new Font(SystemFonts.DefaultFont, FontStyle.Regular); 
     
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
        if ((bool)e.CellElement.RowInfo.Cells["ShouldBeBold"].Value) 
        { 
            e.CellElement.Font = boldFont; 
        } 
        else 
        { 
            e.CellElement.Font = regularFont; 
        } 

    2. When changing the current row in RadGridView the CurrentRowChanged event fires. Handle this event to synchronize the toggle button state:

    void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e) 
        if (e.CurrentRow is GridViewDataRowInfo) 
        { 
            if ((bool)e.CurrentRow.Cells["ShouldBeBold"].Value) 
            { 
                this.radToggleButton1.ToggleState = ToggleState.On; 
            } 
            else 
            { 
                this.radToggleButton1.ToggleState = ToggleState.Off; 
            } 
        } 

    3. And finally handle ToggleStateChanged event to change the cell font programmatically:

    private void radToggleButton1_ToggleStateChanged(object sender, StateChangedEventArgs args) 
        if (this.radToggleButton1.ToggleState == ToggleState.On) 
        { 
            this.radGridView1.CurrentRow.Cells["ShouldBeBold"].Value = true
        } 
        else 
        { 
            this.radGridView1.CurrentRow.Cells["ShouldBeBold"].Value = false
        } 

    I hope this helps. If you have more questions, we will be glad to help.

    All the best,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. southwynd
    southwynd avatar
    11 posts
    Member since:
    Jul 2009

    Posted 26 Aug 2009 Link to this post

    I'm still having difficulting handling the two types of events so let me rephrase my problem.

    1) ToggleStateChanged by User:
    My application has a RadToggleButton the users click to bold/unbold the selected text in a RadGridView.  By toggling the button ON or OFF, the text is bolded or unbolded.  I want to do something like this:

        Private Sub AdviceBoldToggleButton_ToggleStateChanged(ByVal sender As RadToggleButton, ByVal args As Telerik.WinControls.UI.StateChangedEventArgs) Handles AdviceBoldToggleButton.ToggleStateChanged  
            If AdviceBoldToggleButton.ToggleState = ToggleState.[OnThen  
                AdviceBoldToggleButton.ToggleState = ToggleState.Off  
            Else  
                AdviceBoldToggleButton.ToggleState = ToggleState.[On]  
            End If  
      
            If Me.AdviceBoldToggleButton.ToggleState = ToggleState.[OnThen  
                Me.gvAdvice.CurrentRow.Cells("Format").Value = "BOLD"  
            Else  
                Me.gvAdvice.CurrentRow.Cells("Format").Value = ""  
            End If  
        End Sub  

    2) ToggleStateChanged Programatically:
    When the user selects a row in the RadGridView that has bold text, I programatically toggle the RadToggleButton ON.  Sort of like when using Microsoft Word and you click on text that is bold.  When leaving a row that is bold and going to a row that is not bold, this event fires also.  The code looks like this:

        Private Sub gvAdvice_CurrentRowChanged(ByVal sender As RadGridView, ByVal e As CurrentRowChangedEventArgs) Handles gvAdvice.CurrentRowChanged  
            If TypeOf e.CurrentRow Is GridViewDataRowInfo Then  
                If DirectCast(e.CurrentRow.Cells("Format").Value, String).ToUpper = "BOLD" Then  
                    Me.AdviceBoldToggleButton.ToggleState = ToggleState.[On
                Else  
                    Me.AdviceBoldToggleButton.ToggleState = ToggleState.Off  
                End If  
            End If  
        End Sub 

    However, when I toggle programmatically I would like to bypass the normal ToggleStateChangedEvent that fires when the togglebutton is clicked manually.  Is there a way to do this?

  6. Answer
    Deyan
    Admin
    Deyan avatar
    2037 posts

    Posted 27 Aug 2009 Link to this post

    Hi southwynd,

    Thanks for writing.

    There is no direct way to prevent the ToggleStateChanged event from firing when programmatically setting the ToggleState property of the RadToggleButton element. However, you can bypass the logic in the ToggleStateChanged event by using a boolean flag. Take a look at the following code snippet:

    public partial class Form2 : RadForm 
        { 
            private bool stateSetProgrammatically = false
     
            public Form2() 
            { 
                InitializeComponent(); 
                this.radToggleButton1.ToggleStateChanged += new StateChangedEventHandler(radToggleButton1_ToggleStateChanged); 
            } 
     
            void radToggleButton1_ToggleStateChanged(object sender, StateChangedEventArgs args) 
            { 
                if (!stateSetProgrammatically) 
                { 
                    //Do work when state is not changed programmatically 
                } 
            } 
     
            private void radButton1_Click(object sender, EventArgs e) 
            { 
                this.stateSetProgrammatically = true
                this.radToggleButton1.ToggleState = Telerik.WinControls.Enumerations.ToggleState.On; 
                this.stateSetProgrammatically = false
            } 
        } 

    As you can see, before programmatically changing the ToggleState of my button, I set the stateSetProgrammatically flag to true, then set the ToggleState and at the end I reset the stateSetProgrammatically value to false. In the event handler of the ToggleStateChanged event I check whether the stateSetProgrammatically value is true and if yes, I do not execute the logic, otherwise I do.

    I hope this helps. Write back if you need further assistance.
     

    Greetings,
    Deyan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. southwynd
    southwynd avatar
    11 posts
    Member since:
    Jul 2009

    Posted 27 Aug 2009 Link to this post

    Thanks Deyan.  That works.
Back to Top
UI for WinForms is Visual Studio 2017 Ready