在vb.net中使用AddHandler和AddressOF的问题 [英] Problem using AddHandler and AddressOF in vb.net

查看:1100
本文介绍了在vb.net中使用AddHandler和AddressOF的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码:

 Dim img As Image 
Private Sub SendMail_Load(sender as Object,e As EventArgs)处理MyBase .Load
For Each fi As FileInfo in New DirectoryInfo(" D:\ emojis \ 1")。GetFiles()
Dim pbx As New PictureBox
pbx.Width = 32
pbx.Height = 32
pbx.Image = Image.FromFile(fi.FullName)
AddHandler pbx.Click,AddressOf pbx_click
FlowLayoutPanel1.Controls.Add(pbx)

下一个
结束Sub
Friend Sub pbx_click(发送者作为对象,e作为EventArgs)
img = pbx.Image
Clipboard.SetImage(img)
bodytxt.Paste()
End Sub

基本上我的动机是我想通过点击将图像添加到richtextbox(bodytxt)在formboxes(pbx)上生成的表单的加载事件。但是在pbx_click事件中我无法使用:image = pbx.image:It说PBX没有声明。所以我该如何解决?

解决方案


但是在pbx_click事件中我可以使用:image = pbx.image:它说PBX没有声明。所以我该如何修复它?



你已经在函数SendMail_Load中声明了变量PBX( ),因此其使用仅限于该功能。如果你想从其他地方的代码中引用它,那么它应该在所有函数都可以访问的级别(例如,表单级别)声明。

https://docs.microsoft.com/en-us/dotnet/visual - 基本/编程导/语言的功能/声明元素/范围


I am using this code :

    Dim img As Image
    Private Sub SendMail_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each fi As FileInfo In New DirectoryInfo("D:\emojis\1").GetFiles()
            Dim pbx As New PictureBox
            pbx.Width = 32
            pbx.Height = 32
            pbx.Image = Image.FromFile(fi.FullName)
            AddHandler pbx.Click, AddressOf pbx_click
            FlowLayoutPanel1.Controls.Add(pbx)

        Next
    End Sub
    Friend Sub pbx_click(sender As Object, e As EventArgs)
        img = pbx.Image
        Clipboard.SetImage(img)
        bodytxt.Paste()
    End Sub

Basically my motive is i want add image to a richtextbox(bodytxt) by clicking on pictureboxes(pbx)that are generated on form's load event.But in the pbx_click event i can't use : image=pbx.image : It says PBX NOT DECLARED.So how do i fix it?

解决方案

But in the pbx_click event i can't use : image=pbx.image : It says PBX NOT DECLARED.So how do i fix it?

You have declared the variable PBX within the function SendMail_Load(), so its usage is restricted to that function. If you want to refer to it from code in other places, then it should be declared at a level (eg, form level) where it is accessible to all functions.
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/scope


这篇关于在vb.net中使用AddHandler和AddressOF的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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