I want to Increase employee Id as E1,E2,E3 and so on how to do this - vb.net-2010

I want to Increase employee Id as E1,E2,E3 and so on how to do this....Val(TextBox19.Text) + 1 I have done this to Increase 1,2,3 but how to append E at the start of every number in VB.net

I made this by going all sneaky about it...
But I think this is what you're looking for. With each click on the button, you get "E" + the incremented number.
Interested by the idea, I ended up on this for 5 hours :) (Nothing better to do)
Public Class Form1
'When Form1 loads.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'We hide TextBox1
TextBox1.Hide()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'This will increment +1 in the hidden textbox.
Dim i As Integer
i = Val(TextBox1.Text) + 1
TextBox1.Text = i
'We dim the result of the hidden textbox as string, then finally connect "Add" with "Result"
Dim Add As String
Add = "E"
Dim Result As String
Result = TextBox1.Text
TextBox2.Text = Add & Result
End Sub
End Class

Related

How to transfer data from listbox to textbox in Visual Basic

I did some activity but I cant figure out how to retrieve data from listbox1 to textbox1. Example in the listbox1 there are 4 names: John, Jorge, Joe. Then I want to transfer Joe from listbox1 to textbox1. I did an arraylist where I adding those 3 names in the listbox1 but I didn't know how to retrieve the name "Jorge" from listbox1 to textbox1. Send help.
Here's the code where I try to retrieve one of the name from listbox1 to textbox1
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Here's the whole code
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If textbox1.Text = ListBox1.Items.Count Then
textbox1.Text = ArrayofNames(x)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
Private Sub removeBtn_Click(sender As Object, e As EventArgs) Handles removeBtn.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
End Class
Here's the Image of the interface i try to retrieve the name Joe but it wasn'tshowing
Let's go over the code you posted.
In the retrieve button click event you are comparing and Integer to a String with textbox1.Text = ListBox1.Items.Count This won't compile with Option Strict On. You do have Option Strict On, don't you? You should always have this on.
On the next line, you assign the value of ArrayofNames(x), where x refers to your form level variable, to a text box's Text property. Since the value of x is never changed anywhere in the code this will always return the 0 index in the array. (The first element) I can't imagine why it should matter that the Textbox.Text should equal the count of the ListBox items.
In the add button click event you first declare a local variable x. Any code in the method will use this x, not the form level x. You ReDim (without the Preserve keyword) the array to an array with a single element. Your For loop is silly because the length of ArrayofNames is 1, so it is 0 To 0.
The ArrayofNames will never have more than a single element and it will be overwritten each time the add button is clicked.
Fortunately the Listbox maintains its own collection of items.
We can use this collection to simplify your code. Just add the contents of the text box to the the list items.
To retrieve a value from the list box you must first determine if the user has entered a valid index. First, is the entry a valid Integer and is it an index present in the list box.
Remember that .net collections start at index 0.
I use default names in my test program but you should continue to use meaningful names for your controls.
'Your add button
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim index As Integer
If Integer.TryParse(TextBox1.Text, index) AndAlso index >= 0 AndAlso ListBox1.Items.Count - 1 >= index Then
TextBox2.Text = ListBox1.Items(index).ToString
End If
End Sub
'Your add button
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Add(TextBox3.Text)
End Sub

Having trouble with a lottery software

sorry to come in here like this looking for answers, but im actually really stumped. Im supposed to make an application using 2 forms, one to generate its own array and store user entered numbers into another array, and then display them on the second form and tell you how many of the numbers are in common
I cant get the numbers to show up on the second form at all, and I also cant really understand how to get how many numbers are matching, but I will when I can reference them properly in form 2. I got the arrays set up perfectly, but I cant even call the numbers from them to form 2. I need help calling the arrays from the first form to display on the next
Form 1 code
Public Class Form1
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Shared random As New Random()
Dim UserArray(4) As String
Dim LotteryArray(4) As String
Public Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
If txtNumbers.Text <> "" Then
For i As Integer = 0 To 4
If UserArray(i) = "" Then
UserArray(i) = txtNumbers.Text
End If
Next
End If
For n As Integer = 0 To 9
Dim RandomNumber As Integer = CInt(Int(Rnd() * 5) + 1)
LotteryArray(4) = CStr(RandomNumber)
Next
End Sub
End Class
Form 2 Code
Public Class Form2
Public Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
lblUser1.Text = Form1.UserArray(0)
lblUser2.Text = Form1.UserArray(1)
lblUser3.Text = Form1.UserArray(2)
lblUser4.Text = Form1.UserArray(3)
lblUser5.Text = Form1.UserArray(4)
lblRand1.Text = Form1.LotteryArray(0)
lblRand2.Text = Form1.LotteryArray(1)
lblRand3.Text = Form1.LotteryArray(2)
lblRand4.Text = Form1.LotteryArray(3)
lblRand5.Text = Form1.LotteryArray(4)
If Form1.LotteryArray(4) = Form1.UserArray(4) Then
MessageBox("CONGRATULATIONS!", "You Are The GRAND PRIZE WINNER!")
End If
Me.Close()
End Sub
End Class
I will address your first question. How to get your numbers into your second form.
You main problem is the Access modifier you used for your arrays. Dim is the equivalent of Private which means that the variable is only visible within the class. Friend is visible anywhere in the assembly. See https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/access-levels for more information. Don't change your event procedure Access to Public.
You have to let the user fill in another guess. The first loop in the Check button will only enter the same input 5 times. You need one entry on every click but you need to keep track of where you are in the array. Private NumberOfGuesses As Integer Note that this variable is Private. It doesn't need to be seen outside of the Form1 class.
We don't need to refill the LotteryArray on every click so I did it once in the Form.Load You were trying to use the Random class the way you use the old vb6 Random. The .net class is much easier.
To display the arrays on Form2, I used list boxes and loops. Saved a bit of typing.
If you still have a question about part 2 of your problem post another question. Show what you have tried.
Public Class Form1
Private random As New Random()
Friend UserArray(4) As Integer
Friend LotteryArray(9) As Integer
Private NumberOfGuesses As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For n As Integer = 0 To 9
Dim RandomNumber As Integer = random.Next(0, 10)
LotteryArray(n) = RandomNumber
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If NumberOfGuesses > 4 Then
MessageBox.Show("You have used up your guesses")
Form2.Show()
Me.Close()
End If
If TextBox1.Text <> "" Then
UserArray(NumberOfGuesses) = CInt(TextBox1.Text)
NumberOfGuesses += 1
TextBox1.Clear()
TextBox1.Focus()
End If
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each number In Form1.UserArray
ListBox1.Items.Add(number)
Next
For Each number In Form1.LotteryArray
ListBox2.Items.Add(number)
Next
End Sub
End Class

