I have a Procedure in a module called Functions
The function is:
Public Sub AddChat(ByRef RichTextBox As System.Windows.Forms.RichTextBox, ByVal ParamArray Text() As Object)
With RichTextBox
.SelectionStart = 99999999
.SelectionLength = 0
.SelectionColor = Color.White
.SelectedText = "[" & TimeOfDay & "] "
.SelectionLength = 0
End With
For I As Byte = LBound(Text) To UBound(Text) Step 2
With RichTextBox
.SelectionStart = 99999999
.SelectionLength = 0
.SelectionColor = Text(I)
.SelectedText = Text(I + 1) & Microsoft.VisualBasic.Left(vbCrLf, -2 * CLng((I + 1) = UBound(Text)))
.SelectionStart = 99999999
End With
Next
End Function
When i try and use this procedure, i can't reference my Rich Text Box
This is where i call it:
Public Function Connect(ByVal server As String, ByVal port As Integer) As Boolean
Dim hostEntry As IPHostEntry = Nothing
hostEntry = Dns.Resolve(server)
Dim address As IPAddress
For Each address In hostEntry.AddressList
Dim endPoint As New IPEndPoint(address, port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
Try
tempSocket.Connect(endPoint)
Catch Err As SocketException
AddChat(rtbChat, Color.Red, "Error #: " & Err.ErrorCode & ", " & Err.Message)
End Try
If tempSocket.Connected Then
Socket = tempSocket
MsgBox("Connected")
Exit For
End If
Next address
Return True
End Function
RichTextBox is underlined in blue, with the error "Name 'rtbChat' is not declared.
Why do i get this? Any help would be GREATLY apreciated
EDIT: Are there better ways to do these functions, if so please leave opinions.
999999999?? .SelectionStart = .Text.Length
My guess is your problem is from how you referanced the richtextbox.
Public Sub RTBAdd(ByVal rtb As RichTextBox, ByVal ParamArray saElements() As Object)
Dim i As Integer
'If Client.GUI.Switches.TimestampsOn Then
'End If
For i = LBound(saElements) To UBound(saElements) Step 2
With rtb
.SelectionStart = .Text.Length
.SelectionLength = 0
.SelectionColor = saElements(i)
.SelectedText = saElements(i + 1)
.SelectionStart = .Text.Length
End With
Next i
rtb.Refresh()
End Sub
edit: or rtbChat is not the name of your rtb?
I guess your function is not within the form your rtbChat richtextbox is on, but if you want to reference through AddChat(rtbChat,....) it has to ;)
I would recommend you 2 things:
first would be to create an interface through which you can write your output to the richtextbox.
second (not that smooth, you will get exception errors if you try to write output in a pretty fast interval):
in your module above the rtbadd sub add the line
Public OutputRTB As RichtTextBox
then change your rtbadd to:
Public Sub AddChat(ByVal ParamArray Text() As Object)
With OutputRTB
.SelectionStart = .Text.Length
.SelectionLength = 0
.SelectionColor = Color.White
.SelectedText = "[" & TimeOfDay & "] "
.SelectionLength = 0
End With
For I As Byte = LBound(Text) To UBound(Text) Step 2
With OutputRTB
.SelectionStart = .Text.Length
.SelectionLength = 0
.SelectionColor = Text(I)
.SelectedText = Text(I + 1) & Microsoft.VisualBasic.Left(vbCrLf, -2 * CLng((I + 1) = UBound(Text)))
.SelectionStart = .Text.Length
End With
Next
End Function
Last thing you need to do is within the sub "Form_Load()" where your richtextbox resides. Add following code:
OutputRTB = Me.rtbChat
if you want to add alot of output you will want to choose the first one. Hope that helps
Edit: I am not at home so i cant give you my interface example here but one more thing to the second option: You could make it a little more better by defining a boolean which is set to true when you already writing to the rtb. if its true and the function is called write a loop or a thread to wait until the boolean is false again so it is safe to write again. But as i already said: this is not the way output should be written to the rtb, use a interface. soon as i am home ill try to give you a hint ;)