Guidance
指路人
g.yi.org
Guidance Forums / wxWidgets (wxWindows) in C++ / Event Handling Question

Register 
新用户注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Event Handling Question
#2736
Posted by: 2003-10-14 06:44:21
I am makeing a program that uses sliders to control some stage lights, this requires me to send the new slider value and the slider's name. Can you pass other information about the object during an event and not just the event information?
Message2. Re: Event Handling Question
#2745
Posted by: 2003-10-16 02:14:11
Hi!
Not sure what you mean exactly, but maybe wxCommandEvent::SetClientData might help you.

upcase
Message3. Re: Event Handling Question
#2747
Posted by: 2003-10-16 02:54:03
Let me see if I can explain what I'm looking for better. What I have is a class named Light that is based on the wxSlider class. To find out what the slider's value is changed to I have an event table.

BEGIN_EVENT_TABLE(Light, wxSlider)
    EVT_SCROLL(Light::OnChange) //when the slider is moved, call this function
END_EVENT_TABLE()


void Light::OnChange(wxScrollEvent &event) {
    int level = event.GetPosition() + 100; //get the slider's position
    return;
}

The function isn't finished yet, but when it is, it should take the name of the slider, ex "D1" and the value it is at, then copy them to COM1. For this to work correctly, I need to pass the rest of the Light object's attributes, or at least just the name of it to the OnChange function. So far, the only information i have seen given to a function that is called by an event is something like

wxScrollEvent &event 

If the wxCommandEvent::SetClientData is how I can pass other data, could you post an example of how to use it?
Message4. Re: Event Handling Question
#2757
Posted by: upCASE 2003-10-19 19:00:45
Hi!
Sorry that it took some time for me to reply, but we had a very good party on friday, so I wasn't able to write code till today :)

I just had a look into the matter and wrote some example app. I hope I understood what you want to do correctly...
So you have a panel with more than one slider on it. All sliders have attributes like a name and other properties. The class for this special slider is called Light with wxSlider as it's base class. You now want to send the attributes of these sliders to COM1 when the slider is moved.

--- If I got it all wrong, don't read any further :) ---
I think you could do it in two ways. The first one would be the easier solution while the second could be a bit harder to implement. Anyway it all depends on what you want.
You could actually go without wxCommandEvent::SetClientData() and do it like you started.

#ifndef __LIGHT_H
#define __LIGHT_H

#include <wx/wx.h>

class Light : public wxSlider
{
    wxString name;
    int someotherparameter;
          
public:
    Light(wxWindow* parent, int id, int val, int min, int max, wxString name, int other);
private:

    void OnChange(wxScrollEvent& e);
    DECLARE_EVENT_TABLE()
};

#endif

#include "light.h"

BEGIN_EVENT_TABLE(Light, wxSlider)
//    EVT_SCROLL_ENDSCROLL(Light::OnChange) //when the slider is moved, call this function
    EVT_SCROLL(Light::OnChange) //when the slider is moved, call this function
END_EVENT_TABLE()

Light::Light(wxWindow* parent, int id,int val, int min, int max, wxString name, int other)
:wxSlider(parent, id, val, min, max, wxDefaultPosition, wxDefaultSize, wxSL_VERTICAL)
{
    this->name = name;
}

void Light::OnChange(wxScrollEvent &event) {

    int level = event.GetPosition() + 100; //get the slider's position
    
    wxString message;
    message << "Name: " << name << ", other things: " << someotherparameter;
    message << ", level: " << level;

    wxLogMessage(message);
}
In the above code I programmed a Light class like you did and an eventhandler for this class. Now, everytime the slider is moved, a wxScrollEvent is generated and OnChange() is called. OnChange() is the eventhandler for Light. The positive thing is that OnChange() is implemented specifically for this class, so that every instance of Light has it's own handler, not a general one that handles all wxScrollEvent events. So I can simply get the needed attributes of Light by either simply using the like I did (name, someotherparameter) or write set/get methods for that. wxLogMessage is in my case another frame with a wxLogTextCtrl as the log target. Everytime the slider is moved I get all the parameters listed up as a string, e.g. "Name: EX1, other tings: 5, level: 106". You could break there and send the name and stuff to COM1.

The other solution could be to write your own event class. That way you could implement it so that the event itself holds all info you need (like you could call myevent.GetName() or stuff). The advantage would be that you could plug the eventhandler to you main dialog or frame and it would handle all slider events, get the needed stuff and send it to COM1.

I hope I got it right and this helps a little. If you've got further questions reply. I can mail my example to you if you like.

upcase
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Message5. Re: Event Handling Question
#2771
Posted by: 2003-10-25 21:40:20
Ok, I tried your first suggestion  now that I have had some time to work on my program and it seems to work fine. Thank you for your help, but now I have another question. Is there a wxWindows class that can be used for serial port communication, or should I just use a handle object?
Message6. Re: Event Handling Question
#2772
Posted by: upCASE 2003-10-26 02:35:53
Hi!
Try this link
http://www.iftools.com/index.en.html

I got it from the wxWindows website. Since I never used it, I can't tell you if it works...

Or try this one:
 http://www.tetraedre.com/advanced/serial2.php3
as Guidance suggested this.

upcase
upCASE
-----------------------------------
If it was hard to write, it should be hard to read!- Do. Or do not. There is no try!
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2021-10-23  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0