Short any value in VB.Net - vb.net

I'm quite a beginner in programming, and I want to make it shorter: code:
For y as integer = 1 to 5
'Code'
Next
Using sw As New StreamWriter(filepath)
sw.WriteLine("[TxtStringNumP1]")
sw.WriteLine(TxtStringNumP1.Text)
sw.WriteLine("[TxtStringNumP2]")
sw.WriteLine(TxtStringNumP2.Text)
sw.WriteLine("[TxtStringNumP3]")
sw.WriteLine(TxtStringNumP3.Text)
sw.WriteLine("[TxtStringNumP4]")
sw.WriteLine(TxtStringNumP4.Text)
sw.WriteLine("[TxtStringNumP5]")
sw.WriteLine(TxtStringNumP5.Text)
End Using
MsgBox("Ok", vbInformation)

You can make this bit shorter:
For y as integer = 1 to 5
For y = 1 to 5
And you can then rely on the fact that your control have names that differ only by a number, and there is some collection of them that knows them by name:
For y = 1 To 5
Dim n = "TxtStringNumP" & y 'formulate the name
Dim c = Me.Controls.Find(n, True) 'find all controls with a matcing name, returns a collection of controls
sw.WriteLine($"[{n}]") 'write the name we formulated
sw.WriteLine(c(0).Text) 'write the text of the first control. All controls have a Text property, we don't need to convert it to textbox
Next

Related

VBA using forms and textboxes

I am a bit confused as to how to implement code for Text boxes. I am trying to set up a form which displays a text box, for which the user has the option to enter in the values (parameters) , which then are entered into another subprogram and runs that subprogram. For example, in the following code:
Option Explicit
Sub FillSampleTable()
'This will fill an x by y grid of numbers incrementing to use for testing purposes
Dim x As Double
Dim y As Double
Dim z As Double
Dim OffsetColumn As Integer
Dim offsetrows As Integer
Dim a As Long
Application.ScreenUpdating = False
a = 10
OffsetColumn = 1
offsetrows = 1
z = 0
For x = 1 To a
For y = 1 To a
z = z + 1
Cells(x + offsetrows, y + OffsetColumn).Select
Cells(x + offsetrows, y + OffsetColumn).Value = z
Next y
Next x
Application.ScreenUpdating = True
End Sub
I would want to have a text box to prompt the user for the value of 'a', and once that value is loaded, then this subprogram is run. If no value is entered, then it would shoot an error msgbox and return to the entry screen.
When I click the box I get:
Private Sub GridSize_Change()
'I renamed text box GridSize
End Sub
but don't know what to do with this. MS Excel v 2016.
What you need is an inputbox.
You could do a simple :
a = InputBox("Enter number a")
Since you need a to be a positive integer, you should at least specify the datatype accepted by the inputbox to 1 (number). But you probably also need to make sure it is a positive integer.

VB.NET - How do I use a variable in another variables name?

How would I do something similar to this:
Dim amnt As Integer
For x As Integer = 1 To 6
If amnt[x] = 0 Then
btn[x].Enabled = False
End If
Next x
What I mean is, can I reference a variable by using another variable in the name. For example, if "x" were to be 4, then I want to change the button "btn4" to .Enabled = False, but I also want to be able to change the button that I'm changing properties of. So like
If (variablesName & x) = 0 Then
If x is 4 then it should look like this
If variablesName4 = 0 Then
If you mean evaluating a variable by supplying it's name as a string - then this cannot be done in VBA.
For example:
Dim testVar As Integer
Dim v As String
v = "Var"
testVar = 5
Debug.Print test & v '// error
Debug.Print "test" & v '// prints "testVar" as a string
Debug.Print "testVar" '// prints "testVar" as a string
Debug.Print testVar '// prints "5" <~~ only way it can be done
However, for some controls, and some other items - you can access the parent collection and supply a string value to get the correct index. So something like:
For i = 1 To 10
'// Will set checkboxes 1 to 10 to be checked
myForm.Controls("Checkbox" & i).Checked = True
Next
As it stands - it's too broad a question to give a definitive answer, but depending on the kind of objects you are working with you may be able to access a parent collection in this way.

