如果表单是打开创建图片框 [英] if form is open create picturebox

查看:73
本文介绍了如果表单是打开创建图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在创建一个程序,要求我检查表单是否打开,如果是,那么创建一个图片框。现在我有这个代码

so i am making a program that requires me to check if a form is open and if that is true then create a picturebox. right now i have this code

Private Sub MyBase_load(Sender As Object, E As EventArgs) Handles MyBase.Load

    For Each form In My.Application.OpenForms
        If (form.name = Me.Name) Then
            'form is loaded so can do work
            Dim Logo As New PictureBox
            Logo.BackgroundImage = My.Resources.ChangeWallpaperlogojpg
            Logo.Visible = True
            Logo.Width = 32
            Logo.Height = 32
            Logo.Location = New Point(72, 136)
            Controls.Add(Logo)

        End If
    Next
End Sub



但这不起作用



任何人都知道答案吗?


But this doesnt work

Anyone know the answer?

推荐答案

我自己测试过,这段代码正常运行:

I tested it by myself and this code is working :
Private Sub MyBase_load(Sender As Object, E As EventArgs) Handles MyBase.load

    For Each form In Application.OpenForms
        If (form.name = Me.Name) Then
            'form is loaded so can do work
            Dim Logo As New PictureBox
             Logo.BackgroundImage = My.Resources.Resources.Desert
            Logo.Visible = True
            Logo.Width = 32
            Logo.Height = 32
            Logo.Location = New Point(72, 136)
            Controls.Add(Logo)
            'Logo.Parent = Me
        Else
        End If
    Next
End Sub





我看到的区别是BackgroundImage的名称。在我的示例中,我不使用文件名扩展名 - 也许你用你的代码尝试它(代码字母jpg)。



但是这个Logo永远显示。我想,您希望仅在表格处于活动状态时才能看到徽标。因此代码应该有一些修改:



首先创建名为Logo的PictureBox作为表单上的控件。

然后将此代码插入您的表单(而不是上面的代码):



The difference I see is the name of the BackgroundImage. In my sample I don't use the Filename-Extension - perhaps you try it like this with your code (substrct the letters "jpg").

But this Logo is displayed ever. I think, you wish to see the Logo only if the Form is active. Therefore the code should have "some" modifications :

At first create the PictureBox called "Logo" as control on your Form.
Then insert this code to your Form (instead of the code seen above) :

Protected Overrides Sub OnActivated(e As System.EventArgs)
    Logo.Visible = True
    MyBase.OnActivated(e)
  End Sub

Protected Overrides Sub OnDeactivate(e As System.EventArgs)
    Logo.Visible = False
    MyBase.OnDeactivate(e)
End Sub


一个明显的问题是你创建了一个图片框,你不要把它放在任何地方,这样它就不可见了。尝试将图片框添加到控件集合中。



参见 Form.ControlCollection类 [ ^ ]



另一件事是你正在调查新表单已加载时的表单。根据您的要求,在加载新表格之前调查存在更有意义吗?
One obvious problem is that you create a picture box put you don't put it anywhere so it won't be visible. Try adding the picture box to the controls collection.

See Form.ControlCollection Class[^]

Another thing is that you're investigating the forms when the new form is already loading. BAsed on your requirements would it make more sense to investigate the existence before loading a new form?


这篇关于如果表单是打开创建图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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