Passing a RTB as a Paramater
I have a Function in a module called Functions
The function is:
VB Code:
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:
VB Code:
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
P.S. Are my functions ok?
Re: Passing a RTB as a Paramater
try passing the richtextbox ' ByRef ' not ByVal , eg:
VB Code:
Public Function AddChat([COLOR=Red]ByRef[/COLOR] RichTextBox As System.Windows.Forms.RichTextBox, ByVal ParamArray Text() As Object) As Object
Re: Passing a RTB as a Paramater
yeah, i already tried that. STill nothing
Re: Passing a RTB as a Paramater
The function works if you use it in the Class frmMain
But i can't seem to Reference the RTB of frmMain when calling it in a different class.
Re: Passing a RTB as a Paramater
you need to pass a reference to your main form ( the form with the Richtextbox ) to your other class when creating it , eg:
VB Code:
'/// The Class
Public Class Class1
Private frm1 As Form1
Public Sub New(ByVal frm As Form)
frm1 = DirectCast(frm, Form1)
End Sub
Public Sub test(ByVal s As String)
'/// put the string ' s ' in to Form1's Richtextbox
frm1.RichTextBox1.AppendText(s)
End Sub
End Class
to use ( As a Test in this case ) ...
VB Code:
'/// the Form that holds the richtextbox
Private cls As Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
cls = New Class1(Me) '/// Me being this form
cls.test("some text")
End Sub
Re: Passing a RTB as a Paramater