Results 1 to 10 of 10

Thread: changing inner code

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    changing inner code



    if its possible could i temporarily change the inner code of a program instead of using a timer or multiple buttons

    ex: i have a BUTTON1 set to open a webpage like www.happy.com

    could i some how be able to do something like this

    Private Sub BUTTON2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    if textbox1.text=1 then
    button1.code=me.close
    if textbox1.text=2 then
    button1.code=systembeep
    if textbox1.text=3 then
    button1.code=listbox1.disable


    End Class
    Last edited by TD5X; Apr 24th, 2012 at 01:22 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: changing inner code

    There's no such thing as 'button1.code'. If you want to do different things under different circumstances then you simply use an If statement, as you obviously already know how to do. You handle the Click event of the Button and then you use If statements to test which condition is True and then perform the appropriate action based on that.

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: changing inner code

    Quote Originally Posted by jmcilhinney View Post
    There's no such thing as 'button1.code'. If you want to do different things under different circumstances then you simply use an If statement, as you obviously already know how to do. You handle the Click event of the Button and then you use If statements to test which condition is True and then perform the appropriate action based on that.
    yes i was using button1.code as an example
    yes sir i do know how to use ifs, just seeing if i can could go without have my program use timers every single time

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: changing inner code

    I'm afraid that you're not really making sense. There's nothing in your first post that has anything to do with Timers. You're going to have to provide a clearer explanation if you want any more details.

  5. #5

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: changing inner code

    Quote Originally Posted by jmcilhinney View Post
    I'm afraid that you're not really making sense. There's nothing in your first post that has anything to do with Timers. You're going to have to provide a clearer explanation if you want any more details.
    lol sorry i barely know english most sense i can make out of what ive posted

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: changing inner code

    Then sorry but I can't suggest anything other than to use a If statement in your Click event handler to decide what to do at that time.

  7. #7

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: changing inner code

    Quote Originally Posted by jmcilhinney View Post
    Then sorry but I can't suggest anything other than to use a If statement in your Click event handler to decide what to do at that time.
    no idea where my other post went i tried making as much sense as i could but got a 502/bad gateway

    good day sir, from mars

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: changing inner code

    The normal way I'd done this:

    vb Code:
    1. Private Sub Beep()
    2.     '
    3. End Sub
    4.  
    5. Private Sub CloseForm()
    6.     Me.Close()
    7. End Sub
    8.  
    9. Private Sub WhateverElse()
    10.    ' Whatever else
    11. End Sub
    12.  
    13.  
    14. Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    15.     Select Case TextBox1.Text
    16.         Case "1"
    17.             Beep()
    18.         Case "2"
    19.             CloseForm()
    20.         Case "3"
    21.             WhatEverElse()
    22.     End Select
    23. End Sub


    The awkwards way you'd want to do it:
    vb Code:
    1. Private currentHandler As EventHandler
    2.  
    3. Private Sub Beep(ByVal sender As Object, ByVal e As EventArgs)
    4.         '
    5. End Sub
    6.  
    7. Private Sub CloseForm(ByVal sender As Object, ByVal e As EventArgs)
    8.     Me.Close()
    9. End Sub
    10.  
    11. Private Sub WhateverElse(ByVal sender As Object, ByVal e As EventArgs)
    12.     ' Whatever else
    13. End Sub
    14.  
    15. Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    16.     If Not (IsNothing(currentHandler)) Then
    17.         RemoveHandler Button1.Click, currentHandler
    18.     End If
    19.     Select Case TextBox1.Text
    20.         Case "1"
    21.             currentHandler = New EventHandler(AddressOf Me.Beep)
    22.             AddHandler Button1.Click, currentHandler
    23.         Case "2"
    24.             currentHandler = New EventHandler(AddressOf Me.CloseForm)
    25.             AddHandler Button1.Click, currentHandler
    26.         Case "3"
    27.             currentHandler = New EventHandler(AddressOf Me.WhateverElse)
    28.             AddHandler Button1.Click, currentHandler
    29.     End Select
    30. End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2010
    Posts
    56

    Re: changing inner code

    Quote Originally Posted by cicatrix View Post
    The normal way I'd done this:

    vb Code:
    1. Private Sub Beep()
    2.     '
    3. End Sub
    4.  
    5. Private Sub CloseForm()
    6.     Me.Close()
    7. End Sub
    8.  
    9. Private Sub WhateverElse()
    10.    ' Whatever else
    11. End Sub
    12.  
    13.  
    14. Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    15.     Select Case TextBox1.Text
    16.         Case "1"
    17.             Beep()
    18.         Case "2"
    19.             CloseForm()
    20.         Case "3"
    21.             WhatEverElse()
    22.     End Select
    23. End Sub


    The awkwards way you'd want to do it:
    vb Code:
    1. Private currentHandler As EventHandler
    2.  
    3. Private Sub Beep(ByVal sender As Object, ByVal e As EventArgs)
    4.         '
    5. End Sub
    6.  
    7. Private Sub CloseForm(ByVal sender As Object, ByVal e As EventArgs)
    8.     Me.Close()
    9. End Sub
    10.  
    11. Private Sub WhateverElse(ByVal sender As Object, ByVal e As EventArgs)
    12.     ' Whatever else
    13. End Sub
    14.  
    15. Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
    16.     If Not (IsNothing(currentHandler)) Then
    17.         RemoveHandler Button1.Click, currentHandler
    18.     End If
    19.     Select Case TextBox1.Text
    20.         Case "1"
    21.             currentHandler = New EventHandler(AddressOf Me.Beep)
    22.             AddHandler Button1.Click, currentHandler
    23.         Case "2"
    24.             currentHandler = New EventHandler(AddressOf Me.CloseForm)
    25.             AddHandler Button1.Click, currentHandler
    26.         Case "3"
    27.             currentHandler = New EventHandler(AddressOf Me.WhateverElse)
    28.             AddHandler Button1.Click, currentHandler
    29.     End Select
    30. End Sub
    came to the conclusion of what i was trying to accomplish isnt possible after checking here and og other forums google as well

    ty learned about cases from your post

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: changing inner code

    If the thing to do when the button is pressed is determined by 'what has occured in the past' rather than 'current state' (e.g. press buttonX makes actionX the thing to do, press buttonY and then actionY is the thing to do) then another way of doing it is the following:

    vbnet Code:
    1. Private _action As Action
    2.  
    3. Private Sub Beep()
    4.     '
    5. End Sub
    6.  
    7. Private Sub CloseForm()
    8.     Me.Close()
    9. End Sub
    10.  
    11. Private Sub WhateverElse()
    12.    ' Whatever else
    13. End Sub
    14.  
    15. Private Sub ButtonBeep_Click(ByVal sender As Object, e As EventArgs) Handles ButtonBeep.Click
    16.     _action = AddressOf Beep
    17. End Sub
    18.  
    19. Private Sub ButtonCloseForm_Click(ByVal sender As Object, e As EventArgs) Handles ButtonCloseForm.Click
    20.     _action = AddressOf CloseForm
    21. End Sub
    22.  
    23. Private Sub ButtonWhateverElse_Click(ByVal sender As Object, e As EventArgs) Handles ButtonWhateverElse.Click
    24.     _action = AddressOf WhateverElse
    25. End Sub
    26.  
    27. Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
    28.     _action()
    29. End Sub

    Simply assigning a function to the field with type Action allows you to execute that action later.

    As I said, this is better suited for the situation where 'something happens' and as a result the action that happens when you click the button should change. It doesn't sound like that's your situation here, so this is more for giving you ideas than suggesting its use right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width