Can anyone correct me on what is wrong with this code?
I can't add a new line inside Richtextbox control
Dim sb = New System.Text.StringBuilder()
sb.Append("{\rtf1\ansi")
sb.Append("\b " & TextBox2.Text & "\b0" & Environment.NewLine & "my body")
sb.Append("}")
Textbox.Rtf = sb.ToString
Because you aren't using rtf codes. Try:
sb.Append("\b " & TextBox2.Text & "\b0\par my body")
Related
textbox1.text = "OS = " combobox1.selecteditem + environment.newline +
combobox2.selecteditem + environment.newline + combobox3.selecteditem +
environment.newline
Working with Visual Basic 2010
I'm trying to make a display show up as
"OS =" and lets say I chose "combobox3" I want it to look like
"OS = Windows"
instead of
"OS =
Windows"
my code done so fare is
String concatenation should be done with & instead of +. Also, remove environment.newline to get rid of the line breaks.
textbox1.Text = "OS = " & combobox1.SelectedItem & " " &
combobox2.SelectedItem & " " & combobox3.SelectedItem
If I'm misunderstanding the question and you're trying to avoid line breaks where there is no value you would do the following:
Dim strResult As String = "OS = " & Environment.NewLine
ComboBox1.SelectedIndex = 1
If ComboBox1.SelectedItem IsNot Nothing Then
strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
End If
If ComboBox2.SelectedItem IsNot Nothing Then
strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
End If
If ComboBox3.SelectedItem IsNot Nothing Then
strResult += ComboBox1.SelectedItem.ToString() & Environment.NewLine
End If
TextBox1.Text = strResult
I would use Linq personally. You don't have to worry about many If statements and checking if something is selected or not and also concatenating a string either or adding or removing CheckBox's if you plan to do so; it will grab all of them. If you plan to not include some then you would need to modify the query to fit. This is tried and tested as well.
Dim q As List(Of String) = (From s As ComboBox In Me.Controls.OfType(Of ComboBox).Reverse() Where s.SelectedItem IsNot Nothing Select CStr(s.SelectedItem) & Environment.NewLine).ToList
MsgBox("OS = " & String.Join("", q.ToArray)) 'Just for testing!
I have a VB.NET application that has instant messaging-like functionality with a database. It can retrieve the values just fine, but the problem is the formatting isnt coming out right. I want the format to be as follows:
Sender: Message
(so...)
David: Hey guys
What I've tried below doesn't get me the result I'm looking for, it just prints the sender at the top of the rich textbox in my application and the message at the bottom, does anyone have any ideas?
'-------------------Retreives the message-------------------------------------------------------------
Dim sqlStr As String = "SELECT * FROM dojodb.chats"
Dim chatcommand As New MySqlCommand(sqlStr, MysqlConn)
Dim rdr As MySqlDataReader = chatcommand.ExecuteReader()
Dim tbl As New DataTable
tbl.Load(rdr)
'-------For every row, print the message, skip a line, and add 1 so it goes to next msg-- ------
For i As Integer = 0 To tbl.Rows.Count - 1
rowIndex = i
strSender &= CStr(tbl.Rows(rowIndex)("Sender")) & vbNewLine
strMessage &= CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine
strOutPut = strSender + ": " + strMessage
Next
txtGroupChat.Text = strOutPut
'Keeps the richtextbox scrolled to the bottom so that most recent msg is always shown
txtGroupChat.SelectionStart = txtGroupChat.Text.Length
txtGroupChat.ScrollToCaret()
strOutPut = "" 'clearing the string so that it does not print out duplicate info next time
strSender = ""
strMessage = ""
'-------------------------End Retrive---------------------------------------
I feel a bit embarrassed posting this, but...
strSender = CStr(tbl.Rows(rowIndex)("Sender")) & ": "
strMessage = CStr(tbl.Rows(rowIndex)("Message")) & vbNewLine
strOutPut &= strSender & strMessage
What do you think vbNewLine does? Also, be careful of &=
I have an excel on a shared drive and my application is using an Oledb connection to read data from the excel into a DataGridView.
cn = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "data source=" + skuPath + ";Extended Properties=""Excel 12.0;HDR=YES;""")
q1 = "select * from [" + year + "$B4:AM300]"
da = New System.Data.OleDb.OleDbDataAdapter(q1, cn)
Try
cn.Open()
da.Fill(ds, "Table1")
Catch e As OleDb.OleDbException
Dim errorMsg As String
Dim i As Integer
errorMsg = ""
For i = 0 To e.Errors.Count - 1
errorMsg += "Index #" & i.ToString() & ControlChars.Cr _
& "Message: " & e.Errors(i).Message & ControlChars.Cr _
& "NativeError: " & e.Errors(i).NativeError & ControlChars.Cr _
& "Source: " & e.Errors(i).Source & ControlChars.Cr _
& "SQLState: " & e.Errors(i).SQLState & ControlChars.Cr
Next i
End Try
cn.Close()
dt = ds.Tables(0)
When the excel file is already open by another user you get this notification in excel:
And in those situations my code returns this error on the last line:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
Additional information: Cannot find table 0.
So i understand is that because the file is in use then the entire connection returns nothing and data table is therefore empty.
I found a few way of determining if a file is in use or not but nothing regarding how to read from a file in use.
Is it possible? and if so how?
Please remember i only need to read the file and if its possible to always open it as a readonly that would awesome!
You can't. You have to close the open file before read it even if you use the ODBC(read only).
Ref: http://forums.asp.net/t/1083489.aspx?open+a+Microsoft+Jet+OLEDB+4+0+connection+to+excel+file+read+only
In my application I have created I need it to write a visual basic script to change the ip address
The script is as follows...
Dim strIPAddress
Dim strSubnetMask
Dim strGateway
Dim intGatewayMetric
Dim strDns1
Dim strDns2
strIPAddress = "192.168.1.211"
strSubnetMask = "255.255.255.0"
strGateway = "192.168.0.11"
intGatewayMetric = 1
strDns1 = "8.8.8.8"
strDns2 = "4.4.4.4"
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit
The way I am trying to write this is like the following..
Directory.CreateDirectory("C:\SpecMee\IPChanger\")
Dim objwriter As New System.IO.StreamWriter("C:\SpecMee\IPChanger\IpChanger.vbs")
objwriter.WriteLine("Dim strIPAddress" & Environment.NewLine & "Dim strSubnetMask" & Environment.NewLine & "Dim strGateWay" & Environment.NewLine & "Dim intGatewayMetric" & Environment.NewLine & "Dim strDns1" & Environment.NewLine & "Dim strDns2" & Environment.NewLine & "strIPAddress = """ & Environment.NewLine & TB_IPAddress & Environment.NewLine &)
objwriter.Close()
MessageBox.Show("Created")
The problem I am having is one, it is taking ages and two, how would i include the "" in the script.
I dont mind spending time on this, I just dont know if im going about this the right way.
any help would be appreciated.
Thanks
Chris
Add the script as a text file resource to your project. Replace all instances of the IP Address in the script with some known value, then use string.replace:
Const ipPlaceHolder As String = "##IP##"
Dim scriptContent As String = My.Resources.script.Replace(ipPlaceHolder, myTextBox.Text)
IO.File.WriteAllText("C:\SpecMee\IPChanger\IpChanger.vbs", scriptContent)
Where myTextBox contains user input and the text file resource is named "Script"
Two quotation marks in a row makes one in the output.
"And he said ""Thar she blows!"""
( You have two questions in one. That is not considered good customs on Stackoverflow. )
okay I am totally stuck.
I have been getting some help off and on throughout this project and am anxious to get this problem solved so I can continue on with the rest of this project.
I have a gridview that is set to save to a file, and has the option to import into excel.
I keep getting an error of this:
Invalid cast exception was unhandled.
At least one element in the source array could not be cast down to the destination array type.
Can anyone tell me in layman easy to understand what this error is speaking of?
This is the code I am trying to use:
Dim fileName As String = ""
Dim dlgSave As New SaveFileDialog
dlgSave.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
dlgSave.AddExtension = True
dlgSave.DefaultExt = "txt"
If dlgSave.ShowDialog = Windows.Forms.DialogResult.OK Then
fileName = dlgSave.FileName
SaveToFile(fileName)
End If
End Sub
Private Sub SaveToFile(ByVal fileName As String)
If DataGridView1.RowCount > 0 AndAlso DataGridView1.Rows(0).Cells(0) IsNot Nothing Then
Dim stream As New System.IO.FileStream(fileName, IO.FileMode.Append, IO.FileAccess.Write)
Dim sw As New System.IO.StreamWriter(stream)
For Each row As DataGridViewRow In DataGridView1.Rows
Dim arrLine(9) As String
Dim line As String
**row.Cells.CopyTo(arrLine, 0)**
line = arrLine(0)
line &= ";" & arrLine(1)
line &= ";" & arrLine(2)
line &= ";" & arrLine(3)
line &= ";" & arrLine(4)
line &= ";" & arrLine(5)
line &= ";" & arrLine(6)
line &= ";" & arrLine(7)
line &= ";" & arrLine(8)
sw.WriteLine(line)
Next
sw.Flush()
sw.Close()
End If
I bolded the line where it shows in debug, and I really dont see what all the fuss is about LOL
If we assume you only want the value of the cell, then your method is incorrect, as it will try to copy the entire cell to the array.
Would this work for you?
//**row.Cells.CopyTo(arrLine, 0)**
line = row.Cells[0].Value.ToString()
line &= ";" & row.Cells[1].Value.ToString()
line &= ";" & row.Cells[2].Value.ToString()
line &= ";" & row.Cells[3].Value.ToString()
line &= ";" & row.Cells[4].Value.ToString()
line &= ";" & row.Cells[5].Value.ToString()
line &= ";" & row.Cells[6].Value.ToString()
line &= ";" & row.Cells[7].Value.ToString()
line &= ";" & row.Cells[8].Value.ToString()
sw.WriteLine(line)
I generally try to avoid VisualBasic (the syntax always seems opposite to me), but from a little bit of Googling at the VB docs, I'd hazard a guess that you have a type mismatch between your string array (arrLine) and what row.Cells is trying to copy into it.