VB.NET Help sectioning with distinct numbers - vb.net

What I need is this: (A, B, C, D, etc.. are chars(name of the section))
A = 0-40
B = 41-80
C = 81-120
D = 121-160
and so on
I have a combobox containing numbers from 0 to 1040, when I select numbers such as 80, the section turns into C, which should be B because it goes inside 41-80 as seen above. As well as I choose 120, it turns into D, which should be C. etc etc. Only numbers from 1-40 works perfectly. Here is my code:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim distinct1% = 1, distinct2% = 40
For x As Integer = counter To 1040
If x >= distinct1 And x <= distinct2 Then
mysection = a
Exit For
Else
a = Chr(Asc(a) + 1)
distinct1 += 40
distinct2 += 40
End If
Next
lblSectionAdd.Text = "Section: " & mysection
End Sub
I have a label, combobox, and button.

This is because when the limits are raised the value of x is incremented when the for loop starts over.
But please don't try to fix it - this method is so awkward you shouldn't put any effort in it anymore. Simply keep the counter value fixed and divide by your section size to directly compute which section the value belongs to:
Dim counter As String
Dim mysection As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
counter = cmbYearAdd.SelectedItem
Dim a As Char = "A"
Dim sectionsize as Integer = 40
Dim sect as Integer
sect = Int((counter-1) / sectionsize)
mysection = Chr(Asc("A") + sect)
lblSectionAdd.Text = "Section: " & mysection
End Sub
The division result needs to be truncated to Integer, I'm using Int() for this.

Related

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

How to make a program that would detect the same characters within two strings

so i made this but when i enter a string it would only detect one character
and it wont convert the entered string to lower case too
Dim readme, readme2 As String
Dim j, i As Integer
Dim Compare As Integer
readme = TextBox1.Text
readme2 = TextBox2.Text
readme.ToLower.Substring(i, readme.Length)
readme2.ToLower.Substring(j, readme2.Length)
For i = 0 To readme.Length
For j = 0 To readme2.Length
If readme = readme2 Then
Compare = +1
End If
Next
Next
Label4.Text = Compare`enter code here`
Strings are immutable. You cannot apply a method to a string and expects that string to change in response to the inner operations of that method.
You need to reassign the result of the operation to the same string that you have used to call the method
readme = readme.ToLower()
readme2 = readme2.ToLower()
The second part of your question is more confused, are you trying to count the number of equal chars in the same position?
In that case your loop should be
Dim maxLenToCheck = Math.Min(readme.Length, readme2.Length)
For i = 0 To maxLenToCheck - 1
If readme(i) = readme2(i) Then
Compare += 1
End If
Next
In that loop you set always the Compare to 1, the correct syntax to increment the Compare variable is
Compare += 1
Following your comment below, then I presume that your loop should be written as
Dim Compare = 0
For i = 0 To readme.Length - 1
for j = 0 to readme2.Length -1
If readme(i) = readme2(j) AndAlso _
Not Char.IsWhiteSpace(readme(i)) Then
Compare += 1
End If
Next
Next
Based on the comments in the solution by Steve, the author wants to know the count of letters occurring in both strings, ignoring case and white spaces.
By my count, however, the solution should be 21, not 20. Here is a solution using LINQ that also gives visual feedback on where those letters are located:
Public Class Form1
Private Class LetterCount
Public Letter As Char
Public Count As Integer
Public Overrides Function ToString() As String
Return Letter & " : " & Count
End Function
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "This is a Test"
TextBox2.Text = "This should be tryed before"
RichTextBox1.ReadOnly = True
RichTextBox1.Font = New Font("MS Courier", 14) ' monospaced font
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = TextBox1.Text & vbCrLf & TextBox2.Text & vbCrLf
Dim charsA As New List(Of Char)(TextBox1.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))
Dim charsB As New List(Of Char)(TextBox2.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))
Dim DistinctCommonLetters = (From A In charsA, B In charsB Where A = B Select A).Distinct
Dim DistinctCounts =
From Letter In DistinctCommonLetters, C In charsA.Concat(charsB)
Where C = Letter
Group By Letter Into Group
Select New LetterCount With {.Letter = Letter, .Count = Group.Count}
Dim TotalMatches = DistinctCounts.Sum(Function(x) x.Count)
ListBox1.DataSource = DistinctCounts.ToList
Label1.Text = "TotalMatches: " & TotalMatches
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim LC As LetterCount = ListBox1.SelectedItem
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Color.Black
Dim index As Integer = RichTextBox1.Find(LC.Letter, 0, RichTextBoxFinds.None)
While index <> -1
RichTextBox1.Select(index, 1)
RichTextBox1.SelectionColor = Color.Red
index = RichTextBox1.Find(LC.Letter, index + 1, RichTextBoxFinds.None)
End While
End Sub
End Class

