How to count listbox items From Index 10 to 20? - vb.net-2010

My Question is ???
- How to count "listbox" items from index
10 to 20 ... like this =>
10 11 12 13 14 15 16 17 18 19 20
but not like this one
0 1 2 3 4 ......20
- M Using this code but not achieved ....
For Index As integer = CInt(listbox1.items.count = 10) To 20
next
As Well this one but same prome ..
For Index As integer = 10 To Listbox1.items.count 20
next
index is always running from 0 to 20 but not from 10 to 20 ....
i m stuck over here, can one one plzz tell me what m doing wrong within above given code ....
need help ...
thnxxs

It seems you are having a syntax problem, because you write a value to listbox1.items.count while you are in a read context.
The doc states:
For index As Integer = 1 To 5
In order to make your code work, try:
For index As Integer = CInt(listbox1.items.count) To 20
The count should start at anything between 0 and 20. In your case the variable should have the value 10. Make sure the variable has that value before running the For-loop.

I Solved Your Problem :)
Here Is How Your Form Should Look Like:
and here is the Code :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Index As Integer = CInt(ListBox1.Items.Item(8)) To 20
RichTextBox1.Text = RichTextBox1.Text & Index & vbLf
Next
End Sub
'You can also use this code...
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim X As Integer = 8
While X < 19
RichTextBox1.Text = RichTextBox1.Text & ListBox1.Items.Item(X) & vbLf
X += 1
End While
End Sub
End Class
I hope this code was useful to you. :)

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

Adding Multiple PictureBoxes To Array

So I have a total of 55 PictureBoxes that I am trying to add to an array. The naming of them looks like the Following:
Row1_Brick1, Row1_Brick2, up to Row1_Brick10
There is a total of 10 rows and there is 1 less brick in each row.
This is what I have thought of so far to make this work:
Dim bricks(0 To 54) As PictureBox 'Total of 55 Bricks Spread Out
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Start of Loading 55 Bricks into the bricks() Array
For i = 0 To 54
For a = 1 To 10
For am = 10 To 1 Step -1
bricks(i) = ("Row" & a & "_Brick" & am)
Next
Next
Next
End Sub
Any ideas on how to do this would be great.
I recommend a jagged array, which would look like this (note that this is 0-indexed rather than 1-indexed, as with your control names):
Dim bricks(10)() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'Set up child arrays
For i As Integer = 0 To 9
bricks(i) = New PictureBox(9-i)
'Set up each element in the array
For j As Integer = 0 To 9 - i
bricks(i)(j) = Me.Controls("Row" & i + 1 & "_Brick" & j + 1)
Next j
Next
End Sub
But if you really only want a single array, it is at least easier to set up (you might be able to get it down to a single line):
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = Me.Controls.OfType(Of PictureBox)().ToArray()
End Sub
If you need to, you can put in a Where() call to limit to pictureboxes where the name matches your patter, though it would be better to put these controls into a common Panel or GroupBox you can use as the parent rather than the form. You can also use an Orderby() call to make sure the PictureBoxes are returned in the proper sequence:
Dim bricks() As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bricks = BrickPanel.Controls.
OfType(Of PictureBox)().
OrderBy(Function(pb) pb.Name). 'Naive OrderBy... 10 is before 2. I leave it to the OP to fix that part
ToArray()
End Sub
If you are unconformtalbe with the Linq functions, the trick is to increment your result array index as part of your inner loop, rather than having a separate loop by itself:
Dim bricks(54) As PictureBox
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
For r As Integer = 1 To 10
For c As Integer = 1 to 11 - r
bricks(i) = Me.Controls("Row" & r & "_Brick" & c)
i+=1
Next c
Next r
End Sub
This is completely untested, because I was too lazy to create a new project and set up a form the same way you have, but it should be close.
Dim arr(54) As PictureBox
Dim index As Integer = 0
For row As Integer = 1 To 10
For col As Integer = 1 To 10 - row + 1 'Max column is based on the inverted value of the current row
arr(index) = Ctype(f.Controls("Row" & row & "_Brick" & col), PictureBox)
index += 1
Next
Next
This method of getting the control by the name explicitly will avoid any errors due to the order that the controls were added to the form.

Invalid input when integer is within valid means?

