Using flags in buttons

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 756 Views - Last Post: 03 June 2012 - 12:40 PM Rate Topic: -----

#1 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Using flags in buttons

Posted 03 June 2012 - 08:04 AM

I'm trying to implement a boolean flag into a button so that when the button is clicked the flag will equal to true. After that i need to run an if statement stating that if the button is clicked then move/relocate the button to the corner of the screen. I tried setting my flag as true and using an IF statement but it didnt work. i get a syntax error stating "Operator '=' is not defined for types 'System.Windows.Forms.Button' and 'Boolean'

here's my code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If Button1 = flag1 Then
        Button1.Location = New Point(651, 511)
    End If
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Using flags in buttons

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 08:45 AM

Think about the comparrison you wrote on line 2

If button is equal to true

Does that make sense? Can you write... If banana is equal to honda

You are trying to compare two enterly different type. Like
if "YogiBear" = 19

Try this logic


button1_click
   if myBoolean = true then change button location
   set myBoolean to true 'So the next click the if will match and button will move


I've given you the logic in a sort of psuedo code manner. Take a shot at actually coding it.
Was This Post Helpful? 1
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Using flags in buttons

Posted 03 June 2012 - 08:46 AM

There is no reason for the boolean flag this event confirms there was a click. Can you explain better what you after?
Was This Post Helpful? 0
  • +
  • -

#4 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Using flags in buttons

Posted 03 June 2012 - 09:03 AM

View PosttlhIn`toq, on 03 June 2012 - 08:45 AM, said:

Think about the comparrison you wrote on line 2

If button is equal to true

Does that make sense? Can you write... If banana is equal to honda

You are trying to compare two enterly different type. Like
if "YogiBear" = 19

Try this logic


button1_click
   if myBoolean = true then change button location
   set myBoolean to true 'So the next click the if will match and button will move


I've given you the logic in a sort of psuedo code manner. Take a shot at actually coding it.


Ohh thanks, i see what i've done wrong but is there a way i can move the button on the first click because i need the button to move to the corner of the screen and disable so the user cant click it again.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 09:09 AM

Sure, but it has nothing to do with a flag. I have no idea where that came from based on the description you just gave.


You have shown that you know how to set the .Location so do that.
Then set .Enabled to false



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Location = New Point(651, 511)
    Button1.Enabled = false
End Sub


Also, give your controls meaningful names as soon as you make them - as pointed out in #2 below

Some of my common tips (some may apply more than others to your specific style):
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

Was This Post Helpful? 1
  • +
  • -

#6 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Using flags in buttons

Posted 03 June 2012 - 09:16 AM

View Post_HAWK_, on 03 June 2012 - 08:46 AM, said:

There is no reason for the boolean flag this event confirms there was a click. Can you explain better what you after?


Sure k. I'm making a computer version of the Deal or No Deal show and I'm making the 26 case version. My program has to resemble the logic of the show (Here's a link to the show: Deal or No Deal Show ).

RIght now, i got most of the logic right in a flowchart i drew on paper. So now im trying to code the program in vb.
The game work's like this (Pseudocode):

On form load:
Output the prize values but not their specific location in the case
Tell the user to pick a case. This will be their case.

Button1_Click:
If button1 is clicked then
move it to the corner of the screen
disable the button
and so on....

i just need to move the button on the first click
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 09:23 AM

Dear god don't tell us you intend to duplicate this code 26 times: Once in every button_click event handler.

One of the primary tenants of coding is to not duplicate code.
You only need one method handler for this and all 26 of your caseButtons can point to the one handler.

Notice the signature of a button click handler

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

That sender object isn't there for fun. That is the actual object that send the event. In this case that's the actual button you clicked.

So you cast that sender to type button, then relocate it.

I'm a C# guy so this VB might need some fixing, but you should get my meaning


Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ((Button)sender).Location = New Point(651, 511)
    ((Button)sender).Enabled = false
End Sub

See how we didn't hard-code this to just one button, but instead cast the sent object to a button, then manipulated it.
Was This Post Helpful? 0
  • +
  • -

#8 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Using flags in buttons

Posted 03 June 2012 - 09:41 AM

Well i think its time i properly introduced myself. I'm a 15 year Grade 10 high school student who just started learning VB.Net programming like 6 months ago (Computer Science Course). This assignment im doing is my Independent Study Unit, so i'm NOT asking anyone to do my homework. I just got stuck trying to do it by myself.

Quote

Dear god don't tell us you intend to duplicate this code 26 times: Once in every button_click event handler.


No i wasnt going to copy/paste all that code into each 26 buttons. I was actually thinking of using a loop of some sort. If by any chance you want to know my objective for this assignment (NOT doing my homework, just know my objective) here is the link: Deal or No Deal Project .

SInce i'm a beginner i dont know how to really use method handlers and sender objects, i've only been taught basics like: loops, buttons, variables, etc. But i have been doing research on other advanced methods of doing this instead of using basic stuff and I would also love to learn more advanced methods.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 09:46 AM

Quote

SInce i'm a beginner i dont know how to really use method handlers and sender objects, i've only been taught basics like: loops, buttons, variables, etc.


Well.. you can't really say that any more can you?
You've just been shown it and had it explained to you.
Was This Post Helpful? 1
  • +
  • -

#10 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Using flags in buttons

Posted 03 June 2012 - 09:52 AM

True that. Thank you for taking your time to help a programmer in need. Really appreciate it. :bigsmile:
Was This Post Helpful? 1
  • +
  • -

#11 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Using flags in buttons

Posted 03 June 2012 - 10:30 AM

Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ((Button)sender).Location = New Point(651, 511)
    ((Button)sender).Enabled = false
End Sub



Would be:


Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    DirectCast(sender, Button).Location = New Point(651, 511)
    DirectCast(sender, Button).Enabled = false
End Sub


Notice how you can add handlers for this event. Now the sender object is whichever button was clicked.

This is also a lesson in boxing and unboxing. Boxing is storing an something in an Object, where unboxing is converting it back, we CType, TryCast, and DirectCast for this.

Some error protection:


Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
  'not that you should need it
  If TypeOf sender Is Button Then
    DirectCast(sender, Button).Location = New Point(651, 511)
    DirectCast(sender, Button).Enabled = false
  End If
End Sub

This post has been edited by _HAWK_: 03 June 2012 - 10:30 AM

Was This Post Helpful? 3
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 10:53 AM

Thanks for the translation Hawk. Like I said, I'm not a VB guy.
Was This Post Helpful? 0
  • +
  • -

#13 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Using flags in buttons

Posted 03 June 2012 - 10:58 AM

@tlhIn`toq - no problem it's a team thing!
Was This Post Helpful? 0
  • +
  • -

