vb.net position cursor one space greater than text box length - vb.net

I have a TextBox and it contains this text "File Was Created"
I would like to place the cursor one space over from the end of this text in the TextBox
I am trying to NOT say Simple Enough Task BUT I have wasted 2 hours with no solution
YES I know if I change the text to this "File Was Created " it will work NOT a solution
Here is the code mess I have tried
Dim L As Integer
L = tbMessage.Text.Length
L += 1
'tbMessage.Text = CStr(L)
'tbHaveTwo.Text = frmOne.vR
'Me.ActiveControl = tbMessage
'tbMessage.SelectionStart = tbMessage.Text.Length
tbMessage.SelectionStart = L
tbMessage.Select()<br/>
Here is Two updated ways to solve this issue Jimi way less code
tbMessage.Text = "File Was Created"
'This Code involves more code
'Dim str As String
'str = Mid(tbMessage.Text, tbMessage.Text.Length)
'If str <> " " Then
' tbMessage.Text = tbMessage.Text & " "
'End If
'Answer from Jimi Works Great
tbMessage.AppendText(ChrW(32))
tbMessage.SelectionStart = tbMessage.Text.Length
tbMessage.Select()

So you don't end up with a ton of spaces on the end of your message?
tbMessage.AppendText(If(tbMessage.Text.EndsWith(" "), "", " "))
tbMessage.SelectionStart = tbMessage.TextLength
tbMessage.Focus()

Related

Conditional Formatting in word vba

Greeting to all Members and Experts, I am trying to automate
the formatting process in word. The formatting is done by applying styles. But before applying styles I need to trim extra spaces between characters of serial numbers, for example, 1. a. i. and insert tabs after dot(.) and then apply the style. I have attached a sample document. Plz have a look. I have tried to get the desired result by using the following code but it doesn't get the work done
I am new here so i dont know how to attach sample files so, here is the link for sample file. https://docs.google.com/document/d/1Z1dB6tvPKVrxHlw7qV8VNyiy49c5lRZN/edit?usp=sharing&ouid=101706223056224820285&rtpof=true&sd=true
Any help or suggestion would be of great help. Thanks in advance...
Sub formatts()
Dim a As Integer
Dim i As Integer, n As Long, para As Paragraph, rng As Range, doc As Document
Set doc = ActiveDocument
With doc
For i = 1 To .Range.Paragraphs.Count
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = " " Or .Paragraphs(i).Range.Characters(n).Text = Chr(9) Or .Paragraphs(i).Range.Characters(n).Text = Chr(160) Then
.Paragraphs(i).Range.Characters(n).Select
'This line checks whether the first character is whitespace character or not and delete it.
doc.Paragraphs(i).Range.Characters(n).Delete
ElseIf .Paragraphs(i).Range.Characters(n).Text = "." Then
.Paragraphs(i).Range.Characters(n).InsertAfter (vbTab)
n = n + 1
a = a + 1
ElseIf .Paragraphs(i).Range.Characters(n).Text Like "[a-z]." And .Paragraphs(i).Range.Characters(n).Next.Next.Text <> "i" Then
Exit For
End If
If a >= 3 Then Exit For
Next
For n = 1 To doc.Paragraphs(i).Range.Characters.Count
If .Paragraphs(i).Range.Characters(n).Text = "i" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "shh"
Exit For:
ElseIf .Paragraphs(i).Range.Characters(n).Text = "a" Or .Paragraphs(i).Range.Characters(n).Text = "b" Or .Paragraphs(i).Range.Characters(n).Text = "c" And .Paragraphs(i).Range.Characters(n).Next.Text = "." And .Paragraphs(i).Range.Characters(n).Next.Next.Text = " " Then
doc.Range.Paragraphs(i).Style = "sh"
Exit For
End If
Next
Next
End With
End Sub

how to check if dataset contains specific value in VB.net

I have a dataset that contains multiple values. I want to take those rows from that dataset that contains "the specific value" and firstly I want to display those in a MessageBox.
Furtheron, I try to view them in a datagridview called ErrorsDgV.
I already searched this topic and found a good function, but unfortunately, all I get from the MessageBox is an empty box.
ErrorsDgV.DataSource = Srchdataset.Tables("blubb")
LineLabel.Text = "Lines: " &
Srchdataset.Tables("blubb").Rows.Count.ToString
ErrorsDgV.Sort(ErrorsDgV.Columns(1), System.ComponentModel.ListSortDirection.Ascending)
ErrorsDgV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
ErrorsDgV.Columns(1).DefaultCellStyle.Format = "dd/MM/yyyy HH:mm:ss.fff"
Dim answer As String = ""
Dim SearchRows() As Data.DataRow
SearchRows = Srchdataset.Tables("blubb").Select("Data = 'the specific value'")
answer = ""
For k As Integer = 0 To SearchRows.Length - 1
If answer = "" Then
answer = SearchRows(k).Item("Data")
Else
answer = answer & vbNewLine & SearchRows(k).Item("Data")
End If
Next
MsgBox(" " & answer)
I debugged also and got to know that SearchRows is empty, even if the specific value is inlcuded in that DataSet.

My For Loop skips my IF statement

