Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Continually updating a field in Windows Forms (VC++ .net)

Status
Not open for further replies.

BrimStonePhoenix

Programmer
Jun 22, 2006
7
US
I'm using the windows forms to create a GUI for an interface with an optical encoder. The encoder reports position data whenever I send it a command to get the data. What I'm trying to do is this - when I press a button on the GUI, I want it to continually update a text field on the GUI with the current position data, so as I move the knob on the encoder, it updates the text field with the new position.

What I currently have it doing is whenever I press the button, it only gets the current point, and updates the text field once. In order to get another point, I have to push the button again.

I've tried using while/for loops within the event caller, but that just makes the application crash. I'm wondering how I can loop through the 'get data and update the text box' code on just a single button push, and then possibly stop doing it when I push a 'stop' button.
 
Instead of just responding to mouse clicks (WM_COMMAND?), respond to mouse moves as well.
 
When I say click a button, I mean button on the GUI, not whenever I click a mouse button.
 
I know. That's why I said WM_COMMAND, not WM_MOUSEDOWN!

When you get the WM_COMMAND from the Start button, set a flag to true.
When you get the WM_COMMAND from the Stop button, set the flag to false.

In the WM_MOUSEMOVE (I think that's what it's called, don't have Visual Studio on this puter), if the flag is false, do nothing. If the flag is true, get the mouse position from the parameters on the mouse movement and set your field from there.
 
Another option in pseudo code, assuming you want the same button to start/stop the updating.

Basically, use a timer.
Code:
::ButtonClickEvent
{
  If (_timer = null)
    _timer = SetTimer(1, 1000, 0);  //Fire an event every 1000 milleseconds
  else
    KillTimer(_timer);
}

::OnTimer(UINT_PTR nIDEvent)
{
  UpdateTextField();
}

To avoid needless flashing I'd put in a check in UpdateTextField(); to make sure it only actually updates the text field when the data changes.

_timer would be a protected member of type UINT_PTR in case you haven't used SetTimer/KillTimer before
 
Sorry, don't want to be vague since I won't be around for followups for quite a few hours...

In your .h you'll need

Code:
afx_msg void OnTimer(UINT_PTR nIDEvent);

And in your message map you'll need

Code:
ON_WM_TIMER()

FYI the nIDEvent is because of what the WM_TIMER event sends... you likely won't do anything with it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top