Removing value from a List in VB

I'm making a 'Deal or No Deal' type game for a project in Visual Basic 2008.
I'm having a problem assigning the 5 values randomly to 5 boxes.
For example, in one game, the boxes could hold these values:
Box 1 = 1000
Box 2 = 35000
Box 3 = 25000
Box 4 = 75000
Box 5 = 5000
and in another game, they could hold these values
Box 1 = 75000
Box 2 = 25000
Box 3 = 1000
Box 4 = 5000
Box 5 = 35000
The main aim is to randomly assign these values to each box, and once a value has been assigned, it cannot be assigned to another box at the same time.
Here is the code that I have at the moment:
Dim values As New List(Of Integer)
Dim box(4) As Integer
Dim randNum(4) As Integer
'adding values to the Value list
values.Add(1000)
values.Add(5000)
values.Add(25000)
values.Add(35000)
values.Add(75000)
Dim i As Integer
For i = 0 To 4
Dim RandomClass As New Random()
Dim RandomNumber As Integer
RandomNumber = RandomClass.Next(0, 4)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(i)
Next
Console.WriteLine("Box 1 = " & box(0))
Console.WriteLine("Box 2 = " & box(1))
Console.WriteLine("Box 3 = " & box(2))
Console.WriteLine("Box 4 = " & box(3))
Console.WriteLine("Box 5 = " & box(4))
Console.Read()
VB keeps returning this error message when I try t run the application:
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Thanks in advance for any answers.
When you remove an item, the length of the list gets smaller. So on the second loop, index of 4 no longer exists. On the third loop, 3 no longer exists and so on. Try this instead:
Dim RandomClass As New Random()
Dim RandomNumber As Integer
For i = 0 To 4
RandomNumber = RandomClass.Next(0, values.count - 1)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(i)
Next
The answer from Steve prevents the error from occurring, but I think it's still not doing quite what you want. values.RemoveAt(i) will always remove the first item from the list the first time, the second, next time, etc... it is not removing the value that you have just put in the box. To do that you should use values.RemoveAt(RandomNumber) instead.
For i = 0 To 4
RandomNumber = RandomClass.Next(0, values.count - 1)
'assigning a box a random value form the list
box(i) = values(RandomNumber)
'removing that value from the list
values.RemoveAt(RandomNumber)
Next

Find Common Values From Datagridview

I Have about 10 (DatagridView Count may varies As per User Selected Files From 2 to 10) Datagridview ,So How can i find common value from all Datagridviews ??
Comment If you need more brief details
Below is mine but It find common from 2 -2 datagridviews
For i As Integer = 1 To dgvCont
For j As Integer = 0 To Main.DGVM(i).Rows.Count - 1
For Each Val As DataGridViewRow In Main.DGVM(i + 1).Rows
If Val.Cells(0).Value = Main.DGVM(i).Rows.Item(j).Cells(0).Value Then
Dim cm As String = Val.Cells(0).Value
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(Val.Cells(0).Value)
End If
End If
Next
Next
Next
I understand that you want to set two nested loops accounting for an undetermined number of elements (items in an array of DataGridView, I presume), performing the checks you want:
For count1 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
For row1 As Integer = 0 To Main.DGVM(count1).Rows.Count - 1
If (Main.DGVM(count1).Rows(row1).Cells(0).Value Is Nothing) Then Continue For
Dim val1 As String = Main.DGVM(count1).Rows(row1).Cells(0).Value
Dim found As Boolean = False
For count2 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
If (count2 = count1) Then Continue For
For row2 As Integer = 0 To Main.DGVM(count2).Rows.Count - 1
If (Main.DGVM(count2).Rows(row2).Cells(0).Value Is Nothing) Then Continue For
Dim val2 As String = Main.DGVM(count2).Rows(row2).Cells(0).Value.ToString()
If val1 = val2 Then
Dim cm As String = val1
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(val1)
End If
found = True
Exit For 'By assuming that you want to stop searching after finding a match
End If
Next
If (found) Then Exit For 'By assuming that you want to stop searching after finding a match
Next
Next
Next
Your code is not too clear (neither what you want); but this should give you a good enough start to carry out the implementation you are looking for. Bear in mind that this code (like yours) only considers one column (first one); in case of wanting to iterate through all the columns, you would have to add further nested loops accounting for that.