I'm working on a VB program, rather basic (no pun intended), in which I need to convert basic integers to Roman numerals. I have the conversion part working perfectly with my Select Case. I also need to add validation input so if an invalid number is entered, the text box displays as such. Any number between 1 and 10 should result in the ability to click the convert button. Currently, any number I enter between 1 and 10 immediately displays, "That number is invalid."
This is my current code, which fails:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub lblRomanNum_Click(sender As Object, e As EventArgs)
End Sub
Private Sub txtBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox1.TextChanged
Dim intNum As Integer
If intNum < 1 Or intNum > 10 Then
txtBox1.Text = "That number is invalid."
'ElseIf intNum > 10 Then
'txtBox1.Text = "That number is invalid"
End If
End Sub
Private Sub txtBox2_TextChanged(sender As Object, e As EventArgs) Handles txtBox2.TextChanged
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
Select CInt(txtBox1.Text)
Case 1 ' numerical 1
txtBox2.Text = "I"
Case 2 ' numerical 2
txtBox2.Text = "II"
Case 3 ' numerical 3
txtBox2.Text = "III"
Case 4 ' numerical 4
txtBox2.Text = "IV"
Case 5 ' numerical 5
txtBox2.Text = "V"
Case 6 ' numerical 6
txtBox2.Text = "VI"
Case 7 ' numerical 7
txtBox2.Text = "VII"
Case 8 ' numerical 8
txtBox2.Text = "VIII"
Case 9 ' numerical 9
txtBox2.Text = "IX"
Case 10 ' numerical 10
txtBox2.Text = "X"
'Case Else
'If a user enters an invalid value, this message is displayed and no conversion is attempted, according to instructions.
'txtBox2.Text = "That value is invalid."
End Select
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub lblRomanNum_Click_1(sender As Object, e As EventArgs)
End Sub
End Class
Any intNum less than 1 should display the invalid message.
Any intNum greater than 10 should display the invalid message.
If I'm reading what I currently have correctly, this should work and allow me to enter a number between 1 and 10 without the invalid message appearing. Am I missing something here?
Try the brackets and the 'or'
1. Dim intNum As Integer
2. If (intNum < 1) or (intNum > 10) Then
3. txtBox1.Text = "That number is invalid."
4. End If
Your code perfectly worked for me in Visual Studio 2013 with .NET Framework 4.5.1. Try deleting entire block and re-type the code.
Or you can try this code as well
If intNum < 1 OrElse intNum > 10 Then
TextBox1.Text = "That number is invalid."
End If
Needed to add...
Integer.TryParse(txtBox1.Text, i)
...directly under my variable declaration.

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

How i can retrive the data from datagridview row click to corresponding data to another form

i am trying to retrieve data from one form to another form , on gridview click event. I have small code I got it from google search but its giving me error. in this code I was trying to retrieve it to second form datagrid. please check my code. where I am wrong.
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value = True Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
I am getting this error, while I run.
Conversion from string "PO003" to type 'Boolean' is not valid.
please help me
what I have the code that totally wrong code I guess. because what I am trying to do is , I have one gridview, and some data on that, when I click the row on datagrid , I want to open the corresponding information to second form gridview. this is the right way to do that?
Try Adding...
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value.ToString
Ohhh okay i get it remove the if statement to transfer all your data into the form:
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
Next
End Sub
Or replace the if condition if you want to get all data without NULL value or Nothing
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value.ToString <> "" Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
Check if both DataGridView Has The Same Column if not the error may Occur ex. if your passing 10 datas vs 20 datas on other datagridview, the first datagridview will be insufficient to give required data needed by the second datagridview.
Try to analyze this one...
Solution1: DataGridView1 > DataGridview 2
Use this Code
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To frmGoodsReceive.dgvPODetails.RowCount - 1
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
Next
End Sub
Solution2:DataGridview1 < DataGridview2
Use this Code
Private Sub ReceiveGoods_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For j As Integer = 0 To Me.dgvReceiveGoods.RowCount -1
If frmGoodsReceive.dgvPODetails.Rows(j).Cells(0).Value.ToString <> "" Then
Dim count As Integer = 0
For i As Integer = 0 To frmGoodsReceive.dgvPODetails.ColumnCount - 1
Me.dgvReceiveGoods.Rows(count).Cells(i).Value = frmGoodsReceive.dgvPODetails.Rows(j).Cells(i).Value
Next
count += count
End If
Next
End Sub
Solution3:DataGridView1 = DataGridView 2
you can use Both...