GUI - Digits in a textbox loop not working - vb.net

Public Class Form1
Public digits As String = "0123456789"
Public userInput As String
Public digitCount As Integer = 0
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
userInput = Console.ReadLine()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each i As Char In userInput
If digits.Contains(i) Then digitCount += 1
Next
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub MaskedTextBox1_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected
End Sub
End Class
What should my loop be since this one isn't working and what is the syntax for entering in my digitCount in my maskedtextbox1

Replace your loop with the following simple statement:
digitCount = userInput.Count(Function(c) Char.IsDigit(c))
N.B. Requires reference to System.Linq, which is there by default.

Related

create function for sender object handler

how do i create a function for a sender as object click? I tried something, but it didn't work out.
Private Function functionName(ByVal sender As Object, e As EventArgs)
If sender.checked = True Then
For i As Integer = 2 To 14
If i <> 2 Then
Dim cbClubs = DirectCast(Controls.Item("cbBtt" & i & "detrefla"), CheckBox) 'Clubs
cbClubs.Checked = False
End If
Next
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla, sender)
End Sub
Let's put some order in this code.
Private Function functionName(ByVal sender As Object, e As EventArgs)
'...
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla, sender)
End Sub
Why are you using e As EventArgs in functionName(...) declaration? It's never used, so u can delete it:
Private Function functionName(ByVal sender As Object)
In functionName you are evaluating sender.Checked (a tip: you can use If sender.Checked instead of If sender.Checked = True): why not to define sender as CheckBox, instead of Object?
Private Function functionName(ByVal sender As CheckBox)
Now, let's see the for Loop:
For i As Integer = 2 To 14
If i <> 2 Then
'...
End If
Next
You are evaluating i from 2 to 14, but you don't want to do anything if i = 2. Why not to start the for loop from i=3? It's faster and better.
For i As Integer = 3 To 14
'...
Next
Another thing: functionName() doesn't produce an output, so it should be a Sub, not a Function.
Now, your code is:
Private Sub functionName(ByVal sender As CheckBox)
If sender.Checked Then
For i As Integer = 3 To 14
'...
Next
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
functionName(cbBtt2detrefla)
End Sub
Furthermore, if functionName is called only by Button1_Click and here it is only used with cbBtt2detrefla, you can avoid separating the two subs:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If cbBtt2detrefla.Checked Then
For i As Integer = 3 To 14
'...
Next
End If
End Sub
Last, but not least:
Dim cbClubs = DirectCast(Controls.Item("cbBtt" & i & "detrefla"), CheckBox) 'Clubs
cbClubs.Checked = False
This works only if cbBtt3detrefla and the others checkBoxes are directly on the form. If they are in a Panel, for example, you'll get a System.NullReferenceException. If you want to iterate on every control, you can do something like this.

How to check if the return value its null vb.net

I start to make an application that automatic press a button on my webpage
the code its works perfect but how do i check if the return value its null?
This is my code
Public Class Form5
Dim CheckButton, skip_button As HtmlElement
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
skip_button = WebBrowser1.Document.GetElementById("skip_button")
CheckButton = WebBrowser1.Document.GetElementById("skip_button")
skip_button.InnerText = "skip_button" 'Replace testID by the ID you want
End Sub
End Class
You can check the result of GetElementById like the following:
Public Class Form5
Dim CheckButton, skip_button As HtmlElement
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
skip_button = WebBrowser1.Document.GetElementById("skip_button")
CheckButton = WebBrowser1.Document.GetElementById("skip_button")
If Not skip_button Is Nothing Then
skip_button.InnerText = "skip_button" 'Replace testID by the ID you want
End If
If Not CheckButton Is Nothing Then
'some code here
End If
End Sub
End Class

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

add subitems to a listview

I'm using a backgroundworker to populate a listview, but i want to add subitems also. Can anyone help me out?
Public Class Form1
Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Dim li As New List(Of ListViewItem)
For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
li.Add(New ListViewItem(My.Computer.FileSystem.GetName(fn)))
'here i want to add a subitem containing the filesize
'My.Computer.FileSystem.GetFileInfo(fn).Length
Next
e.Result = li.ToArray
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
lv.Items.AddRange(DirectCast(e.Result, ListViewItem()))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
bgw.RunWorkerAsync()
End Sub
End Class
Try this in your For Each loop:
Dim NewItem as New ListViewItem(My.Computer.FileSystem.GetName(fn))
NewItem.SubItems.Add(My.Computer.FileSystem.GetFileInfo(fn).Length)
li.Add(NewItem)
Hopefully that should do the trick
this is working too, but is it correct?
Public Class Form1
Dim item1 As String = ""
Dim item2 As String = ""
Private Sub bgw_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
Dim progress As Integer = 0
'calculate progress later
progress = 1
For Each fn As String In My.Computer.FileSystem.GetFiles("s:\Videos", FileIO.SearchOption.SearchAllSubDirectories, "*.*")
item1 = My.Computer.FileSystem.GetName(fn)
item2 = My.Computer.FileSystem.GetFileInfo(fn).Length
bgw.ReportProgress(progress)
Next
End Sub
Private Sub bgw_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles bgw.ProgressChanged
Dim li As New ListViewItem
li = lv.Items.Add(item1, 0)
li.SubItems.Add(item2)
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lv.Items.Clear()
bgw.RunWorkerAsync()
End Sub
End Class