Vb Write a program to print multiples of 2 and 3

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.
I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub
This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

Index was outside the bounds of the array - VB

Public Class Form1
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
a += 1
Else : a += 0
End If
If CheckBox2.Checked = True Then
b += 1
Else : b += 0
End If
If CheckBox3.Checked = True Then
c += 1
Else : c += 0
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
Else : max = max
End If
Next
Label1.Text = f(max)
End Sub
End Class
So, in summary you're using checkboxes to increment numbers every time Button1 is clicked. These values are stored in variables a, b and c, which when Button2 is clicked, are put into an integer array. From the looks of your code you're then trying to find which one of these is the largest value and display a value from another array based off that index. The problem, of course, is that you're taking the value of d and using it as the index of f. The value of max then becomes an arbitrarily large number, easily exceeding the bounds of f which in your example only has three values in it. You might want to try it more like this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim index as Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
index = i
Else : max = max
End If
Next
Label1.Text = f(index)
End Sub
This still uses max as the comparer, but takes note of the index of the For loop when it reaches a new highest number, using that to pull out the value of f, ensuring that the value of index is never higher than i.
The values of a,b and c are being incremented ever time you click the first button depending on their checked state. Then when you loop thru and get the max of the variables you are trying to use that as an indec into the f() array. The f() array can only go from 0 to 2 but max can be any value. You cannot use max to select from the array. What are you trying to do ?

Making a string that adapts itself to the text inside it

I'm making a program to solve second grade equations (e.g. 2x²+2x+2) and I was trying to have the user input the whole equation in a single text box. The computer then stores what has been typed into the text box in a string, and then it parses the string to find the coefficents. For an equation like 2x²+2x+2, the coeficients are 2, 2 and 2 and they are stored in the string at locations 0, 4 and 7. The big problem is, what if it's a bigger equation like 32x²+32x+45 or 123x²+45x+6? My logic won't work in that case. Does anyone know how to do it?
Here's my code that only works for the small equations:
Public Class Form1
Dim i1 As Double
Dim i2 As Double
Dim i3 As Double
Dim delta As Double
Dim x1 As Double
Dim x2 As Double
Dim leters As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i1 = T1.Text
i2 = T2.Text
i3 = T3.Text
delta = (i2 * i2) - 4 * (i1 * i3)
If (delta < 0) Then
Ld.Text = delta
L1.Text = "Impossível"
L2.Text = "Impossível"
Else
x1 = (-i2 + Math.Sqrt(delta)) / (2 * i1)
x2 = (-i2 - Math.Sqrt(delta)) / (2 * i1)
Ld.Text = delta
L1.Text = x1
L2.Text = x2
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
GroupBox1.Text = "Equação"
GroupBox1.Width = 200
GroupBox1.Height = 58
T1.Width = 188
T3.Hide()
T2.Hide()
Label1.Hide()
Button1.Hide()
Button2.Show()
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
GroupBox1.Text = "Coeficientes"
GroupBox1.Width = 200
GroupBox1.Height = 143
T1.Width = 119
T3.Show()
T2.Show()
Label1.Show()
Button1.Show()
Button2.Hide()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
leters = T1.Text
leters.ToString()
End Sub
End Class
You could use RegEx to parse the string, which would be nice because you could change the RegEx expression as a setting without rebuilding your assembly. However, if the equation string format is always going to be the same, as appears to be the case here (because the rest of the code would fail if it weren't), then you could simply use String.Split to parse the string. For instance:
Dim equation As String = "32x²+32x+45"
Dim parts() As String = equation.Split(New Char() {"+"c, "x"c}, StringSplitOptions.RemoveEmptyEntries)
Dim coefficient1 As Integer = Integer.Parse(parts(0))
Dim coefficient2 As Integer = Integer.Parse(parts(2))
Dim coefficient3 As Integer = Integer.Parse(parts(3))