RadDropDown

1 Answer 38 Views
DropDownList
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Mark asked on 09 Jun 2023, 04:17 PM

Is there an event that is triggered when the READONLY flag is set on this control (or any other control in fact)?

 

Thanks in advanced.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 14 Jun 2023, 10:40 AM

Hi, Mark,

Setting the RadDropDownList.ReadOnly property internally sets the DropDownListElement.ListElement.ReadOnly property. Indeed, there is no event being triggered when the property is modified.

However, I have prepared a sample code snippet demonstrating how to create a custom ReadOnlyChanged event and use it to track the changes in the event:  

        public class CustomDropDown : RadDropDownList
        {
            public event EventHandler<ReadOnlyEventArgs> ReadOnlyChanged;
            public override string ThemeClassName
            {
                get
                {
                    return typeof(RadDropDownList).FullName;
                }
            }
            protected virtual void OnReadOnlyChanged(bool _readOnly)
            {
                if (ReadOnlyChanged != null)
                    ReadOnlyChanged(this, new ReadOnlyEventArgs(_readOnly));
            }
            public void SetReadOnly(bool isReadOnly)
            {
                this.ReadOnly = isReadOnly;
                ReadOnlyChanged(this, new ReadOnlyEventArgs(isReadOnly));
            }

        }

        public class ReadOnlyEventArgs : EventArgs
        {
            public ReadOnlyEventArgs(bool isReadOnly)
            {
                ReadOnly = isReadOnly;
            }

            public bool ReadOnly { get; private set; }
        }
  ((CustomDropDown)this.radDropDownList1).ReadOnlyChanged += DropDown_ReadOnlyChanged;
        private void RadDropDownList1_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine(  e.PropertyName);
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 14 Jun 2023, 10:46 AM

Thank you, This is exactly what I was looking for.  I will try this out.

 

Thank You.

Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 14 Jun 2023, 11:00 AM

okay, so looking at the code above, both the SetReadOnly and OnReadONlyChanged are never called.  I am going to have to use these methods instead of just setting the ReadOnly property in our code?  This is not a problem, just wanted to understand the approach.
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 14 Jun 2023, 11:32 AM

I am workign through the code above, and see that I have to call the "SetReadOnly" method, instead of just setting the "ReadOnly" property in order for the ReadOnlyChanged event to get executed.   What is the purpose of the OnReadOnlyChanged() method?
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 14 Jun 2023, 11:54 AM | edited

Thanks for your input,  but I think I found a simpler solution, which allows me to just set the property of ReadOnly, instead of calling the SetReadOnly method
public class CustomDropDown : RadDropDownList
   {
      public event EventHandler<ReadOnlyEventArgs> ReadOnlyChanged;

      protected virtual void OnReadOnlyChanged(bool _readOnly)
      {
         ReadOnlyChanged?.Invoke(this, new ReadOnlyEventArgs(_readOnly));
      }
      
      public override string ThemeClassName
      {
         get
         {
            return typeof(RadDropDownList).FullName;
         }
      }

      public override bool ReadOnly
      {
         get => base.ReadOnly;
         set
         {
            base.ReadOnly = value;
            OnReadOnlyChanged(value);
         }
      }
   }

   public class ReadOnlyEventArgs : EventArgs
   {
      public ReadOnlyEventArgs(bool isReadOnly)
      {
         ReadOnly = isReadOnly;
      }

      public bool ReadOnly { get; private set; }
   }

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 19 Jun 2023, 05:57 AM

Hi, Mark,

The purpose of the OnReadOnlyChanged method is to raise the event handler if there is any subscriptions to the event. Additional information about the demonstrated approach with creating a custom event is demonstarted here: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/defining-an-event-in-windows-forms-controls?view=netframeworkdesktop-4.8 

As to the alternative solution in your last post, I can confirm that it is OK and you can use it in your scenario. Should you have further questions please let me know.

Tags
DropDownList
Asked by
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or