如何将变量从 usercontrol1 共享到 usercontrol2 [英] How to share a variable from usercontrol1 to usercontrol2

查看:27
本文介绍了如何将变量从 usercontrol1 共享到 usercontrol2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

因此,在 usercontrol1 中,我没有使用其他任何东西,只是使用 webbrowser 提取链接.

我试图声明一个公共链接"但它不起作用.

在 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)客户端处理()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:使用第三个兰斯洛特代码:在用户控制代码中:

 公共类 Usercontrol1Private Sub Button1_Click(sender As Object, e As EventArgs) 处理Button1.Click将链接变暗为 string= webbrowser.getelement 等.Dim UC2 作为 usercontrol2UC2.DoSomething(link.tostring)结束子结束类

在 Usercontrol2 代码中:

 公共类 Usercontrol2Public Sub DoSomething(importantInformation As String) '<- 我认为 3r 代码中缺少某些内容.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)客户端处理()Dim memStream As MemoryStream = New MemoryStream(bytes)Dim bmpQrCode As Bitmap = New Bitmap(memStream)PictureBox1.Image = bmpQrCode结束子结束类

我做错了什么?

解决方案

以下是一些框架代码,可让您了解我的意思:

公共类FormA公共属性 SomeValueIWantToShare As StringPrivate Sub Button1_Click(sender As Object, e As EventArgs) 处理Button1.Click将 formB 调暗为 FormB(Me)结束子结束类公开课FormB私有 myFormA 作为 FormAPublic Sub New(formA As FormA)myFormA = formA' 然后你可以像这样获得你需要的属性:Dim stringFromA As String = formA.SomeValueIWantToShare' 例如,如果您需要的文本在文本框中,则可能是这样的:Dim anotherString As String = formA.ThatTextBoxWithTheString.Text结束子结束类

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

公共类FormAPrivate Sub Button1_Click(sender As Object, e As EventArgs) 处理Button1.Click将 formB 调暗为 FormB(ThatTextBoxWithTheString.Text)结束子结束类公开课FormBPublic Sub New(importantInformation As String)做某事(重要信息)结束子Private Sub DoSomething(importantInformation As String)' 做东西结束子结束类

或者:

公共类FormAPrivate Sub Button1_Click(sender As Object, e As EventArgs) 处理Button1.Click将 formB 调暗为 FormBformB.DoSomething(ThatTextBoxWithTheString.Text)结束子结束类公开课FormBPublic Sub DoSomething(importantInformation As String)' 做东西结束子结束类

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

I'm using form1 which contains Usercontrol1 and form2 which contains usercontrol2.

In usercontrol1 a webbrowser is extracting a link and I'd like to pass this link to usercontrol2 becuase It will create a QR code with google api.

So, in usercontrol1 I am not using nothing else than just webbrowser extracting the link.

I tried to declare a public "link" but it doesn't work.

In usercontrol2 I have:


     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

There is a way to use that Usercontrol1 link variable in this code in usercontrol2?

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

Thanks

edit1: Using 3rd Laancelot code: In Usercontrol code:

    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

While in Usercontrol2 code:

    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

What I'm doing wrongly?

解决方案

Here's some skeleton code to give you an idea of what I mean:

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

Alternatively, if you already know the information you need when you instantiate Form2, you can just give it that information instead:

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

Or:

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

I can answer questions as needed. Have fun!

这篇关于如何将变量从 usercontrol1 共享到 usercontrol2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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