VB.NET Print Formatted Table On Console Window - vb.net

I have data that is stored in a 2-Dimensional List that I want to print to the console window where it is all lined up properly.
Example:
Dim aList As New List Of(List Of String))
aList = AfunctionThatFetchesData
aList
{column 1} {column 2} {column 3}
This is some data 0 3
Some more 1 3
One more 2 3

Check out the documentation for Console.WriteLine, where you will see that it uses the composite formatting feature, which support alignment parameters. So, you can align things using, e.g.
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{column 1}", "{column 2}", "{column 3}")
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "This is some data", 0, 3)
which results in:
{column 1} {column 2} {column 3}
This is some data 0 3
Adjusting the spacing and alignments in the format string will get you what you want.

If you want the user to be able to manually enter data into a table:
Console.Write("Enter Data For Column 1: ")
Dim Data1 As String = Console.ReadLine
Console.Write("Enter Data For Column 2: ")
Dim Data2 As String = Console.ReadLine
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "{Data Type}", "{Column 1}", "{Column 2}")
Console.WriteLine("{0,-20} {1,-10} {2,-10}", "Data Entered:", Data1, Data2)
Console.WriteLine("ENTER To Exit: ")
Console.ReadLine()
It should look like this (Click Me).

Related

VB.Net String Replace Separated by Comma

I have a string that contains numbers
label1.text = 2,8,11,9,1,12
Now when I replace 1 it replaces 11 too and I don't want that to happen
I would like when I remove something it just remove the specific thing
but 1 will also remove 11
I'm using this for a branchaccess for multiple branch the numbers are the ids of the branch
label1.text = 2,8,11,9,1,12
Dim newstring As String
newstring = Label1.Text.Replace("," & "1", "")
Label1.Text = newstring
Can anyone please help me? Thank you
Provided that there are never spaces between your numbers, you could do something like:
label1.text = "2,8,11,9,1,12"
Dim newstring As String
newstring = label1.Text.Replace(",1,", ",")
If newstring.StartsWith("1,") Then
newstring = newstring.Substring(2)
End If
If newstring.EndsWith(",1") Then
newstring = newstring.Substring(0, newstring.Length - 2)
End If
label1.Text = newstring
This does three things:
Replace instances of ,1, with , (removing 1 and one comma from the middle of the text)
Remove 1, from the start of the string (only if it is there)
Remove ,1 from the end of the string (only if it is there)
You could also use a Regular Expression to achieve the same result, which may be cleaner or more efficient.
Perhaps a better way would be store the branch IDs separately and join them together only when you want to show them in the label.
For example,
Dim branchIds As New List(Of Integer) From {2, 8, 11, 9, 1, 12}
branchIds.Remove(1)
Label1.Text = String.Join(",", branchIds)
results in Label1 showing 2,8,11,9,12.
That way, you could change the "," to, say, ", " to improve readabilty, and it would make no difference to how the branch IDs are processed. It is a good idea to separate the data from the display of the data.

vb.net-how to create (insert elements) array but no repeated elements should be entered?

VB.NET: How to create a program to insert 10 elements in an array?
How to create another array from it but no repeated number should be inserted?
Here's an example of the concept:
Dim ary(9) As String
ary(0) = 1
ary(1) = 3
ary(2) = 5
ary(3) = 4
ary(4) = 6
ary(5) = 4
ary(6) = 3
ary(7) = 8
ary(8) = 9
ary(9) = 3
Dim newary() As String = ary.Distinct.ToArray
...but if you are not explicitly bound to using an array, a list would be much better. With an array, you'd have to limit yourself to the number of items you've instantiated the array with, or redim/resize it every time you add an element to it. A list wouldn't be limited like that and you could add new values on the fly.
Also, if all you need is distinct elements in the array, why not just check if your original array already contains a value before adding it, that way you skip having to copy the distinct values out of there..?
Or use this:
Dim lst As New List(Of String)
lst.AddRange({"1", "2", "3", "4", "5", "6", "7", "8", "9"})
Dim array As String() = lst.ToArray
It does the same as the other answer but its smaller
It doesnt get much shorter than this. You'll need to import Linq. array2 will contain 8 values since 1 and 9 are repeated.
Dim array1 As Integer() = {1, 1, 2, 3, 4, 5, 6, 7, 9, 9}
Dim array2 As Integer() = array.Distinct().ToArray()

vb.net get text between <> brackets

