473,564 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conveting to event driven thinking

For all you seasoned VB programmers this is going to be a no brainer.
However, as a C programmer learning VB I'm having trouble getting my
arms around the event-driven nature of VB.

Suppose I have a form with a single command button (cmdPush).
The form's code (including the cmdPush_Click() sub is between the
dashed lines.

--------------------------------------------------------------------

Option Explicit
Dim i As Integer

Private Sub Form_Load()
i = 0
End Sub

Private Sub cmdPush_Click()
i = i + 1
End Sub

--------------------------------------------------------------------

After cmdPush is pushed 10 times I want the program to end. HOWEVER,
I do not want the cmdPush_Click() to test the value of i or to end the
program. I want that done somewhere else. The way I would do this in
C doesn't work in VB because of the event-driven natue of VB!

How do I do this in VB?

Multiple different answers will be greatly appreciated!

..ARN.
Jul 17 '05 #1
2 3716
> For all you seasoned VB programmers this is going to be a no brainer.
However, as a C programmer learning VB I'm having trouble getting my
arms around the event-driven nature of VB.

Suppose I have a form with a single command button (cmdPush).
The form's code (including the cmdPush_Click() sub is between the
dashed lines.

--------------------------------------------------------------------

Option Explicit
Dim i As Integer

Private Sub Form_Load()
i = 0
End Sub

Private Sub cmdPush_Click()
i = i + 1
End Sub

--------------------------------------------------------------------

After cmdPush is pushed 10 times I want the program to end. HOWEVER,
I do not want the cmdPush_Click() to test the value of i or to end the
program. I want that done somewhere else. The way I would do this in
C doesn't work in VB because of the event-driven natue of VB!

How do I do this in VB?

Multiple different answers will be greatly appreciated!


I have to ask... why do you think you don't want the cmdPush_Click event to
test or end the program? It's the most logical place. Now, of course, you
could create a Function or Sub to do the actual work, but that procedure
will have to be called from this particular event. There is nothing else
watching what you are doing. In an event driven world, your components do
things when the **user** interacts with them. If the user is not interacting
with a component, it just sits there idle. If the user is not interacting
with **any** components of your program, then your whole program is just
sitting there idle. Yes, you can (sort of) defeat this by activating Timers
or Subclassing and/or Hooking components, but basically (pun intended), the
above is true.

In a non-event driven program, you have to create a gigantic loop and
continually look for things to react to on your own. An event driven
program, in effect, is automatically in a gigantic loop and the system is
automatically checking your components for you. If something happens on one
of your components, the system informs your program (invisible to you) and
your program reacts by executing the code (if any) in that particular
control's activated event(s). For this "automation ", you have to give
something up... the ability to control the **linear** flow of your code.
Trust me, this is a good trade-off; but you have to get used to it.
Basically, you have to stop thinking of your program flowing in a linear
fashion with you deciding what is happening each step of the way (your
individual event code still moves that way, but within the overall structure
of an event driven paradigm); instead you have to design it to react no
matter what the user chooses to do. For example, when you wanted input from
the user at a certain stage of an old linear program, you simply stopped the
program and waited for the user to enter something. The user could do
nothing else (unless you gave him/her an ESC key exit or something). In an
event driven program, you place a control, say a TextBox, and you have an
event in the TextBox wait for the user to do something. But, at the same
time, you might have other controls for the user to interact with (other
TextBoxes for other information), CheckBoxes for option selection, ListBoxes
for a different form of option selection, etc. And the user can visit these
in any order he/she decides. Hence, you need to be able to know when the
user thinks they have finished. A CommandButton designated as, say, OK could
be used for that purpose.

I don't know if any of the above ramblings has helped you or not; hopefully
it did.

Rick - MVP
Jul 17 '05 #2
>
So, when it's the human's turn two button choices are enabled: "Roll"
and "Pass." When Roll is pressed a pair of dice are rolled and the
sum is compared to the mark. If this sum equals the mark then the
human's turn is over and the computer takes its turn. If Pass is
pressed then the human's turn is ended and the points accumulated
during the round are added to his cumulative score.

