vb.net I can't answer a simple loop - vb.net

I had a project to do with looping a text into vertical and horizontal
suppose to look like this
http://pastebin.com/UJ6LZybU
Please help me :(
well this are the code i tried
but it only showed the end of the words
Dim input As String = "HELLOWORLD"
Dim i As Integer = 0
For i = 0 To input.Length - 1
Dim words As Char = input(i)
TextBox1.Text = ("HELLOWORLD" & vbNewLine & words)
Next
this project can be answered in any type of loop
doloop
forloop

Set TextBox1.Multiline = True and resize
Dim input As String = "HELLOWORLD"
TextBox1.Text = ("HELLOWORLD" & Environment.NewLine)
For i As Integer = 1 To input.Length - 1
TextBox1.Text &= (input(i) & Environment.NewLine)
Next

This should do it:
Dim i As Integer
Dim input As String = "HELLOWORLD"
TextBox1.Text = input
For i = 1 To input.Length - 1
TextBox1.Text &= vbNewLine & input(i)
Next

Related

Problems with Sub in vba after repeating it

Hello i have problem with sub
Sub lab1_5()
Dim displayText As String
Do
number = InputBox("Write number: ")
For i = 1 To 9
number = number * 2
displayText = displayText & " " & number
Next i
MsgBox displayText
Dim answer As VbMsgBoxResult
answer = MsgBox("KONIEC" & vbNewLine & "Chcesz spróbować jeszcze raz?", vbYesNo)
Loop While (answer = vbYes)
End Sub
If i write for example 2 it shows 2 4 8 16 etc and than after clicking yes and writing another number i have previous(2 4 8 etc) and new numbers. How can i repair it so i have only new numbers?
You need to reset displayText to an empty string at the start of the do loop.
Sub lab1_5()
Dim displayText As String
Do
Number = InputBox("Write number: ")
displayText = "" '<--- this line here
For i = 1 To 9
Number = Number * 2
displayText = displayText & " " & Number
Next i
MsgBox displayText
Dim answer As VbMsgBoxResult
answer = MsgBox("KONIEC" & vbNewLine & "Chcesz spróbowac jeszcze raz?", vbYesNo)
Loop While (answer = vbYes)
End Sub

Displays the result many times in Mulitselect Listbox Search in VB6

I am creating a program in vb6 with ms access. while i am searching the database from multi select list box in vb it displays the results wrongly.
if i click the first item it shows one time
if i click second item it shows that item two times
it i click third item it shows that item three times.
how to solve this
i tried the below code
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) = True Then
If str <> "" Then str = str & ""
If Val(List1.SelCount) = 1 Then
str = List1.List(List1.ListIndex)
Else
str = str & " or name= " & List1.List(List1.ListIndex)
End If
End If
Next i
If str <> "" Then
Set rs = db.OpenRecordset("select * from Customers where name= '" & str & "'")
display
End If
result
Kumar vasanth vasanth kannan kannan kannan
Try this:
Option Explicit
Private Sub Command1_Click()
Dim i As Integer
Dim str As String
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then str = str & " or name = '" & List1.List(i) & "'"
Next i
str = Mid(str, 4)
If str <> "" Then
Set rs = db.OpenRecordset("select * from Customers where " & str)
display
End If
End Sub

How to check whether a string value is present in a DataGridView

Im working on a code that has a TextBox, A Button and a Datagrid view.
I want to Display "Data Not Exist" when a value in TextBox is not present on the DataGridView when i press the button.
This is my code so far
If DataGridView1.Rows.Contains(TextBox1.Text) = False Then
MessageBox.Show("Data Not Exist!")
End If
You need to loop through all rows and columns
Dim isFound As Boolean = False
For Each row As GridViewRow In DataGridView1.Rows
for i As Integer = 0 to DataGridView1.Columns.Count -1
If row.Cells[i].Text = TextBox1.text Then
isFound = True
exit for
End If
Next
Next
If (isFound) Then
MessageBox.Show("Data Exists!")
Else
MessageBox.Show("Data Not Exists!")
EndIf
You can do it easily with either using a LINQ or a ForLoop
This code will search all matches it will find across the DataGridView and will prompt in which Row and Column it sees the match.
With a ForLoop, you need to run a loop for Column and for the Row.
Private Sub SearchUsingForLoop()
Dim resultString As String = Nothing
For x = 0 To DataGridView1.ColumnCount - 1
For y = 0 To DataGridView1.RowCount - 1
If DataGridView1.Item(x, y).Value.ToString.ToUpper = txtSearch.Text.ToUpper Then
resultString &= " - Column " & x + 1 & " Row " & y + 1 & vbCrLf
End If
Next
Next
If resultString <> Nothing Then
resultString = txtSearch.Text & " found in : " & vbCrLf & resultString
Else
resultString = "Data does not exist."
End If
MsgBox(resultString)
End Sub
Do remember that index of DatagridViewRow and DatagridViewColumn starts with 0.
Another way of doing this is by LINQ:
Private Sub SearchUsingLINQ()
Dim resultSet = From dgRow As DataGridViewRow In Me.DataGridView1.Rows, _
dgCell As DataGridViewCell In dgRow.Cells _
Where dgCell.Value.ToString.ToUpper = txtSearch.Text.ToUpper _
Select dgCell
Dim resultString As String = Nothing
If resultSet.Count > 0 Then
resultString = txtSearch.Text & " found in :" & vbCrLf
For Each dgCells In resultSet
resultString &= " - Column " & dgCells.ColumnIndex + 1 & " Row " & dgCells.RowIndex + 1 & vbCrLf
Next
End If
If resultString <> Nothing Then
MsgBox(resultString)
Else
MsgBox("Data does not exist.")
End If
End Sub
Feel free to use any of those. But I suggest you to study iterating a DataGridView first.

