04-23-2011, 01:07 PM
(This post was last modified: 04-23-2011, 01:09 PM by AceInfinity.)
(04-23-2011, 12:27 PM)The High Roller Wrote: Hey thanks Infinity, this is actually helpful for my math. I am in Algebra 1 and we use factors ALL the time. Thanks again! PROPS!
Sidenote: Is there a source code please?
I'll give you the basic function I used and other things like how I got the enter key to perform the task of button 1 which handled that function to a string.
Here's the rule I used to create the method for finding factors. If the input in the textbox was 1 I just manually created the output for the factors in the listbox and didn't use this code at all. (Using an if statement and Exit Sub command before this code).
Code:
'"1" will always be a factor so we'll always add it (with the exception of 0)
ListBox1.Items.Add("1")
'Defining how we find the factors of the number in Textbox1
Dim N, x As Integer
N = Val(TextBox1.Text)
For x = 2 To N - 1
If N Mod x = 0 Then
ListBox1.Items.Add(x)
End If
Next
'This is where we add the value of N (All the factors) to the listbox
ListBox1.Items.Add(N)
'Show the number input from Textbox1 into Label2 to confirm which number we are searching for factors (Just something fancy)
Label2.Text = TextBox1.Text
'Show in another label how many factors there are (how many listbox items there are)
Label11.Text = ListBox1.Items.Count
'Otherwise if listbox has 2 factor values, it has to be 1 and another number (itself) meaning it's a prime number
If ListBox1.Items.Count = 2 Then
'So display the value of Label12.Text to show that it's a prime number with this message
Label12.ForeColor = Color.DarkOrange
Label12.Text = "(This is a prime number!)"
Else 'Otherwise, it's not a prime number, so don't show anything
Label12.Text = ""
End If
Here's for pressing Enter which will perform the task of button 1 click.
Code:
'Keypresses for in Textbox1
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'If the key pressed is Enter
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
'Then perform the code for the Find Factors button
Find_Factors.PerformClick()
End If
Here is the code I used to only allow numbers into the textbox for the input
Code:
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'Only allow numeric entries for the textbox
If AllowedChars.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
End If
End Sub
And I defined AllowedChars with this at the top under the Public Class
Code:
Dim AllowedChars As String = "0123456789" & vbBack
I'm glad you find it useful though, that's one of the reasons I created this besides the fact that I was just fooling around in Visual Basic a while ago. I knew at least a couple people would find it useful for one of their math courses.