This is a migrated thread and some comments may be shown as answers.

Spinner inside PropertyGrid

9 Answers 268 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Eusebio
Top achievements
Rank 1
Veteran
Eusebio asked on 07 Jan 2021, 03:48 PM

About the PropertyGrid in one of the SPINNER type inputs, we need this behavior, please help us:


1. We need to not be allowed to enter a number with the keyboard. Only allow increase / decrease using the Spinner arrows.  It's possible ?.

2. By increasing or decreasing with the arrows, certain code is executed immediately. Since we tried it through the PropertyValueChanged () event, but it is executed when the Spinner loses focus.

        private void rpgHcPropiedades_PropertyValueChanged(object sender, PropertyGridItemValueChangedEventArgs e)
        {
            if (e.Item.Name == "NumOperarios")
            {
                PropertyGridItem item = e.Item as PropertyGridItem;
                byte operario = (byte)item.Value;
                byte operarioAnt = (byte)item.OriginalValue;

               TO-DO!!!
            }
        }

 

3. Make a validation before the new value in the Spinner is accepted. If the validation does not pass, the Spinner does not increase.

4. Assign it a rank.

Please...
Can you provide us with some example code to achieve this?

 

Thank you very much!

POCO:

        [Category("Operaciones de cálculo")]
        [DisplayName("Operarios")]
        public byte NumOperarios { get => numOperarios; set => numOperarios = value; }

 

 

9 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 08 Jan 2021, 10:41 AM

Hello, Eusebio,   

The PropertyValueChanged event is expected to be fired when the value of a certain property is changed. While the editor is being active, you are actually changing the editor's value but is it not committed to the actual property of the SelectedObject. That is why when you commit the editor's value, the PropertyValueChanged event gets fired and your code is executed.

In order to allow the users to edit the value only by using the spin buttons, you can disable the textbox in the PropertyGridSpinEditor. As to the validation before the value is accepted, you can detect the BaseSpinEditorElement.ValueChanging event. You have access to the editor and its element in the RadPropertyGrid.EditorInitialized event. Please have a look at the following code snippet: 

        public RadForm1()
        {
            InitializeComponent();

            this.radPropertyGrid1.SelectedObject = new Item(1,"Test",1); 
            this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized;
        } 

        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            PropertyGridSpinEditor spinEditor = e.Editor as PropertyGridSpinEditor;
            if (spinEditor != null)
            {
                BaseSpinEditorElement element = spinEditor.EditorElement as BaseSpinEditorElement;
                element.TextBoxItem.Enabled = false;
                element.TextBoxItem.UseDefaultDisabledPaint = false;

                element.ValueChanging -= element_ValueChanging;
                element.ValueChanging += element_ValueChanging;
            }
        }

        private void element_ValueChanging(object sender, ValueChangingEventArgs e)
        {
            //detect the value changing for the editor
        }

        public class Item
        {
            public int Id { get; set; }

            public string Name { get; set; }

            [Category("Operaciones de cálculo")]
            [DisplayName("Operarios")]
            public byte NumOperarios { get; set; }

            public Item(int id, string name, byte numOperarios)
            {
                this.Id = id;
                this.Name = name;
                this.NumOperarios = numOperarios;
            }
        }

As to the rank, could you please give us some more details about the exact goal that you are trying to achieve? Once we get better understanding of the precise case, we would be able to think about a suitable solution and provide further assistance. Thank you in advance. 

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

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 11 Jan 2021, 11:55 AM

Thanks Nadya,

 

Regarding the interval of the Spin, it should allow from 1 to 10. Do you understand me?

 

Best regards,

 

0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Jan 2021, 10:46 AM

Hi, Eusebio,

If you want to restrict the user to enter values between 1 and 10, it is appropriate to specify the minimum/maximum when the editor is initialized:

        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            PropertyGridSpinEditor spinEditor = e.Editor as PropertyGridSpinEditor;
            if (spinEditor != null)
            {
                BaseSpinEditorElement element = spinEditor.EditorElement as BaseSpinEditorElement; 
                spinEditor.MinValue = 1;
                spinEditor.MaxValue = 10; 
            }
        }

I believe that it would fit your needs.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 12 Jan 2021, 12:52 PM
Thanks Dess and Nadya.
0
Eusebio
Top achievements
Rank 1
Veteran
answered on 19 Jan 2021, 11:12 AM

Thanks, but when the PropertyGrid loses focus, the "Element_ValueChanging" event is executed causing strange behavior.
I have dealt in various ways, including with helper variables containing the current value.
See my images please.

Best regards