Check if string starts or ends with vbCrLf

How can I check if a string starts (or ends) with vbCrLf?
I've tried with substring but it doesn't seem to work:
Dim s As String = ""
s &= vbCrLf & "Test"
If s.Substring(0, 1) = vbCrLf Then
MsgBox("Yes")
End If
Try this
StartsWith - checks the first part of a String.
Dim s As String = "vbCrLf bla bla bla"
If s.StartsWith("vbCrLf") Then
MsgBox("Yes")
End If
EndsWith - checks the last characters of a String.
Dim s As String = "bla bla bla vbCrLf"
If s.EndsWith("vbCrLf") Then
MsgBox("Yes")
End If
Left (and Right) can be used in place of .StartsWith/.EndsWith if you're having issues with these.
Dim s As String
s = vbCrLf & "Test"
If Left(s, Len(vbCrLf)) = vbCrLf Then
MsgBox("Yes")
End If
The simplest solution I could come up with for determining whether a string contains another string anywhere is to use the split function:
Dim s As String
Dim i As Integer
Dim v As Variant
s = vbCrLf & "Test"
i = Split(s, vbCrLf)
For Each item In i
j = j + 1
Next item
If j > 1 or vbCrLf = "" Then
MsgBox("Yes")
End If
I believe you are asking if there is a way to see if the string begins with a line brake. [Line Feed Return] (vbLf) or [Carriage Return] (vbCr). I use the Chr value to do that. Chr(13) is vbCr. Chr(10) is vbLf. You can use Asc to find out what the first Chr of a string is. Something like this;
Dim s As String = vbCr & "bla bla bla"
If Asc(s) = 13 Then
MsgBox("s strats with a Carriage Return")
else If Asc(s) = 10 Then
MsgBox("s strats with a Line Feed Return")
End If

How to insert text in between texts in Textbox VB6/VBA

I wanted to insert some texts(new line) in between existing texts in a textbox (multiline = true).
Example: (Textbox1.text's value is written below)
Name: Name of Client
DOB: 11/11/11
>>>THIS IS WHERE I WHAT TO INSERT THE VALUE OF TEXTBOX2.TEXT
Hospitalization: No
Serial Number: 12345678
Private Sub cmdTransfer_Click()
Dim SearchNote As Integer, SearchThis As String, tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbCrLf
End If
SearchThis = "Hospitalization"
SearchNote = InStr(Textbox1.Text, SearchThis)
If SearchNote Then
With textbox1
.SetFocus
.SelStart = SearchNote
.Text = .Text & .SelStart & tx2
End with
End If
End Sub
What I'm doing in my code is I'm getting the number of characters before the "Hospitalization" so that I can insert the value of Textbox2 before it. I dont know how to do that tho. Please help.
Thanks!
I believe the code you are looking for is this:
Left(SearchNote, InStr(1, SearchNote, "Hospitalization") - 1) & "new text to insert" & Mid(SearchNote, InStr(1, SearchNote, "Hospitalization"))
Left will take the first few letters up to the starting point of "Hospitalization". Then you insert the new string (possible with a new line before and after with & chr(10) &). Then you add with Mid everything after "Hospitalization".
Since I don't have a sample copy of your spreadsheet, there is a chance that one/some of my variables might be different. If you find problems with any of these, check all of the vars.
Solution #1: Create module and add this function:
Function addText(txtBox As String, addString As String)
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = addString & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
' Return with added text
addText = CStr(input1 & input2 & input3)
End If
End Function
then call in your button to update your text box:
Private Sub cmdTransfer_Click()
Dim tx2 As String
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if there is nothing to add
End
End If
If textbox1.Value <> vbNullString Then
textbox1.Value = addText(textbox1.Value, tx2)
End If
End Sub
Solution #2: Call everything from within your button:
Private Sub cmdTransfer_Click()
Dim endIndex As Long
Dim SearchThis As String
Dim input1, input2, input3 As String
Dim txtBox As String, tx2 As String
'set tx2
If cb9.Value = True Then
tx2 = "ADDRESS: " & vbTab & text2.Text & vbNewLine
Else
' Stop if nothing to add
End
End If
If textbox1.Value <> vbNullString Then
' set txtBox variable
txtBox = textbox1.Value
Else
' Avoid Error if text box is null
End
End If
SearchThis = "Hospitalization"
' Get index of Hospitalization
endIndex = InStr(1, txtBox, SearchThis) - 1
If endIndex > 0 Then
input1 = Mid(txtBox, 1, endIndex)
input2 = tx2 & vbNewLine
input3 = Mid(txtBox, endIndex, Len(txtBox))
textbox1.Value = input1 & input2 & input3
End If
End Sub
What i would do is split text1 into an array then just add the text in the middle, mainString is text1, midStr is text2:
Dim mainStr as String, midStr as String, ArreStr() as String
mainStr=text1.text:midStr=text2.text
ArreStr=Split(mainStr,VBNewLine)
text1.text=ArreStr(0) & vbnewline & midStr & vbnewline & ArreStr(1)