vb.net button percentage

hello there,
I would like to create a little button that will display "hello" in textbox1.text and display "bye" in textbox2.text
the thing that I need help is that if I press the button 100 times, I want to display 60% hello and 40% bye..
how can I do that?
LVL 1
XK8ERAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
use random

on button click

Dim num As Integer = random.Next(100)
if num>60 then
  textbox1.text = "Bye"
else
  textbox2.text = "Hello"
end if
0
XK8ERAuthor Commented:
just for testing I have this but its showing an error

(Random.Next) Error      1      Reference to a non-shared member requires an object reference.      
WindowsApplication1\Form1.vb      2      26      WindowsApplication1

Public Class Form1
    Dim num As Integer = Random.Next(100)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If num > 60 Then
            TextBox1.Text = TextBox1.Text + 1
        Else
            TextBox2.Text = TextBox2.Text + 1
        End If
    End Sub
End Class

Open in new window

0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Try this out:
Public Class Form1

    Private R As New Random
    Private HelloCount As Integer
    Private ByeCount As Integer
    Private TotalCount As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dbl As Double = R.NextDouble

        TextBox1.Text = IIf(dbl <= 0.6, "Hello", "")
        HelloCount = HelloCount + IIf(dbl <= 0.6, 1, 0)
        TextBox2.Text = IIf(dbl > 0.6, "Bye", "")
        ByeCount = ByeCount + IIf(dbl > 0.6, 1, 0)

        TotalCount = TotalCount + 1
        Label1.Text = "Hello Count: " & HelloCount & " / Percentage: " & ((HelloCount / TotalCount) * 100) & "%"
        Label2.Text = "Bye Count: " & ByeCount & " / Percentage: " & ((ByeCount / TotalCount) * 100) & "%"
    End Sub

End Class

Open in new window

0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

XK8ERAuthor Commented:
I wish it wasnt that confusing Idle_Mind..
0
Moe DeShongCommented:
Homework?
I don't believe random will work.  A random number could just as easily be the number 5 100 times in a row.  If you're only allowed 100 clicks how will you know if hello is displayed two times in a row unless you erase the text boxes?  Why not just click the button 60 times and display hello then if the number of clicks is over 60 display bye 40 times.  If that is really all the criteria you have it's just a simple count to 60.

dim counter as integer=0
if counter <61 then
textbox1.text=hello
counter=counter+1
else
textbox2=bye
end if

Somehow I think there must be more to the problem.
0
XK8ERAuthor Commented:
either way with that code it always shows different percentage.. not 60/40
0
XK8ERAuthor Commented:
no it doesnt really work 60 counts then 40 counts.. im looking for more simultaneous counts
0
Moe DeShongCommented:
I don't understand.  60 hellos and 40 bye is 60% and 40%
0
Moe DeShongCommented:
"im looking for more simultaneous counts"
You want more randomness???
0
XK8ERAuthor Commented:
but it fails after 110 click.. its about percentage not 60 hellos
0
XK8ERAuthor Commented:
lets say the perfect its set to 66% and 33%

hello
hello
bye
hello
hello
bye

etc...
0
Moe DeShongCommented:
You're not stating what you want very well.  If it about percentage the code gives you that percentage.  Restate the problem.  The code given achieves what you asked for.
0
Moe DeShongCommented:
So, you want the hello and bye mixed up for some reason.  Correct?
0
XK8ERAuthor Commented:
and which code would that be?
0
XK8ERAuthor Commented:
yes mixed up
0
Moe DeShongCommented:
Here's your problem, even if you do a simple equation it's going to look symetrical.  You have to have 1.5 hello for every 1 bye.   60 divided by 40.  so there's really no way to make it truly random.  At some point you'll have to check a counter and throw in several hello or several bye to make the percent correct.  How do you want it to look?
0
Moe DeShongCommented:
dim iHello as integer
dim iBye as integer

If iHello>than ibye*1.5 then
textbox2.text=bye
iBye=ibye+1
else
textbox1=hello
iHello=iHello+1
end if

Give me a minute and I'll run this and make sure it works.  Probably have to add one more if to make sure it works out at the end
0
XK8ERAuthor Commented:
i dont mind how many hellos or bye.. but it cant be 60 hellos then AFTER 40 byes
0
Moe DeShongCommented:
ok, this is tested and works perfect.
declare these two variables at the form level

  Dim iHello As Integer
   Dim iBye As Integer

In the button click event put this code:

      If iHello > iBye * 1.5 Then
         Me.text2.Text = "bye"
         iBye = iBye + 1
       Else
         Me.text1.Text = "hello"
         iHello = iHello + 1
      End If
   
0
Moe DeShongCommented:
Check it out.  I'll stay online for a few minutes in case you have a problem.  Let me know.
0
Moe DeShongCommented:
I added three additional text boxes.  One keeps track of the number of times hello has been printed, the next keeps track of the number of times bye is printed and the third counts the number of times the button has been pushed.  I added a second button that clears all the variables.  Here's the complete code for the form:
 
Public Class Form1
   Dim iHello As Integer
   Dim iBye As Integer


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

      If iHello > iBye * 1.5 Then
         Me.text2.Text = "bye"
         iBye = iBye + 1
         Me.txtbyecount.Text = iBye.ToString
      Else
         Me.text1.Text = "hello"
         iHello = iHello + 1
         Me.txthellocount.Text = iHello.ToString
      End If
      Me.txtbuttonClick.Text = iHello + iBye
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      Me.text1.Text = Nothing
      Me.text2.Text = Nothing
      iBye = 0
      iHello = 0
      Me.txthellocount.Text = Nothing
      Me.txtbyecount.Text = Nothing
      Me.txtbuttonClick.Text = Nothing
   End Sub
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
XK8ERAuthor Commented:
alright that works great.. the 1.5 is 60/40 right? how do I know maybe 70/30?
0
Moe DeShongCommented:
70 divided by 30
2.3333333333333333

0
XK8ERAuthor Commented:
thanks a lot.. that was amazing!
0
Moe DeShongCommented:
glad to help.  thanks for the points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.