Call One button from another Button with delay in VB.NET












-2















I have Button1 with this code:



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

Label2.Text = ""
Label3.Text = ""
Label4.Text = ""

End Sub


and want to call button1 when click on button2 with delay, for example when clicked on button2 after 20 seconds apply button1.



in button2 code:



 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles      Button2.Click

Button1_Click(sender, e)
Label6.Text = ""
End Sub


how can I make it with delay of time so first clear label6.text then after delay clear labe2 and 3 and 4.










share|improve this question

























  • Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

    – Hans Passant
    Nov 21 '18 at 14:52
















-2















I have Button1 with this code:



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

Label2.Text = ""
Label3.Text = ""
Label4.Text = ""

End Sub


and want to call button1 when click on button2 with delay, for example when clicked on button2 after 20 seconds apply button1.



in button2 code:



 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles      Button2.Click

Button1_Click(sender, e)
Label6.Text = ""
End Sub


how can I make it with delay of time so first clear label6.text then after delay clear labe2 and 3 and 4.










share|improve this question

























  • Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

    – Hans Passant
    Nov 21 '18 at 14:52














-2












-2








-2








I have Button1 with this code:



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

Label2.Text = ""
Label3.Text = ""
Label4.Text = ""

End Sub


and want to call button1 when click on button2 with delay, for example when clicked on button2 after 20 seconds apply button1.



in button2 code:



 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles      Button2.Click

Button1_Click(sender, e)
Label6.Text = ""
End Sub


how can I make it with delay of time so first clear label6.text then after delay clear labe2 and 3 and 4.










share|improve this question
















I have Button1 with this code:



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

Label2.Text = ""
Label3.Text = ""
Label4.Text = ""

End Sub


and want to call button1 when click on button2 with delay, for example when clicked on button2 after 20 seconds apply button1.



in button2 code:



 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles      Button2.Click

Button1_Click(sender, e)
Label6.Text = ""
End Sub


how can I make it with delay of time so first clear label6.text then after delay clear labe2 and 3 and 4.







vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:05







Mohammad Khaled

















asked Nov 21 '18 at 13:23









Mohammad KhaledMohammad Khaled

408




408













  • Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

    – Hans Passant
    Nov 21 '18 at 14:52



















  • Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

    – Hans Passant
    Nov 21 '18 at 14:52

















Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

– Hans Passant
Nov 21 '18 at 14:52





Do avoid "calling a button". Use a timer, its Tick event can call a private method that the button's Click event handler can use as well. Do consider if you still need the button, it is not likely.

– Hans Passant
Nov 21 '18 at 14:52












2 Answers
2






active

oldest

votes


















0














As Hans pointed out avoid calling another button. Create a private sub that both buttons can use. Then use a timer for the 20 second delay. Something like this:



'Declare a timer with a tick rate of 20 seconds
Dim Timer As New Timer With {.Interval = 20000}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearLabels2To4()
End Sub

'Create this as a separate sub, so it can be used by both buttons.
Private Sub ClearLabels2To4()
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClearLabels2To4()
'Start the 20 second timer
Timer.Start()
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
Label6.Text = ""
'Stop the timer so it doesn't continually fire every 20 seconds
Timer.Stop()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a handler for the timer ticks
AddHandler Timer.Tick, AddressOf Timer_Tick
End Sub





share|improve this answer
























  • Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

    – Mary
    Nov 21 '18 at 20:58





















0














If you wanna or need to use button, the following code might be worked.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label6.Text = ""
System.Threading.Thread.Sleep(20000)
Button1_Click(sender, e)
End Sub


The following link give a way to pause or delay the process the link.



Hopefully it helps. Thanks.






share|improve this answer
























  • I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

    – Mohammad Khaled
    Nov 21 '18 at 18:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413024%2fcall-one-button-from-another-button-with-delay-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As Hans pointed out avoid calling another button. Create a private sub that both buttons can use. Then use a timer for the 20 second delay. Something like this:



'Declare a timer with a tick rate of 20 seconds
Dim Timer As New Timer With {.Interval = 20000}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearLabels2To4()
End Sub

'Create this as a separate sub, so it can be used by both buttons.
Private Sub ClearLabels2To4()
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClearLabels2To4()
'Start the 20 second timer
Timer.Start()
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
Label6.Text = ""
'Stop the timer so it doesn't continually fire every 20 seconds
Timer.Stop()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a handler for the timer ticks
AddHandler Timer.Tick, AddressOf Timer_Tick
End Sub





share|improve this answer
























  • Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

    – Mary
    Nov 21 '18 at 20:58


















0