I wonder if anyone can help. I am making a program that will convert Text to ASCII. However, I want my program to ignore spaces. Hence, "IT WAS A" should look like this: 7384 876583 65
When I use the Step Into Feature of VB I can see that my For loop is skipping my IF statement which should be giving me my spaces. I don't understand why. As you can probably tell, I am a beginner so any specific help would be greatly appreciated. My code looks like this:
Dim PlainText, ConvertedLetter As String
Dim LetterToConvert As Char
Dim AscNumber, Counter As Integer
ConvertedLetter = ""
PlainText = txtPlain.Text
For Counter = 1 To Len(PlainText)
LetterToConvert = Mid(PlainText, Counter, 1)
If PlainText = " " Then
ConvertedLetter = " "
Else : AscNumber = Asc(LetterToConvert)
ConvertedLetter = ConvertedLetter & AscNumber
End If
Next
txtAscii.Text = ConvertedLetter
Because you're comparing PlainText, which is the whole string, to " ". It would need to be:
If LetterToConvert = " " Then ....
Try this:
Dim PlainText, ConvertedLetter As String
ConvertedLetter = ""
PlainText = "IT WAS A"
For Each c As Char In PlainText 'iterate through each character in the input
If c <> " " Then ' check whether c is space or not
ConvertedLetter &= Asc(c).ToString()' ascii value is taken if c<>" "
Else
ConvertedLetter &= " " ' c is space means add a space
End If
Next
MsgBox(ConvertedLetter) ' display the result
You will get the output as
7384 876583 65

simple messagebox not getting displayed

i am trying to display a message box if the string is empty and this is the code i have tried.i am not getting any errors but still message box is not getting displayed.Any help is appreciated.
Dim tmp1 As String, tmp2 As String, tmp3 As String
Dim TextBox1 As String
Dim TextBox2 As String
Dim TextBox3 As String
tmp1 = Sheets("Sheet1").TextBox1.Value
If tmp1 = " " Then
MessageBox.Show ("file1 not selected")
End If
tmp2 = Sheets("Sheet1").TextBox2.Value
If tmp2 = " " Then
MessageBox.Show ("file2 not selected")
End If
tmp3 = Sheets("Sheet1").TextBox3.Value
If tmp3 = " " Then
MessageBox.Show ("file3 not selected")
Two things
A. If tmp2 = " " Then
If you are checking for blanks then use this
If Len(Trim(tmp2)) = 0 Then
B. MessageBox.Show is VB.Net. For VBA use
MsgBox "file1 not selected"
Similarly for others...
Based on your question, I recommend you try the following steps:
Name three cells in sheet 1 as TextBox1, 2, and 3 - use name manager in Excel (no VBA needed)
Type the below code in VBE:
code:
If Range("TextBox1").value = "" Then
Msgbox "pls fill field 1"
End if
I hope it makes sense and solves your problem.

Tool to reformat VB.Net code - specifically line breaks

Are there any tools available for automatically formatting vb.net code - specifically for adding line breaks at a predefined line length? I'm working with a lot of code with long lines (thousands of lines), and manually reformatting it is quite time consuming. I've seen a number of tools for rearranging code into regions etc., but haven't found any that reformat with line breaks. Free would be great.
Try having VS auto-wrap your lines. The option should be in the Tools | Options | Basic | Settings | Word Wrap.
Another thing to do is go to the Edit | Advanced | Format Document menu option, which helps clear the air with not well-formed documents.
A 3rd option is to install DevExpress' Code Rush Xpress add-on, which add's very handy vertical lines for when code blocks begin and end, and also helps in refactoring code. You can get it from here: http://devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/. It's free, but doesn't support the Express editions of Visual Studio.
Use Visual Studio 2008 you have to use Ctrl + A + K + F for formatting your c#, vb code
I know this was posted a long time ago. But if ever someone had the same problem, try this sub I made. The sub will have two outputs (Textbox1 = Code with breaks, Textbox3 = a one liner code).
Create two textboxes (named Textbox1 and Textbox3) and a button (Button1)
Create a sub (name what you want) and enter this code:
Try
Dim x As String = TextBox1.Text
x = x.Replace("& """, "")
x = x.Replace(""" _", "")
x = x.Replace("""", "")
x = x.Replace(vbNewLine, "")
x = x.Replace(vbTab, "")
While x.Contains(" ") '2 spaces.
x = x.Replace(" ", " ") 'Replace with 1 space.
End While
TextBox3.Text = x
Dim l As Integer = Len(x)
Dim xlim As Integer = InputBox("Specify the maximum number of characters for each line:", "Line Max", 66)
Dim ylim As Double = 0
TextBox1.Text = ""
ylim = l / xlim
If Int(ylim) <> ylim Then
ylim = Int(ylim) + 1
Else
ylim = Int(ylim)
End If
Dim una As String = "", huli As String = ""
Dim mynewstring As String = ""
Dim startin As Integer = 1
For i = 1 To ylim
If i = 1 Then
una = """"
Else
una = vbTab & "& """
End If
If i = ylim Then
huli = """"
Else
huli = """ _"
End If
mynewstring = mynewstring & una & Strings.Mid(x, startin, xlim) & huli & vbNewLine
startin += xlim
Next
TextBox1.Text = mynewstring
Catch ex As Exception
MsgBox(ex.Message)
End Try
P.S. I did not add the code to restore your original input.