Therefore, the computer's turn may be "started" in two different ways.
What I'm having trouble with is how to implement this turn-based
sequence in a loop for, say, ten rounds.


Okay, so you'll have two buttons (Roll and Pass), someplace to show
the current score ect.

I suggest that you, essentially, write three Subs or Functions, one
called "PlayerTurn ", the second "PlayerRoll " and the third
"ComputerTu rn" or so.

PlayerTurn initializes the player's turn, and is called when you start
a new game. It does nothing much really, maybe tell the player it's
his turn, and enable the buttons (see later why).

PlayerRoll is called when the player presses the "Roll" button. It
determines the roll and shows the result.
1) If the player succeeds with his roll, the buttons stay as they are,
the player can again choose to roll or pass (or quit, never forget
that).
2) If the player fails, ComputerTurn is called.

If the player presses the "Pass" button, the score is updated and
ComputerTurn is called as well.

In ComputerTurn, the player buttons are disabled (Enabled=False) , and
the computer does his turn. Once that is resolved, the score is
updated, the turn count is increased, ect. and PlayerTurn is called
again.

A bit more elegant would be to write a Function called "ComputerRo ll",
and add a timer (say "ComputerTurnTi mer"). Now the function
"ComputerTu rn" does nothing but disable the buttons and activate the
timer.
Inside the timer, "ComputerRo ll" is called. The whole logic is inside
that function. If the computer wants to pass, or fails, the timer is
stopped and the buttons are enabled again.
If you set the timer right, the player can actually watch the computer
make his move.

Hope that gives you a few suggestions

Robert
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1157
by: Jonathan Rea | last post by:
Hello, Any help on the below would be much appreciated. I have an application which is rapidly getting larger and is event driven. i was wondering if anyone knew any good tutorials out there that describe strategies for event structuring. Thanks Jon
0
2714
by: kamlesh.bafna | last post by:
Hi , I am trying to design an event manger class for my Windows application. It is sort of smart client application but doe not depend on any external factors to update itself. Better term would be, its has Rich GUI with mulitple windows being shown to the user at the same time ( sort of MDI simulation). The refresh of the different windows...
3
2574
by: msch-prv | last post by:
I am fairly new to PHP. I started creating a variety of classes (calendars, input forms, combo boxes, etc. ) but I am running into code overhead when I integrate these objects (testing for POST, GETS, Cookies etc.). I am looking for an interrupt driven approach to handle event processing (akin to VB's GUI) to simplify interactions....
3
5521
by: geskerrett | last post by:
We have been asked to develop and application for a client that is a 'notification" system. We would like to use python, but are struggling to find the right starting point. Any suggestions, tips or sample code would be appreciated. Application outline; Machine A is running a "listener" application that is connected to a another device...
14
2103
by: Snor | last post by:
I'm attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven server. I've been told time and time again that I should use an event driven server design (that is, use twisted). There is a lot of interaction...
4
6686
by: AzizMandar | last post by:
C++ Event Coding Questions I have done some simple programs in C++ and read a lot of good C++ books (Including The C++ Programing Language, and C++ Primer) I am trying to understand and implement an Event based program and Message system. I have a very basic event engine that I'm feeling works a bit backwards. I'm looking for documents,...
2
3902
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite well, so at times it is tempting just to port parts of it over mostly as-is. In fact, one MSDN article I read suggested using straight HTML wherever...
0
11300
MMcCarthy
by: MMcCarthy | last post by:
VBA is described as an Event driven programming language. What is meant by this? Access, like most Windows programs, is an event driven application. This means that nothing happens unless it is in response to some event that has been detected by the application. The steps are fairly straightforward: An event happens The event is detected...
15
3755
by: Phillip B Oldham | last post by:
Are there any python event driven frameworks other than twisted?
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.