维护变量以与其他过程一起使用 [英] Maintaining variables for use with other procedures

查看:88
本文介绍了维护变量以与其他过程一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我可能会以错误的方式做这件事......



我试图将文本逐行附加到动态创建的文本框中。 />
程序有线程,这似乎是我现在可以让它工作的唯一方法。



第一个程序接收数据

Firstly, I could be doing this the wrong way...

I am trying to append text, line by line to a dynamically created textbox.
The program has threading, it seems to be the only way I can get it to work at the moment.

1st procedure receives the data

Private Sub tcp()
        Dim server As TcpListener
        server = Nothing

        Do

            Try

                ' Set the TcpListener on port 13000.
                Dim port As Int32 = 22490
                Dim localAddr As IPAddress = IPAddress.Parse("127.0.0.1")

                server = New TcpListener(localAddr, port)

                ' Start listening for client requests.
                server.Start()

                ' Buffer for reading data
                Dim bytes(1024) As Byte
                Dim data As String = Nothing



                ' Enter the listening loop.
                While True
                    ' Perform a blocking call to accept requests.
                    ' You could also user server.AcceptSocket() here.
                    Dim client As TcpClient = server.AcceptTcpClient()


                    data = Nothing

                    ' Get a stream object for reading and writing
                    Dim stream As NetworkStream = client.GetStream()
                    '                  Sleep(2000)
                    Dim i As Int32

                    ' Loop to receive all the data sent by the client.
                    i = stream.Read(bytes, 0, bytes.Length)

                    While (i <> 0)


                        'translate data bytes to a ASCII string.

                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)

                        'process the data sent by the client.
                        Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)



                        'send back a response.
                        stream.Write(msg, 0, msg.Length)

                        stream.Read(bytes, 0, bytes.Length)


                        datastring = data

                        txtboxcreate(datastring)
                        i = 0
                    End While


                    ' Shutdown and end connection
                    client.Close()
                    append(datastring)

                End While
                currenttb = Nothing
            Catch e As SocketException
            Finally
                server.Stop()

            End Try
        Loop
    End Sub 'Main





第二个程序,创建一个名称等的文本框。



2nd procedure, creates a textbox with names etc.

Private Sub txtboxcreate(ByVal datastring As System.Object)
        Dim count As Integer = 1
        Dim tb As New RichTextBox
        Dim currenttb As RichTextBox
        Name = ("TxtBox" & CStr(counttotal))
        '      Dim text As String = datastring
        With tb
            .Name = Name
            '         .Text = text
            .Multiline = True
            .AutoSize = True
            .Width = 250
            .ReadOnly = True
            .ScrollBars = False
            '       .MaxLength = 100
            .Tag = 20
            .WordWrap = True
            .BackColor = Color.Black

            '           move unused properties under here'
            ' .ScrollBars = ScrollBars.Both
            '           tb.MaxLength = 100
            '           tb.Height = 100
            '           tb.AutoSize = True
            ' add events to the textbox created
            AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
        End With
        count = (count + counttotal)
        ' this defines where the textboxes appear

        currenttb = tb
        updatefrm(tb)
    End Sub





第3个程序调用flowlayoutpanel并添加文本框



3rd procedure invokes the flowlayoutpanel and adds the textbox

Private Sub updatefrm(ByVal sender As RichTextBox)
        'The tcp sub runs as a seperate thread. If you try to call the form directly it won't work as you are cross threading
        'you need to invoke "Call" the control you want to do the work with - in this case flowlayoutpanel1
        If InvokeRequired Then
            Invoke(Sub() FlowLayoutPanel1.Controls.Add(sender))
        Else : FlowLayoutPanel1.Controls.Add(sender)
        End If
    End Sub







第4程序然后逐行读取第1程序中的数据,查找某些关键字(xred,xblk)




4th procedure then reads the data from the 1st procedure line by line, looking for certain keywords (xred, xblk)

Private Sub append(ByVal sender As System.Object)
        Dim strreader As New StringReader(datastring)
        Dim line As String
        While True
            line = strreader.ReadLine()
            If line Is Nothing Then
                Exit While
            End If
            If line.Contains("xred") Then
                currenttb.ForeColor = Color.Red
            End If
            If line.Contains("xblk") Then
                currenttb.ForeColor = Color.White
            End If
            currenttb.AppendText(line)

        End While
    End Sub







这就是我应该在脑子里工作的方式,但是。



我有一个声明为currenttb的变量声明red public(我试过公共共享)以及


在设置tb设置的过程2中,我有一行将创建的tb链接到currenttb。并且,虽然过程2仍然是最新的,但我可以看到currenttb变量已经存储了我需要的所有内容。但是,当它转移到第3个程序时,它会丢失信息。



我唯一能想到的是我在错误的位置声明currenttb变量。但我不知道还能把它放在哪里呢?



我已经设置了所以一旦所有内容都运行完毕,变量就会被设置为空,这样当下一个作业通过时,就会有一个空的持有者。



对所有的编码很抱歉.....




This is how it's supposed to work in my head, But.

I have got a variable declared called currenttb which is declared public (I've tried public shared aswell)

During procedure 2 where the tb settings are set I have a line which links the tb created to currenttb. And, while procedure 2 is still current, I can see the currenttb variable has everything stored that I need. But, when it moves to the 3rd procedure it loses the information.

The only thing I can think of is I'm declaring the currenttb variable in the wrong spot. But I don't know where else to put it?

I've got it set so once everything is run through, the variable is then set to nothing, so that way when the next job comes through, it's got an empty place holder.

Sorry about all the coding.....

推荐答案

你需要了解ByRef和ByVal之间的区别以及功能是什么



删除=>昏暗的currenttb作为RichTextBox从程序2内部使其全局并传递参考



You need to understand what the difference between ByRef and ByVal and what a function is

Remove => Dim currenttb As RichTextBox from inside Procedure 2 make it global and pass the reference in

' currenttb is defined outside procedures it wont disappear :-)
Dim currenttb As RichTextBox

' Pass currenttb in to procedure 2 by reference
Private Sub txtboxcreate(ByVal datastring As System.Object, ByRef currenttb As RichTextBox)
       blah blah blah
 End Sub

' Pass currenttb in to procedure 4 by reference
Private Sub append(ByVal sender As System.Object, ByRef currenttb As RichTextBox)
       blah blah blah
 End Sub


这篇关于维护变量以与其他过程一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