Grasshopper

algorithmic modeling for Rhino

1. I want to add an event handler when ever my slider value change, it will call a method.

I have tried this below, got error message says "No Overload for "Slider_ValueChanged" matched delegate "Grasshopper.GUI.Base.SliderBase.ValueChangeEventHandler"

myslider.Slider.ValueChanged += new Grasshopper.GUI.Base.GH_SliderBase.ValueChangedEventHandler(Slider_ValueChanged);

2. Is it possible to add an value changed event handler to the Input parameter of the component ! so it detect the changes on the input parameter and trigger a method to be called.

Thank you,

Views: 3541

Replies to This Discussion

Hi,

It is possible to register to the slider's value changed event handler,
But i'm not aware of an easy way of doing that tho my way is not THAT complicated.

Also, If the slider is connected to your input parameters why not just call your method from the SolveInstance function and check there if the previous value of your slider has changed? (Called whenever any input parameter is changed)

But if you're trying to link to a slider that is not connected to your component you can.
1. go through your active grasshopper document which is a GH_DocumentServer object
2. go through
Grasshopper.Instances.DocumentServer[0].Objects (replace 0 with the documentserver id the slider component is on)
3. check if the current object's type is
Grasshopper.Kernel.Special.GH_NumberSlider or any other slider.
4. The number slider is actually a component itself, by registering to it's SolutionExpired event you can get the changed value (It expires mostly when a value is changed)
5. x.SolutionExpired += x_SolutionExpired; (
void x_SolutionExpired(IGH_DocumentObject sender, GH_SolutionExpiredEventArgs e)
6. The solution expired delegate can be / or call the method you want
7. You can get the current value from x.CurrentValue

Good luck ;)

H.

BTW, you can link to a slider just like i've explained how to link a slider that isn't connected and also check if this.Params.Input[0].equals(x).

x being the slider object

HI Hen, Thank you for the response.Actually The number slider is linked the component as an input.I have tried x.SolutionExpired += x_SolutionExpired; but

I have getting the same message of No overload for "..." matches delegate. I think I am doing something wrong at the instantiate the evens handler delegate method. But no idea what I am missing. Any idea ?

This is the component structure I follow;

1. I set the input param first

sliderInput = pManager[0];

2. In solve instance

            IList<Grasshopper.Kernel.IGH_Param> sources = sliderInput.Sources;

            bool isinputEmpty = !sources.Any();
            if (!isinputEmpty)
            {
                IGH_DocumentObject comp = sources[0].Attributes.GetTopLevel.DocObject;
                myslider = comp as GH_NumberSlider;
                if (myslider != null)
                {
                    ValDisplay = myslider.Slider.Value.ToString();
                }
                
            }

myslider.SolutionExpired += myslider_SolutionExpired;  // Here I have problem No overload for "..." matches delegate.

And adding a void delegate method below in same component class

        private void myslider_SolutionExpired(Object sender, GH_SolutionEventArgs e)
        {
            // ? unregistered the event ?
        }

Hi,

You're trying to register to the SolutionExpired event of your input parameter, tho you need to understand that it's not your input parameter you're registering to - it's a component that exists in the Grasshopper document that is currently connected to your component and if it's disconnected from your component and another Slider is getting linked to your input parameter you're now following two slider components.

In short, you need to deeply understand how to correctly register to an event what you're doing and handle all possible cases such as:

1. a new slider is linked to the input component

- In this case you need to unregister from previous param (if it exists) and register to the new one

2. your component is removed from the document

- When your component is removed from the GH document it doesn't delete your component class instance - the class is still "alive" for an unknown period of time and if you've previously registered to the Slider's value change event - your function will still be triggered even if your component is not in the GH document.

What you need to do in this case if to override the RemovedFromDocument function and unregister from the slider (if exists).

Have you thought about what if the input parameter is not a slider? what do you want to do then?

About the no overload message you're clearly doing something wrong i'm not sure what it is - In visual studio there's a nice feature that whenever you're trying += of some event handler it automatically generates a stub function that matches the event handler (you need to press tab for it to generate).

After looking at your code and well to be completely honest i'm not sure what it is you're trying to do because if you're already reading the value from the slider in SolveInstance, which BTW can be done easily by just calling DA.GetData<double>(0, ref val). then why are you in need of registering the slider value changed event?

Whenever a value is changed in the slider and it is linked to your component - your component's solve instance function will be called.

Please explain a little bit more about what you're trying to accomplish and i'll do my best to help you out.

Thank you Hen, I have moved to this issue into a new discussion.

http://www.grasshopper3d.com/forum/topics/window-forms-param-data-b...

I am trying to bind the GH component parameter to a windows form property.  what here is an very unorthodox way... since usually a gh component gets the inputs, solve instance function and may be set the outputs if there is any!

I would like to call a wpf class/ windows form only once at the beginning of the solve instance and component stays valid (or solution is not expired) until the dialog results return a value. I need to set the input parameter value changed event handler in gh component, each time the input value changes it trigger WPF object property set the TextBox value. My struggle is to call appropriate event handlers in Grasshopper so far.

Yes VS automatically generates the handlers but gives no hint about the delegates or arguments needs to be passed. And I have almost zero experience with events handlers in GH.

Once again, Thanks a lot for your help,

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service