my code:

        private void rpgHcPropiedades_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {

            // Configurar Spinner de Operarios(rpgHcPropiedades):   
            PropertyGridSpinEditor spinEditor = e.Editor as PropertyGridSpinEditor;
            if (spinEditor != null)
            {

                // Deshabilitar el GET del Spinner Operarios:
                BaseSpinEditorElement element = spinEditor.EditorElement as BaseSpinEditorElement;
                element.TextBoxItem.Enabled = false;
                element.TextBoxItem.UseDefaultDisabledPaint = false;

                spinEditor.MinValue = 1;
                spinEditor.MaxValue = CantidadMaximaCabecerasSimograma;  // Por ahora ponemos esto como maximo.

                element.ValueChanging -= Element_ValueChanging;
                element.ValueChanging += Element_ValueChanging;




            }

        }


        private void Element_ValueChanging(object sender, ValueChangingEventArgs e)
        {
            // Se dispara cuando le damos al Spinner de operarios en el RPG de "Propiedades".
            if (e.NewValue == null || e.OldValue == null)
            {
                e.Cancel = true;
                return;
            }
           

            string valorNuevo3 = e.NewValue.ToString();

            byte valorNuevo2 = Convert.ToByte(valorNuevo3);

            if (valorNuevo2 <= 0)
            {

                e.Cancel = true;
                return;
            }


            if (valorNuevo2 == operarioActualSpinner)  //  try - prevenir que se borre cuando rpg pierede el foco on/off
            {

                e.Cancel = true;
                return;

            }


            // Anadir o retirar el operario.
            //string valorAnterior = e.OldValue.ToString();
            //byte valorAnterior2 = Convert.ToByte(valorAnterior);

            byte valorAnterior2 = operarioActualSpinner;


            // Validacion 1: Que entre antiguo y nuevo valor sea 1
            //var diferencia = valorAnterior2 - valorNuevo2;
            var diferencia = operarioActualSpinner - valorNuevo2;

            int diferencia2 = Math.Abs(diferencia);
            if (diferencia2 != 1)
            {

                e.Cancel = true;
                return;
            }


            // Validacion 2: Que se controle el antiguo y nuevo valor se controles con 2 variables.
            // (por ahora no es necesario, ya que la validacion 1 si funciona).



            if (valorNuevo2 < valorAnterior2)  // disminuyendo?
            {

                // Validamos si operario es factible de retirar:
                if (!OperarioTieneElementos(valorAnterior2) )
                {
                    // Retiramos el operario:
                    AnadirRetirarOperario("-", valorAnterior2);
                }
                else
                {

                    e.Cancel = true;
                    return;
                }

            }
            else
            {

                // Preguntamos por el tope de cabeceras que acepta actualmente ( lo hacemos para que no de error )
                if (dsHojaCalculoCabecerasSimograma.Tables[0].Rows.Count >= CantidadMaximaCabecerasSimograma)
                {
                    oViewHc.NumOperarios = operarioActualSpinner; // REFRESCAR EL VALOR EN EL RPG (PENDX: FUNCA?)
                    e.Cancel = true;
                    return;
                }


                // anadimos operario:
                AnadirRetirarOperario("+", valorNuevo2);
            }


            operarioActualSpinner = valorNuevo2;

            oViewHc.NumOperarios = operarioActualSpinner; // REFRESCAR EL VALOR EN EL RPG (PENDX: FUNCA?)



        }

 

 

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 21 Jan 2021, 12:05 PM
Can you help me please?
When the PropertyGrid loses focus, the Element_ValueChanging fires again receiving wrong NewValue and OldValue values.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Jan 2021, 12:19 PM

Hello, Eusebio, 

According to the provided screenshots, it wouldn't be quite easy to determine what are the exact steps that you are executing and what is the undesired behavior that you are facing.

I have modified my sample project following the provided code snippet. Indeed, when you confirm the editor's value the BaseSpinEditorElement.ValueChanging event gets fired one last time which may lead to undesired behavior on your end.

That is why I would recommend you to use the RadPropertyGrid.ValueChanging event which is specifically introduced for detecting the editor's value changing. 

Please give it a try and see how it works for your scenario. 

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Eusebio
Top achievements
Rank 1
Veteran
answered on 21 Jan 2021, 12:45 PM

Do you have some code snippet for apply in RadPropertyGrid.ValueChanging event , please?

Thanks a lot.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Jan 2021, 12:53 PM

Hello, Eusebio,

Actually, there is nothing specific in the code. In a similar way you perform the custom logic in the BaseSpinEditorElement.ValueChanging event you can move it to the RadPropertyGrid.ValueChanging event. You can subscribe to this event either at design time or in the form's constructor. Note that it is relevant for the editor's value changing while it is being active.

        public RadForm1()
        {
            InitializeComponent();

            this.radPropertyGrid1.SelectedObject = new Item(1,"Test",1); 
            this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized; 

            this.radPropertyGrid1.ValueChanging += RadPropertyGrid1_ValueChanging;
        }

        private void RadPropertyGrid1_ValueChanging(object sender, ValueChangingEventArgs e)
        {
            Console.WriteLine(e.NewValue);
        }

Should you have further questions please let me know.

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
PropertyGrid
Asked by
Eusebio
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Eusebio
Top achievements
Rank 1
Veteran
Share this question
or