Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 19th, 2006, 11:17 AM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple Program Help

So i'm a newbie to VB and i need some help doing something that is so simple i cant work it out.

I have a form that has a button, on clicking the button it pings a computer.

Code:
Private Sub btnPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPing.Click

        Dim Count As Integer


        If My.Computer.Network.Ping("10.50.50.217") Then
            lblDisplay.Text = "Server pinged successfully."
            Count = 0
            lblcount.Text = Count

        Else
            lblDisplay.Text = "Ping request timed out."
            Count = Count + 1
            lblcount.Text = Count
        End If

    End Sub


So i'm thinking people are already calling me stupid :). What i want to happen is when the button is clicked and the ping is ok it sets the counter (lblcount) to 0. Which works.

But if the ping fails i want the counter to count how many in a row. So if i click the button 4 times and ping fails 4 times the lblcount should display 4. Then if i click it and ping is ok the lblcount goes back to 0.

I've got it displaying 1 after the first fail but then stays. Should i have made this a global variable or a class??

Please Help
I do learn from my mistakes.

Thanks

 
Old December 19th, 2006, 11:34 AM
Authorized User
 
Join Date: Nov 2006
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ef1196
Default

Each time you click your button you are resetting the value of COUNT to zero.
Move your variable declaration for COUNT to the form level and your code will work.

Public Class Form1

    Dim Count As Integer

    Private Sub btnPing_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPing.Click

        If My.Computer.Network.Ping("10.50.50.217") Then
            lblDisplay.Text = "Server pinged successfully."
            Count = 0
            lblcount.Text = Count

        Else
            lblDisplay.Text = "Ping request timed out."
            Count = Count + 1
            lblcount.Text = Count
        End If

    End Sub

End Class



Best Regards,
Earl Francis
 
Old December 19th, 2006, 11:51 AM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much that works a treat.

Now to add a clock, telnet and more code :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple assembly program runtime error vraifreud Visual C++ 2005 0 September 21st, 2007 01:20 AM
Simple Q Apocolypse2005 Beginning VB 6 5 August 8th, 2007 08:04 AM
Simple Java Program WargeMaster Java GUI 1 September 9th, 2005 12:25 PM
Setup Project: Program not added in Start>Program arif_1947 VS.NET 2002/2003 2 March 31st, 2005 06:40 AM
Start a program inside another program Silje Classic ASP Professional 1 November 16th, 2004 02:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.