How to Find out if an items is not in an array

Ok i just want to know if value x is not in my array
Heres what i have been trying
Im using VB.net
and just need to know when x isnt in the array so i can take an action. thankx
Dim L, Path(0) As Integer
Open = cleara(Open)
sealed = cleara(sealed)
Open(0) = Agent
sealed(0) = Agent
Finds adjacent nodes
L = Agent
Do Until sealed(sealed.GetLength(0) - 1) = Targ Or Open.GetLength(0) = 0
'Agents(0) = L
H = Find_H(L, Targ, Open)
'T = Find_T(L, Targ, Open)
ReDim F(T.GetLength(0) - 1)
For lp As Integer = 0 To F.GetLength(0) - 1
F(lp) = H(lp) '+ H(lp)
Next
L = Find_lowest(F, Open)
Open = Remove_from(Open, L)
sealed = Add_to(sealed, L)
Ad = Find_adjacent(L, Targ)
For lp As Integer = 0 To Ad.GetLength(0) - 1
Ok here is where my problems is
What i need to do is
find out if ad is in seal if yes ignore it
If ad isnt in sealed the if it is in open compare T values
if ad isn in sealed or open then add it to open and set L as parent of ad
The below was a way to test and see if the values were loading into the arrays right
If Walk(Ad(lp)) <> -1 Then
Parents(Ad(lp)) = L
Open = Add_to(Open, Ad(lp))
For lp2 As Integer = 0 To sealed.GetLength(0) - 1
For lp3 As Integer = 0 To Open.GetLength(0) - 1
If lp3 < Open.GetLength(0) - 1 Then
If Open(lp3) = sealed(lp2) Then
Open = Remove_from(Open, sealed(lp2))
End If
End If
Next
Next
End If
Next
G.Graphics.DrawRectangle(Pens.White, Grid(Targ))
TempDrawing()
Loop
This is suppost to be an a* program but i have been having trouble main with my heuistics if you can tell me what im doing wrong it would also be a great help
Here is how this work so far
Add my location to open
Create H,G,F for items in open list
Find lowest F
Find adjacent nodes
Loop through nodes
If node is not walkable then ignore
If in sealed ignore (this is where im stuck at)
If not in sealed and is walkable then if in open compare G scores else add to open
try this
If Array.IndexOf(yourArray, x) == -1 Then
'Do something
End If
This need to be for -
For lp as integer = 0 to array.getlength(0) - 1
^ error. Needs to array.GetLength()
Also, if a number is found, you can print it and break from the loop. There is no need to iterate further in the loop.
You need to be careful, the variable that is used as a flag( i.e., g) is different from the variable actually setting a value to it. ( i.e., G )
You forgot to assign initial value to g outside of the for loop.
Try this.
Dim g as int
g = 0
For lp as integer = 0 to array.Length() - 1
If X = array(LP) Then
g = 1
Exit For
End If
next
If g = 0 Then
X is not in array
End If
Use the Enumerable.Except method to get all points in one enumerable collection that are also present in a second collection.
Here is an example taken from the MSDN documentation of the method:
' Create two arrays of doubles.
Dim numbers1() As Double = {2.0, 2.1, 2.2, 2.3, 2.4, 2.5}
Dim numbers2() As Double = {2.2}
' Select the elements from the first array that are not
' in the second array.
Dim onlyInFirstSet As IEnumerable(Of Double) = numbers1.Except(numbers2)
Dim output As New System.Text.StringBuilder
For Each number As Double In onlyInFirstSet
output.AppendLine(number)
Next
' Display the output.
MsgBox(output.ToString())
' This code produces the following output:
'
' 2
' 2.1
' 2.3
' 2.4
' 2.5