Multiple Click Incrementation

I am incrementing a variable (+1) everytime a PictureBox gets clicked but the count is stuck at 1, not going to 2 or 3 or 4 everytime the PictureBox is clicked.
Dim AddIncrement As Integer
AddIncrement = AddIncrement + 1
Dim Menu As String = AddIncrement
TextBox1.Text = Menu
This is inside a method, right? With a signature that looks something like this:
Private Sub PictureBox1_Click(sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
This may not be 100% exact, but it should be close. So what you really have looks more like this:
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
Dim AddIncrement As Integer
AddIncrement = AddIncrement + 1
Dim Menu As String = AddIncrement
TextBox1.Text = Menu
End Sub
Now we need to look at the AddIncrement variable. The Dim statement for this variable is inside the method. That is, it is redefined every time the method runs. Each method call is actually working with a different variable.
To fix it, move the variable declaration outside the method, to be at the Class/Module level:
Private AddIncrement As Integer = 0
Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click
AddIncrement += 1
TextBox1.Text = AddIncrement.ToString()
End Sub

How to add text after every textbox line in VB.NET

I've searched everywhere, I can't find a way.
I want to find a way to add text after every textbox line but I can't find a way to do it.
I have a textbox1 with:
example1
example2
example3
And so on...
and another textbox2 with #gmail.com
I want the textbox2 to be added to the end of every line in textbox1 like:
example1#gmail.com
example2#gmail.com
example3#gmail.com
And so on...
Any way to do it? Thanks in advance.
This solution is concise, and removes empty lines.
Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
Return String.Join(Environment.NewLine, otherText.
Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
Select(Function(s) s & textToAppend))
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Here's your example working
And if you had an empty line, it is removed in the resulting string
Of course, you could overwrite the original textbox instead, but careful not to click the button twice!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
TextBox1.SelectionStart = TextBox1.Text.Length
End If
End Sub
(this option requires some discipline when pressing enter)
This is full working code.Enjoy it!!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Lines.Length = 0 Then
MsgBox("Please enter some data in textbox1")
Exit Sub
End If
'---------------------------
Dim b(Me.TextBox1.Lines.Length - 1) As String
Dim i As Integer = 0
For Each a As String In Me.TextBox1.Lines
b(i) = a + Me.TextBox2.Text
i = i + 1
Next
'-----------------
Me.TextBox1.Clear()
Me.TextBox1.Lines = b
End Sub

Moving average method VB

i'd try to make moving average in vb
i want to check the cells and set the value to text box
but the result is all the text box has the same value
how to make my first check value (penjualan/bulan) is inputed into first text box and the second check (penjualan/bulan) to second text box.
here is my code
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.ColumnIndex = 5 Then
tb1.Text = DataGridView1.CurrentRow.Cells(3).Value
tb2.Text = DataGridView1.CurrentRow.Cells(3).Value
tb3.Text = DataGridView1.CurrentRow.Cells(3).Value
End If
End Sub
thanks.
You set EVERY time the cellClicked-event is raised all 3 Textboxes to the same value, CurrentRow.Cells(3).Value.
Another problem is that your code will set the text in the Textboxes always. It dont check if the Checkbox is checked or not. it just updated every time you click in any cell in this column, the text in the 3 boxes to the value of your currently selected row.
Here you have a solution. Its not perfect but should work, although you should try to understand and optimize it.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitializeDgv()
End Sub
Private Sub InitializeDgv ()
Dim row as String() = New String(){"2016",240}
DataGridView1.Rows.Add(row)
row = New String(){"2017",223}
DataGridView1.Rows.Add(row)
row = New String(){"2015",54}
DataGridView1.Rows.Add(row)
End Sub
Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
if e.ColumnIndex=2
Dim checkedRows=(From dgv as DataGridViewRow in DataGridView1.Rows where dgv.Cells(2).Value=True select dgv).ToList()
Dim controlsList As new List(of TextBox)
controlsList.Add(TextBox1)
controlsList.Add(TextBox2)
controlsList.Add(TextBox3)
for Each item in controlsList
item.Text=String.Empty
Next
for i=0 to checkedRows.Count-1
controlsList(i).Text=checkedRows.Item(i).Cells(0).Value
Next
End If
End Sub
End Class