#14 driftking96  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 02-June 12

Re: Using flags in buttons

Posted 03 June 2012 - 11:06 AM

View Post_HAWK_, on 03 June 2012 - 10:30 AM, said:

Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ((Button)sender).Location = New Point(651, 511)
    ((Button)sender).Enabled = false
End Sub



Would be:


Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
    DirectCast(sender, Button).Location = New Point(651, 511)
    DirectCast(sender, Button).Enabled = false
End Sub


Notice how you can add handlers for this event. Now the sender object is whichever button was clicked.

This is also a lesson in boxing and unboxing. Boxing is storing an something in an Object, where unboxing is converting it back, we CType, TryCast, and DirectCast for this.

Some error protection:


Private Sub caseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
  'not that you should need it
  If TypeOf sender Is Button Then
    DirectCast(sender, Button).Location = New Point(651, 511)
    DirectCast(sender, Button).Enabled = false
  End If
End Sub


Thnaks _HAWK_ it actually changes the case number depending on the button clicked. Ex: when btncase1 is pressed case 1 is displayed, and when btncase2 is clicked case 2 is displayed. :^:
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5431
  • View blog
  • Posts: 11,641
  • Joined: 02-June 10

Re: Using flags in buttons

Posted 03 June 2012 - 11:17 AM

EXACTLY! - I think you're getting it. The sender *IS* the object that caused the event. The sender *IS* the actual button the player clicked on - and so that actual button is sent to the method handling the click event.

At that point you can interogate it for anything you want because it is the real button. Get it's .Text displayed... get its .Backgroundcolor...

Get its .Tag object...
That's a hint. Because a .Tag can be any object. So you could place the contents of the case into the .Tag of the button. Then read it back when the button is clicked on. No need to keep an array of which case has what content. Why keep more things than you need? In a sense, the button really becomes a case because it has a number on the outside (the .Text) and a content on the inside (the .Tag) that you only discover after opening the case (clicking the button)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2