I am building a tool for Autodesk Inventor that works with an expression string.
=Pipe Ø<Pipe_OD> x <Pipe_t> - lg. <Pipe_length>mm
The text between <...> can be almost anything it's what the user made as input so these values are not fixed. And the number of <...> in the string can vary from 0 to 5.
What I would like to have as result for this string is following:
A converted string where the values between the <...> are replaced by number with an ascending value.
=Pipe Ø<1> x <2> - lg. <3>mm
And a string() where the values that are replaced by the numbers (above) are stored in.
I found a method that can work for strings with 1 <...> in the string but now that the quantity is variable I'm clueless about how I can do this.
Link to method
Edit: New answer with regular expressions (much better, will still have problems with additional < and > though)
Dim s As String = "abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>k"
Dim myValues As New List(Of String)
Dim matches As MatchCollection = Regex.Matches(s, "<(.|\n)*?>", RegexOptions.IgnoreCase)
Dim totalDiff As Integer = 0
Dim idx As Integer = 0
For Each ma As Match In matches
Dim realIndex = ma.Index - totalDiff
s = s.Remove(realIndex, ma.Length).Insert(realIndex, "<" & idx.ToString & ">")
idx += 1
totalDiff += ma.Length - (idx.ToString.Count + 2)
myValues.Add(ma.Value.Trim({"<"c, ">"c}))
Next
"abc <pipe_val1> 123 <pipe_val2> &*( <pipe_val3>" will be changed to "abc <0> 123 <1> &*( <2>" and myValues will contain "pipe_val1", "pipe_val2" and "pipe_val3"

How in Visual Basics change characters in a string for example (1 to 0) and (0 to 1)?

How in Visual Basics change characters in a string for example (1 to 0)
and (0 to 1) without the problem of changing them first to all to 0 and then all to 1, i want to get a result for example like that "00110010101101001010" to "11001101010010110101" (to flip it)
Use an intermediate value.
Change all 0 -> 2
Change all 1 -> 0
Change all 2 -> 1
How about this:
Dim text = "00110010101101001010"
Dim flipped = New String(text.Select(Function(c) If(c = "0"c, "1"c, "0"c)).ToArray())
That gives me:
11001101010010110101
Another option is to convert the string to a character array iterate over each character individually; then build a new string from the modified array (or overwrite the original):
Dim data As String = "00110010101101001010"
Dim arr() As Char = data.ToCharArray
For i As Integer = 0 To arr.Length - 1
arr(i) = If(arr(i) = "1", "0", "1")
Next
Dim data2 As New String(arr)
Debug.Print(data)
Debug.Print(data2)

consolidate items in datagridview visual basic.net 2008

Dim X As String
Dim V, Q, Y As Double
DGV.ColumnCount = 3
con.Open()
cmd = New SqlCommand("select Name,Price from Items where Name ='" & ListItems.SelectedItem & "'", con)
DR = cmd.ExecuteReader
While DR.Read()
' Q = Val(Qty.Text)
X = (DR("Name").ToString())
Y = Val((DR("Price").ToString()))
V = Q * Y
Dim row As String() = New String() {Q, X, V}
DGV.Rows.Add(row)
i am using visual basic.net and if i have similar items in datagridview as below
for example
1 hot dog 5 $
2 hot dog 10 $
5 hot dog 20 $
how can we consolidate them in one line as
8 hot dog 40 $
So you have columns "Name" and "Price". From your text under your code I see that name is going to be "5 hotdog" and Price to be "20$". I don't know if they are formatted that way, but I am going to assume so.
So what you want to do first is calculate the values you want in your "total" row. Since you have "hot dog" or a string after your initial number, we want to just get that number for each value. We'll loop through, evaluate that number, and sum it up. We'll do the same with the price column, but instead we'll remove the "$" in the string. Again, I'm doing a lot of assuming here.
Dim nameTotal As Integer = 0
Dim priceTotal As Integer = 0
'loop through each row of the DGV
For Each row As DataRow In DGV.Rows
'evaluate value in current row
Dim str_nameValue As String = row.Item("Name").ToString()
str_nameValue = str_nameValue.Remove(" hot dog")
'or if there are other string attached, remove those from the str_nameValue here to get an integer
'processing stringcode here...
'add to name total
nameTotal += CInt(str_nameValue)
'do the same for the other column
Dim str_priceValue As String = row.Item("Price").ToString()
str_priceValue = str_priceValue.Remove("$")
'sum
priceTotal += CInt(str_priceValue)
Next
'for loop is finished, add a new row with the values
'add row to table with a parameter of total values, and an empty one for the second column
DGV.Rows.Add(nameTotal & " hot dog " & priceTotal & "$", " ")
You should be in charge of formatting your strings to meet your needs.