Making a random array of numbers, with each number being unique - vba

I'd like for a string of 24 numbers to appear in the text box, and then for each to have a random number from 0 to 23.
so far I have the following code made, but it makes a duplicate number appear occasionally.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
Dim Output As String = String.Empty
Dim NumberArray(24) As Integer
For n As Integer = 0 To 23
Dim RandomNumber As Integer = CInt(Int(Rnd() * 25) + 1)
NumberArray(n) = RandomNumber
Output &= "Index #" & n.ToString & Space(4) & NumberArray(n) & vbCrLf
Next
TextBox1.Text = Output
End Sub
End Class
any help greatly appreciated!

Related

Sorting Numbers in Textbox in VB.Net

I need help with my program I want my output box to be in ascending order like:
0
1
2
3
4
5
6 and so on
but my output is like this 1234567890 and so on.
Here's my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x
Next
TextBox2.Text = strNumbers
End If
End Sub
End Class
I believe I may have solved your problem :)
If you change textbox2 into a rich textbox, it will now allow you to output multiple lines
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x & vbNewLine & vbNewLine
Next
RichTextBox1.Text = strNumbers
End If
End Sub
Here is a little something I threw together to test the output

How to insert a numbered bullet list in richtextbox

I am trying to insert a numbered bullet list when a user selects the button it will increment the counter
I have searched questions online and different types of loop condition statements
Private Sub ToolStripButton16_Click(sender As Object, e As EventArgs) Handles ToolStripButton16.Click
ToolStripButton16.CheckOnClick = False
Dim i As Integer
i = 0
Dim numbList As String
Dim buttonClickCount As Integer
buttonClickCount = 0
Do While (i = buttonClickCount)
i += 1
numbList = " " & i & "." & vbCrLf
RichTextBox1.AppendText(numbList)
Loop
buttonClickCount += 1
End Sub
Expected result:
1.
2.
3.
4.
Probably not the most elegant solution but it should put you in the right direction.
'Using a class variable here allows you to keep an "application state".
Dim buttonClickCount as Integer = 0
Private Sub ToolStripButton16_Click(sender As Object, e As EventArgs) Handles ToolStripButton16.Click
ToolStripButton16.CheckOnClick = False
buttonClickCount += 1
Dim newValue As String = " " & buttonClickCount & "." & vbCrLf
RichTextBox1.AppendText(newValue)
End Sub

How would I insert items from textbox into listview?

I have a listview with two columns. I also have a textbox. In the textbox, there are lines with strings that I want to insert into the listview.
Every 1st line will be inserted into the first column and every 2nd line will be inserted into the second column. How would I achieve this.
This is my current code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox3.Text = My.Resources.clsid
Dim ListArr(230) As String
Dim i As Integer = 0
For Each line As String In TextBox3.Lines
ListArr(i) = line(i)
i += 1
For Each litem As String In ListArr
AddLitem(litem, litem)
Next
Next
End Sub
Public Function AddLitem(ByVal Desc As String, ByVal CLSID As String)
Dim litem As ListViewItem
If i = 0 Then
Lstr(0) = Desc
i += 1
ElseIf i = 1 Then
Lstr(1) = Desc
litem = New ListViewItem(Lstr)
ListView1.Items.Add(litem)
ListView1.Items.RemoveAt(ListView1.Items.Count)
End If
Lstr(0) = Desc
'Lstr(1) = CLSID
End Function
Note:
You can us Math.Mod to check if you have to handle the first or second row.
For example
0 mod 2 result 0
1 mod 2 result 1
2 mod 2 result 0
3 mod 2 result 1
I use ListView1.Clear to make sure, i'm refilling an empty ListView.
Example Code. This copies all lines from the TextBox1 to the ListView1.
Separeted by a pipe |.
Delete the IIf(String.IsNullOrEmpty(strView2), "", "|") & if you don't want this separation.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strView1 As String = ""
Dim strView2 As String = ""
For i As Integer = 0 To TextBox1.Lines.Count - 1
If (i Mod 2) Then
strView2 &= IIf(String.IsNullOrEmpty(strView2), "", "|") & TextBox1.Lines(i)
Else
strView1 &= IIf(String.IsNullOrEmpty(strView1), "", "|") & TextBox1.Lines(i)
End If
Next
ListView1.Clear()
ListView1.Items.Add(strView1)
ListView1.Items.Add(strView2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "cat" & vbCrLf & "street" & vbCrLf & "dog" & vbCrLf & "house" & vbCrLf & "bird" & vbCrLf & "garden"
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