vb.net - Winforms button click mousedown/mouseup -


i experienced strange thing in else's application making myself familiar with: button on click starts or stops plc (twincat beckhoff) this:

(just remark: hbool1 variable integer handle plc knows internal variable want change: here: switch on/off)

private sub button1_mousedown(sender object, e mouseeventargs) handles button1.mousedown     'switch off     adsclient.writeany(hbool1, false) end sub  private sub button1_mouseup(sender object, e mouseeventargs) handles button1.mouseup     'switch on     adsclient.writeany(hbool1, true) end sub 

i wasn't familiar mouseup/mousedowns on button , expected click. out of curiosity tried click event:

dim state_ison bool  private sub button1_click(sender object, e mouseeventargs) handles button1.click             if state_ison       'switch off        adsclient.writeany(hbool1, false)     else                'switch on        adsclient.writeany(hbool1, true)     end if end sub 

the variable state_ison set true or false when plc responds answer.

i not understand 2 things:

  1. why 1st code snippet work? on first click switches on, on next click switches off. far see, both events called in row: first mousedown, afterwards mouseup. plc should switched on always, can switch off well.

  2. why own idea not work? isn't more logical?!

what did not try yet check mouseclick event. perhaps own idea work, still there question, why 1st code work @ all.

edit:

i figured out thing: consider first snippet, how done (working): put 2 message boxes in code , noticed different behaviour: message boxes, not work more. when click button, point showing "switch off", , no following "switch on". while might related interrupting nature of message box, still interesting.

private sub button1_mousedown(sender object, e mouseeventargs) handles button1.mousedown     'switch off     adsclient.writeany(hbool1, false)     msgbox("switch off") end sub  private sub button1_mouseup(sender object, e mouseeventargs) handles button1.mouseup     'switch on     adsclient.writeany(hbool1, true)     msgbox("switch on") end sub 

another note: mouseclick-event not work, either. have feeling there wrong in code of plc , former author figured out mousebuttonup/down hack somehow make work, instead of fixing plc. possible?

to start or stop plc tcadsclient should use writecontrol methods available like:

tcclient.writecontrol(newstateinfo(adsstate.run,tcclient.readstate().devicestate));  tcclient.writecontrol(newstateinfo(adsstate.stop,tcclient.readstate().devicestate)); 

you not stopping plc. instead writing bool variable on plc, happens change control flow of plc program. normal programming style on plc trigger action whith rising or falling edge on bool variable. create edge makes perfect sense use mouseup/mousedown event. there possibility when click fast rising edge not recognized. can happen if variable gets updated false , true in same plc cycle. should use clickevent set false , trigger timer set variable true again ensure not in same plc cycle.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -