create function for sender object handler - vb.net

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.

Related

How to delay in adding items in list box vb.net

I am using for, while & do while loop in vb.net to add some values in list box. I want to use a timer to delay in adding values to list box. Please tell the syntax, what should I use & Following is my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer
For a = 1 To 10
ListBox1.Items.Add(a)
End If
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim b As Integer
b = 4
If b < 5 Then
MessageBox.Show("eRROR")
End If
While b <= 3
ListBox2.Items.Add(b)
b = b + 1
End While
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim c As Integer
c = 20
Do
c = c + 1
ListBox3.Items.Add(c)
Loop While c <= 30
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
A timer executes once every n milliseconds, you take advantage of this by handling the timer's Tick event. If you wanted to keep track of what increment you're on, either declare a private variable outside of the scope (see here) of the Tick event or declare a static variable inside the Tick event.
Private Variable:
Private counter As Integer = 0
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
counter += 1
If (counter < 31) Then
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub
Static Variable:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Static counter As Integer = 0
If (counter < 31) Then
counter += 1
ListBox1.Items.Add(counter)
Else
Timer1.Stop()
End If
End Sub

How to remove the last item in the list(Of) in vb.net

I have a windows form application which added string into list of collection. This can be done by input the string into the textbox then click 'add' button, and the list will display in a listbox.
Now, I want to delete the last item in the list collection & the listbox.
Below are the code snippett that I have done
Public strList As List(Of String) = New List(Of String)
'add string to list
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TxtBox.Text <> "" Then
strList.Add(TxtBox.Text)
TxtBox.Clear()
End If
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lstItem.Items.Clear()
strList.ForEach(AddressOf ListItem)
End Sub
'Add item into list
Public Sub ListItem(s As String)
lstItem.Items.Add(s)
'lstItem.Sorted = True
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
strList.ToList.ForEach(AddressOf DeleteItem)
End Sub
'Delete item
Public Sub DeleteItem(s As String)
For i = 0 To strList.Count
lstItem.Items.RemoveAt(strList.Count - 1)
i = i + 1
Next
End Sub
as you can see, in the sub DeleteItem, i try to delete the last item of the list collection by clicking 'delete button'. but the error says Additional information: InvalidArgument=Value of '1' is not valid for 'index'.
can anyone help me on this? thank you.
What you really ought to do is use a BindingList(Of String) and bind it to the ListBox. That way, you only have to deal with one list. Adding and removing against the underlying BindingList will automatically affect the ListBox:
Private items As New BindingList(Of String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DataSource = items
End Sub
Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click
items.Add(TextBox1.Text)
End Sub
Private Sub deleteSelectedButton_Click(sender As Object, e As EventArgs) Handles deleteSelectedButton.Click
items.RemoveAt(ListBox1.SelectedIndex)
End Sub
Private Sub deleteLastButton_Click(sender As Object, e As EventArgs) Handles deleteLastButton.Click
items.RemoveAt(items.Count - 1)
End Sub

Enable a counter at a specific time everyday

I setup a counter which needs to be enabled at specific time everyday. for an example lets say at (3 PM) everyday. what i came up with is following piece of code. but it gives me an error when it reach the time saying parameter is not valid please help me,
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
Execute()
End Sub
Private Sub Execute()
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Try this, add a timer to your program and call it CheckTimer and update your code like this :
Private t As Integer = 0
Private Sub Home_monitoring_tab_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rs.FindAllControls(Me)
CheckTimer.Interval = 1
CheckTimer.Start
End Sub
Private Sub CheckTimer_Tick(sender As Object, e As EventArgs) Handles CheckTimer.Tick
If DateTime.Now.ToString("HH:mm") = "15:00" Then
shift1_timer.Enabled = True
End If
End Sub
Private Sub shift1_timer_Tick(sender As Object, e As EventArgs) Handles shift1_timer.Tick
t += 1
Label14.Text = CStr(t)
End Sub
Hope it helped :)

Where to add the code for the regular expression to ensure that only numbers are accepted [duplicate]

This question already has answers here:
How do I make a textbox that only accepts numbers?
(41 answers)
Closed 8 years ago.
I am trying to add regular expression to the code below to ensure that only numbers are accepted. The code is very basic it calculates the area of a square and put the result in a RichTextBox.Text
I am using VB Visual Studio 2012. Any help will be greatly appreciated.
------------------------------------------------------
Public Class SquareArea
Inherits ShapeArea
Public Overrides Function Area() As Double
Return (Me.Lengh ^ 2)
End Function
End Class
------------------------------------------------------------
Public Class Square
Private Sub Square_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ResultButton_Click(sender As Object, e As EventArgs) Handles ResultButton.Click
Dim area = New SquareArea
area.Lengh = SideTextBox.Text
ResultRichTextBox.Text = area.Area()
End Sub
Private Sub CloseSquareButton_Click(sender As Object, e As EventArgs) Handles CloseSquareButton.Click
Me.Close()
End Sub
End Class
There are several ways of doing this. But the best would be to use the Validating Event of the SideTextBox textbox.
Private Sub SideTextBox_Validating (ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSideTextBox.Validating
'your code here
End Sub
or
You could also use its KeyPress Event so the user is prompted whenever they enter a non-numeric character.
Private Sub SideTextBox_KeyPress (ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSideTextBox.KeyPress
'your code here
End Sub
Use this code...
Private Sub txtValue_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue.KeyPress
If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
I don't usually write in Visual Basic but you are looking to add RegularExpression to your class as a Class Member (System.Text.RegularExpressions namespace). Regular Expression Pattern shown below will allow only digits. Calling the Match method on regex returns a Match class which you can call Success on for a Boolean result (true/false)
You may have to make slight changes as I don't normally write in VB, but the expression and class are correct
'Declare Regular Expression Class, Compile once
Dim RegularExpression regex As RegularExpression = New RegularExpression("^[0-9]*$", RegExOptions.Compile)
Private Sub Square_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub ResultButton_Click(sender As Object, e As EventArgs) Handles ResultButton.Click
Dim area = New SquareArea
' Insert Magic Here
If regex.Match(SideTextBox.Text).Success=False Then
MessageBox.Show("Invalid entry")
Return
End If
area.Lengh = SideTextBox.Text
ResultRichTe...
Private Sub CloseSquareButton_Click(sender As Object, e As EventArgs) Handles CloseSquareButton.Click
Me.Close()
End Sub
End Class

GUI - Digits in a textbox loop not working

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.