As Hans pointed out avoid calling another button. Create a private sub that both buttons can use. Then use a timer for the 20 second delay. Something like this:



'Declare a timer with a tick rate of 20 seconds
Dim Timer As New Timer With {.Interval = 20000}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearLabels2To4()
End Sub

'Create this as a separate sub, so it can be used by both buttons.
Private Sub ClearLabels2To4()
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClearLabels2To4()
'Start the 20 second timer
Timer.Start()
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
Label6.Text = ""
'Stop the timer so it doesn't continually fire every 20 seconds
Timer.Stop()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a handler for the timer ticks
AddHandler Timer.Tick, AddressOf Timer_Tick
End Sub





share|improve this answer
























  • Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

    – Mary
    Nov 21 '18 at 20:58
















0












0








0







As Hans pointed out avoid calling another button. Create a private sub that both buttons can use. Then use a timer for the 20 second delay. Something like this:



'Declare a timer with a tick rate of 20 seconds
Dim Timer As New Timer With {.Interval = 20000}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearLabels2To4()
End Sub

'Create this as a separate sub, so it can be used by both buttons.
Private Sub ClearLabels2To4()
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClearLabels2To4()
'Start the 20 second timer
Timer.Start()
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
Label6.Text = ""
'Stop the timer so it doesn't continually fire every 20 seconds
Timer.Stop()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a handler for the timer ticks
AddHandler Timer.Tick, AddressOf Timer_Tick
End Sub





share|improve this answer













As Hans pointed out avoid calling another button. Create a private sub that both buttons can use. Then use a timer for the 20 second delay. Something like this:



'Declare a timer with a tick rate of 20 seconds
Dim Timer As New Timer With {.Interval = 20000}
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ClearLabels2To4()
End Sub

'Create this as a separate sub, so it can be used by both buttons.
Private Sub ClearLabels2To4()
Label2.Text = ""
Label3.Text = ""
Label4.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClearLabels2To4()
'Start the 20 second timer
Timer.Start()
End Sub

Private Sub Timer_Tick(sender As Object, e As EventArgs)
Label6.Text = ""
'Stop the timer so it doesn't continually fire every 20 seconds
Timer.Stop()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'Create a handler for the timer ticks
AddHandler Timer.Tick, AddressOf Timer_Tick
End Sub






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 17:07









Nathan ChampionNathan Champion

702315




702315













  • Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

    – Mary
    Nov 21 '18 at 20:58





















  • Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

    – Mary
    Nov 21 '18 at 20:58



















Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

– Mary
Nov 21 '18 at 20:58







Good answer. Not a good idea to name your Timer variable the same name as the class. Also you can skip the AddHandler if you use WithEvents. Private WithEvents DelayTimer As New Timer With {.Interval = 20000}

– Mary
Nov 21 '18 at 20:58















0














If you wanna or need to use button, the following code might be worked.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label6.Text = ""
System.Threading.Thread.Sleep(20000)
Button1_Click(sender, e)
End Sub


The following link give a way to pause or delay the process the link.



Hopefully it helps. Thanks.






share|improve this answer
























  • I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

    – Mohammad Khaled
    Nov 21 '18 at 18:53
















0














If you wanna or need to use button, the following code might be worked.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label6.Text = ""
System.Threading.Thread.Sleep(20000)
Button1_Click(sender, e)
End Sub


The following link give a way to pause or delay the process the link.



Hopefully it helps. Thanks.






share|improve this answer
























  • I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

    – Mohammad Khaled
    Nov 21 '18 at 18:53














0












0








0







If you wanna or need to use button, the following code might be worked.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label6.Text = ""
System.Threading.Thread.Sleep(20000)
Button1_Click(sender, e)
End Sub


The following link give a way to pause or delay the process the link.



Hopefully it helps. Thanks.






share|improve this answer













If you wanna or need to use button, the following code might be worked.



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
Label2.Text = ""
Label3.Text = ""
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label6.Text = ""
System.Threading.Thread.Sleep(20000)
Button1_Click(sender, e)
End Sub


The following link give a way to pause or delay the process the link.



Hopefully it helps. Thanks.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 17:53









DodeDode

62




62













  • I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

    – Mohammad Khaled
    Nov 21 '18 at 18:53



















  • I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

    – Mohammad Khaled
    Nov 21 '18 at 18:53

















I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

– Mohammad Khaled
Nov 21 '18 at 18:53





I tried your way and I got that when clicked on button2 it delays to activate button2 not button1 and didn't give me what I want!

– Mohammad Khaled
Nov 21 '18 at 18:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413024%2fcall-one-button-from-another-button-with-delay-in-vb-net%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?