问题描述
所以我正在创建一个程序,要求我检查表单是否打开,如果是,那么创建一个图片框。现在我有这个代码
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?
推荐答案
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
这篇关于如果表单是打开创建图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!