-
Apr 24th, 2012, 01:18 AM
#1
Thread Starter
Member
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.
-
Apr 24th, 2012, 01:50 AM
#2
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.
-
Apr 24th, 2012, 02:35 AM
#3
Thread Starter
Member
Re: changing inner code
Originally Posted by jmcilhinney
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
-
Apr 24th, 2012, 03:04 AM
#4
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.
-
Apr 24th, 2012, 03:06 AM
#5
Thread Starter
Member
Re: changing inner code
Originally Posted by jmcilhinney
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
-
Apr 24th, 2012, 03:43 AM
#6
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.
-
Apr 24th, 2012, 03:45 AM
#7
Thread Starter
Member
Re: changing inner code
Originally Posted by jmcilhinney
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
-
Apr 24th, 2012, 03:46 AM
#8
Re: changing inner code
The normal way I'd done this:
vb Code:
Private Sub Beep() ' End Sub Private Sub CloseForm() Me.Close() End Sub Private Sub WhateverElse() ' Whatever else End Sub Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click Select Case TextBox1.Text Case "1" Beep() Case "2" CloseForm() Case "3" WhatEverElse() End Select End Sub
The awkwards way you'd want to do it:
vb Code:
Private currentHandler As EventHandler Private Sub Beep(ByVal sender As Object, ByVal e As EventArgs) ' End Sub Private Sub CloseForm(ByVal sender As Object, ByVal e As EventArgs) Me.Close() End Sub Private Sub WhateverElse(ByVal sender As Object, ByVal e As EventArgs) ' Whatever else End Sub Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click If Not (IsNothing(currentHandler)) Then RemoveHandler Button1.Click, currentHandler End If Select Case TextBox1.Text Case "1" currentHandler = New EventHandler(AddressOf Me.Beep) AddHandler Button1.Click, currentHandler Case "2" currentHandler = New EventHandler(AddressOf Me.CloseForm) AddHandler Button1.Click, currentHandler Case "3" currentHandler = New EventHandler(AddressOf Me.WhateverElse) AddHandler Button1.Click, currentHandler End Select End Sub
-
Apr 24th, 2012, 04:03 PM
#9
Thread Starter
Member
Re: changing inner code
Originally Posted by cicatrix
The normal way I'd done this:
vb Code:
Private Sub Beep()
'
End Sub
Private Sub CloseForm()
Me.Close()
End Sub
Private Sub WhateverElse()
' Whatever else
End Sub
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Select Case TextBox1.Text
Case "1"
Beep()
Case "2"
CloseForm()
Case "3"
WhatEverElse()
End Select
End Sub
The awkwards way you'd want to do it:
vb Code:
Private currentHandler As EventHandler
Private Sub Beep(ByVal sender As Object, ByVal e As EventArgs)
'
End Sub
Private Sub CloseForm(ByVal sender As Object, ByVal e As EventArgs)
Me.Close()
End Sub
Private Sub WhateverElse(ByVal sender As Object, ByVal e As EventArgs)
' Whatever else
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
If Not (IsNothing(currentHandler)) Then
RemoveHandler Button1.Click, currentHandler
End If
Select Case TextBox1.Text
Case "1"
currentHandler = New EventHandler(AddressOf Me.Beep)
AddHandler Button1.Click, currentHandler
Case "2"
currentHandler = New EventHandler(AddressOf Me.CloseForm)
AddHandler Button1.Click, currentHandler
Case "3"
currentHandler = New EventHandler(AddressOf Me.WhateverElse)
AddHandler Button1.Click, currentHandler
End Select
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
-
Apr 25th, 2012, 06:12 AM
#10
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:
Private _action As Action
Private Sub Beep()
'
End Sub
Private Sub CloseForm()
Me.Close()
End Sub
Private Sub WhateverElse()
' Whatever else
End Sub
Private Sub ButtonBeep_Click(ByVal sender As Object, e As EventArgs) Handles ButtonBeep.Click
_action = AddressOf Beep
End Sub
Private Sub ButtonCloseForm_Click(ByVal sender As Object, e As EventArgs) Handles ButtonCloseForm.Click
_action = AddressOf CloseForm
End Sub
Private Sub ButtonWhateverElse_Click(ByVal sender As Object, e As EventArgs) Handles ButtonWhateverElse.Click
_action = AddressOf WhateverElse
End Sub
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
_action()
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|