【问题标题】:How to share a variable from usercontrol1 to usercontrol2如何将变量从 usercontrol1 共享到 usercontrol2
【发布时间】:2020-10-14 13:13:57
【问题描述】:

我正在使用包含 Usercontrol1 的 form1 和包含 usercontrol2 的 form2。

在 usercontrol1 中,一个网络浏览器正在提取一个链接,我想将此链接传递给 usercontrol2,因为它将使用 google api 创建一个二维码。

所以,在 usercontrol1 中,我只使用网页浏览器提取链接。

我试图声明一个公共“链接”,但它不起作用。

在 usercontrol2 我有:


     Dim sitoGoogleQrCode As String = "http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={TESTO}"
            sitoGoogleQrCode = sitoGoogleQrCode.Replace("{WIDTH}", PictureBox1.Width.ToString()).Replace("{HEIGHT}", PictureBox1.Height.ToString()).Replace("{TESTO}", WebUtility.UrlEncode(usercontrol1.link))
            Dim client As WebClient = New WebClient()

            Dim bytes As Byte() = client.DownloadData(sitoGoogleQrCode)
            client.Dispose()
            Dim memStream As MemoryStream = New MemoryStream(bytes)
            Dim bmpQrCode As Bitmap = New Bitmap(memStream)
            PictureBox1.Image = bmpQrCode

有没有办法在 usercontrol2 的这段代码中使用那个 Usercontrol1 链接变量?

WebUtility.UrlEncode(usercontrol1.link))
        Dim client As WebClient = New WebClient()

谢谢

edit1:使用第三个 Laancelot 代码: 在用户控件代码中:

    Public Class Usercontrol1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
dim link as string= webbrowser.getelement etc..

        Dim UC2 As usercontrol2
        UC2.DoSomething(link.tostring)
    End Sub
End Class

在 Usercontrol2 代码中:

    Public Class Usercontrol2
    Public Sub DoSomething(importantInformation As String) '<- I think is missing something on 3r code.
          Dim sitoGoogleQrCode As String = "http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={TESTO}"
            sitoGoogleQrCode = sitoGoogleQrCode.Replace("{WIDTH}", PictureBox1.Width.ToString()).Replace("{HEIGHT}", PictureBox1.Height.ToString()).Replace("{TESTO}", WebUtility.UrlEncode(UC2.link))
            Dim client As WebClient = New WebClient()

            Dim bytes As Byte() = client.DownloadData(sitoGoogleQrCode)
            client.Dispose()
            Dim memStream As MemoryStream = New MemoryStream(bytes)
            Dim bmpQrCode As Bitmap = New Bitmap(memStream)
            PictureBox1.Image = bmpQrCode
    End Sub
End Class

我做错了什么?

【问题讨论】:

  • 调用Form2的代码在哪里? link 变量在哪里?请阅读stackoverflow.com/help/minimal-reproducible-example
  • 当 UserControl1 使用 UserControl2 创建表单时,它可以将自身作为参数,因此您可以根据需要将其与 UserControl2 关联。
  • Kshkarin,我确实问对了,因为我不知道如何从 usercontrol1 调用 usercontrol2,因此,我也不知道如何从 form1 调用 form2。 .我的意思是,如果我知道,我不会问它。链接变量只是我从 webbrowser 中提取的一个简单字符串,它是一个链接,这个链接需要进入 usercontrol2 来创建二维码。
  • @laancelot,你的意思是什么?
  • 你成功通过了importantInformation,但你还没有真正使用它。

标签: vb.net


【解决方案1】:

这里有一些框架代码可以让你明白我的意思:

Public Class FormA
    Public Property SomeValueIWantToShare As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim formB As FormB(Me)
    End Sub
End Class

Public Class FormB
    Private myFormA As FormA

    Public Sub New(formA As FormA)
        myFormA = formA
        ' then later you can get the property you need like this:
        Dim stringFromA As String = formA.SomeValueIWantToShare
        ' if the text you need is in a textbox, for instance, it could be this:
        Dim anotherString As String = formA.ThatTextBoxWithTheString.Text
    End Sub
End Class

或者,如果您在实例化 Form2 时已经知道所需的信息,则可以改为提供该信息:

Public Class FormA
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim formB As FormB(ThatTextBoxWithTheString.Text)
    End Sub
End Class

Public Class FormB
    Public Sub New(importantInformation As String)
        DoSomething(importantInformation)
    End Sub

    Private Sub DoSomething(importantInformation As String)
        ' do stuff
    End Sub
End Class

或者:

Public Class FormA
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim formB As FormB
        formB.DoSomething(ThatTextBoxWithTheString.Text)
    End Sub
End Class

Public Class FormB
    Public Sub DoSomething(importantInformation As String)
        ' do stuff
    End Sub
End Class

我可以根据需要回答问题。玩得开心!

【讨论】:

  • 嗨 laancelot,我需要两个用户控件之间的通信,而不是论坛。我可以使用 usercontrol1 更改 form 并使用 usercontrol2 更改 formb 吗?我还需要申报表格吗?
  • @user14441867 您可以将任何内容作为参数传递,我添加了更多选项以便您了解总体思路。不过,我不确定您的问题是什么意思,如果您愿意进一步解释,我会尽力提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-23
  • 2019-11-12
  • 1970-01-01
  • 2021-05-13
  • 2014-04-20
  • 2018-03-23
相关资源
最近更新 更多