0

我有一个Form(ClientGUI),它有一个RichTextBox. 我想要做的是将文本从位于另一个类(MyQuickFixApp)中的 Sub 附加到这个 RichTextBox。我知道 Sub 可以工作,因为调试器会通过,但它不会将文本附加到我的RichTextBox.

我怎么做 ?

谢谢你的帮助!


客户端GUI.vb:

Imports QuickFix
Imports QuickFix.Transport
Imports QuickFix.Fields

Public Class ClientGUI
    Dim initiator As SocketInitiator

    Public Sub ClientGUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim filename As String = "Resources/initiator.cfg"
        Dim settings As New SessionSettings(filename)
        Dim myApp As New MyQuickFixApp()

        Dim storeFactory As New FileStoreFactory(settings)
        Dim logFactory As New FileLogFactory(settings)

        initiator = New SocketInitiator(myApp, storeFactory, settings, logFactory)
    End Sub

    Public Sub ConnectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ConnectToolStripMenuItem.Click
        ToolStripDropDownButton1.Text = "Establishing connection..."
        ToolStripDropDownButton1.Image = My.Resources.Connecting
        initiator.Start()
    End Sub

    Public Sub DisconnectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DisconnectToolStripMenuItem.Click
        ToolStripDropDownButton1.Text = "Disconnecting..."
        ToolStripDropDownButton1.Image = My.Resources.Disconnecting
        initiator.Stop()
    End Sub
End Class

MyQuickFixApp.vb:

Imports QuickFix
Imports QuickFix.Transport
Imports QuickFix.Fields

Public Class MyQuickFixApp
    Inherits MessageCracker : Implements IApplication

    Dim _session As Session = Nothing

    Public Sub FromAdmin(message As Message, sessionID As SessionID) Implements IApplication.FromAdmin
        ClientGUI.RichTextBox1.AppendText("")
        ClientGUI.RichTextBox1.AppendText("IN (ADMIN): " + message.ToString())

        Try
            Crack(message, sessionID)
        Catch ex As Exception
            ClientGUI.RichTextBox1.AppendText("")
            ClientGUI.RichTextBox1.AppendText("==Cracker exception==")
            ClientGUI.RichTextBox1.AppendText(ex.ToString())
            ClientGUI.RichTextBox1.AppendText(ex.StackTrace)
        End Try
    End Sub

    Public Sub FromApp(message As Message, sessionID As SessionID) Implements IApplication.FromApp
        ClientGUI.RichTextBox1.AppendText("")
        ClientGUI.RichTextBox1.AppendText("IN (APP): " + message.ToString())

        Try
            Crack(message, sessionID)
        Catch ex As Exception
            ClientGUI.RichTextBox1.AppendText("")
            ClientGUI.RichTextBox1.AppendText("==Cracker exception==")
            ClientGUI.RichTextBox1.AppendText(ex.ToString())
            ClientGUI.RichTextBox1.AppendText(ex.StackTrace)
        End Try
    End Sub

    Public Sub ToApp(message As Message, sessionId As SessionID) Implements IApplication.ToApp
        Try
            Dim possDupFlag As Boolean = False
            If (message.Header.IsSetField(Tags.PossDupFlag)) Then
                possDupFlag = Converters.BoolConverter.Convert(message.Header.GetField(Tags.PossDupFlag))
            End If
            If (possDupFlag) Then
                Throw New DoNotSend()
            End If
        Catch ex As FieldNotFoundException
            ClientGUI.RichTextBox1.AppendText("OUT (APP): " + message.ToString())
        End Try
    End Sub

    Public Sub OnCreate(sessionID As SessionID) Implements IApplication.OnCreate
        '_session = Session.LookupSession(sessionID)
        ClientGUI.RichTextBox1.AppendText("Session created - " + sessionID.ToString())
    End Sub

    Public Sub OnLogon(sessionID As SessionID) Implements IApplication.OnLogon
        ClientGUI.RichTextBox1.AppendText("Logon - " + sessionID.ToString())
        ClientGUI.ToolStripDropDownButton1.Text = "Connected"
        ClientGUI.ToolStripDropDownButton1.Image = My.Resources.Connected
        'MsgBox("onlogon")
    End Sub

    Public Sub OnLogout(sessionID As SessionID) Implements IApplication.OnLogout
        ClientGUI.RichTextBox1.AppendText("Logout - " + sessionID.ToString())
        ClientGUI.ToolStripDropDownButton1.Text = "Disconnected"
        ClientGUI.ToolStripDropDownButton1.Image = My.Resources.Disconnected
    End Sub

    Public Sub ToAdmin(message As Message, sessionID As SessionID) Implements IApplication.ToAdmin
        ClientGUI.RichTextBox1.AppendText("OUT (ADMIN): " + message.ToString())
    End Sub

    Public Sub OnMessage(message As FIX42.Heartbeat, sessionID As SessionID)
        ClientGUI.RichTextBox1.AppendText("HEARTBEAT")
    End Sub
End Class
4

2 回答 2

1

我猜MyQuickFixApp类中的代码访问您的默认实例ClientGUI,而不是实际运行的实例,每次您编写ClientGUI.(...).

看到这个线程为什么在 VB.Net 中每个表单都有一个默认实例,但在 C# 中却没有?有关默认实例的更多信息,您应该避免使用。

所以你可以在 MyQuickFixApp类构造函数中添加一个参数:

 Public Class MyQuickFixApp
    Inherits MessageCracker : Implements IApplication

    Dim _clientGUI As ClientGUI = Nothing
    Public Sub New(cltGui As ClientGUI)
        _clientGUI = cltGui
    End sub

    (...)
End class

然后,在MyQuickFixApp类中替换所有ClientGUI.(...),_clientGUI.(...)以确保访问正确的实例。

最后,像这样初始化你的MyQuickFixAppClientGUI

Dim myApp As New MyQuickFixApp(me)

注意这段代码,你只能访问Form_Load事件中类的方法。ClientGUI如果您想稍后从表单中访问该变量,则应在类中声明并在 form_load 中对其进行初始化。

Public Class ClientGUI
    Dim initiator As SocketInitiator
    Dim myApp As MyQuickFixApp()

    Public Sub ClientGUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       (...)
       myApp =New MyQuickFixApp(Me)
       (...)
    End Sub

     (...)
End Class
于 2013-08-27T11:57:50.977 回答
0

通知

    private void button3_Click(object sender, EventArgs e)
    {
        TestClass tc = new TestClass();

        tc.addComment(richTextBox1);
    }

在班上

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

public class TestClass
{
    public void addComment(RichTextBox rt) 
    {
        rt.Text = rt.Text + Environment.NewLine + "My Dynamic Text";
    }
}

你也可以在 VB.net 中做同样的事情

于 2013-08-27T08:13:14.247 回答