.NET面板控件中的错误?或者我错过了什么? [英] Bug in .NET panel control? Or am I missing something?

查看:113
本文介绍了.NET面板控件中的错误?或者我错过了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,看看这个...


我有一个带有面板控制和按钮的表格(在面板外面

控制)。


我有两个事件处理程序 - 一个处理

表单上按钮的click事件。另一个处理自定义的CardInserted我写的一个班级的活动

监视智能卡插入附带的智能卡

读卡器。


两个事件处理程序中包含* exact *相同的代码 - 这是在事件触发时向面板控件添加一个

的新按钮控件。


按钮'的点击事件的代码工作正常 - 按钮被添加到面板控件没有任何问题的



我的代码CardInserted" event - 与

button.click代码完全相同的代码不起作用。该事件被引发(验证),代码

运行(已验证),但它STOPS在panel.controls.add(newbutton)运行

行。它没有抛出错误,它只是停止运行,没有按钮获得

添加。该程序继续运行,我可以继续通过表单'的button.click添加按钮到

面板控件,但是CardInserted按钮。代码

根本不会运行 - *尽管代码与button.click相同

代码*!


我试过用一个简单的

" msgbox panel.controls.count"换掉panel.controls.add(newbutton)行。并且工作正常 - 所以我可以从CardInserted事件中看到

面板控件。我甚至可以做一个

" panel.controls.remove"命令,如果我想 - 我只是不能在我的CardInserted事件处理程序中添加一个新的控件




我很困惑。为什么我无法在我的CardInserted事件处理程序下运行适用于

button.click的代码?我不能做的唯一的事情

添加一个控件。


这是为什么?它是面板控件中的一个错误吗?

OK, check this out...

I have a form with a panel control and button on it (outside the panel
control).

I have two event handlers - one handles the click event of the button on the
form. The other handles a custom "CardInserted" event for a class I wrote
that watches for smart cards to be inserted into an attached smart card
reader.

BOTH event handlers have the *exact* same code in them - which is to add a
new button control into the panel control whenever the event fires.

The code for the button''s click event works just fine - buttons get added to
the panel control without any problem.

The code for my "CardInserted" event - which is the EXACT same code as the
button.click code does NOT work. The event gets raised (verified), the code
runs (verified), but it STOPS running at the panel.controls.add(newbutton)
line. It doesn''t throw an error, it just stops running and no button gets
added. The program continues to run and I can keep adding buttons to the
panel control via the form''s button.click, but the "CardInserted" code
simply will not run - *despite being the same code as the button.click
code*!

I tried swapping out the panel.controls.add(newbutton) line with a simple
"msgbox panel.controls.count" and that works just fine - so I can see the
panel control from the CardInserted event. I can even do a
"panel.controls.remove" command if I want - I just can''t ADD a new control
during my CardInserted event handler.

I''m totally confused. Why would I be unable to run code that works for
button.click under my CardInserted event handler? The ONLY thing I can''t do
is add a control.

Why is this? Is it a bug in the panel control?

推荐答案

我设置了一个只有基本元素的超简化应用程序。它是一个带有面板控件和一个按钮的形式。


form_load中的东西是让我的CardInserted事件触发。当我用
插入一张智能卡时,卡已插入 msgbox命令关闭,

添加按钮 msgbox命令关闭,但按钮已添加 msgbox

命令不会触发。当从/ b
按钮调用Add子时它会触发。


所以*某些东西*不允许使用panel1.controls.add(pButton)命令

执行。


表单背后的代码如下:


私有mintIndex As Integer

Private WithEvents mBECardReaderManager As BECardReaderManager


Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As

System.EventArgs)处理Button1 .Click

AddButton()

End Sub


Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As

String,ByVal eCardReaderNumber As Integer,ByVal eCardID As String)句柄

mBECardReaderManager.BECardInserted

MsgBox(" Card Inserted")

AddButton( )

End Sub


Private Sub AddButton()

Dim pButton作为新按钮

pButton .Height = 50

pButton.Width = 100
pButton.Left =(mintIndex Mod 5)* 100

pButton.Top =(mintIndex \ 5)* 50

pButton.Text = 按钮# &安培; mintIndex

mintIndex = mintIndex + 1

MsgBox(添加按钮)

Panel1.Controls.Add(pButton)

MsgBox(添加按钮)

结束子


私有子Form1_Load(ByVal发送者为对象,ByVal e As System.EventArgs)

处理MyBase.Load

Dim mBEReg作为新的BERegistrySettings

mBEReg.LoadRegistrySettings()

mBECardReaderManager =新的BECardReaderManager

mBECardReaderManager.Initialize(mBEReg)

End Sub
I set up a super-streamlined app with only the essential elements. It''s one
form with a panel control and a button on it.

The stuff in form_load is to get my CardInserted event to fire. When I
insert a smart card, the "Card Inserted" msgbox command goes off, the
"Adding Button" msgbox command goes off, but the "Button Added" msgbox
command does NOT fire. It does fire when the Add sub is called from
Button1_Click.

So *something* is not allowing that panel1.controls.add(pButton) command to
execute.

The code behind the form looks like this:

Private mintIndex As Integer
Private WithEvents mBECardReaderManager As BECardReaderManager

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddButton()
End Sub

Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String) Handles
mBECardReaderManager.BECardInserted
MsgBox("Card Inserted")
AddButton()
End Sub

Private Sub AddButton()
Dim pButton As New Button
pButton.Height = 50
pButton.Width = 100
pButton.Left = (mintIndex Mod 5) * 100
pButton.Top = (mintIndex \ 5) * 50
pButton.Text = "Button #" & mintIndex
mintIndex = mintIndex + 1
MsgBox("Adding Button")
Panel1.Controls.Add(pButton)
MsgBox("Button Added")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Dim mBEReg As New BERegistrySettings
mBEReg.LoadRegistrySettings()
mBECardReaderManager = New BECardReaderManager
mBECardReaderManager.Initialize(mBEReg)
End Sub





您的CardInserted事件是否由其他线程引发?


如果是这样,您将无法以您尝试的方式访问该面板。

Windows控件不是线程安全的。您只能在创建它们的线程上访问它们(带有几个例外的
)。您需要使用

Panel1.Invoke才能将委托传递给面板,然后可以使用

添加按钮。


HTH


Charles

" BoloBaby" <博****** @ hotmail.com>在消息中写道

新闻:1o ******************** @ comcast.com ...
Hi

Does your CardInserted event get raised by a different thread?

If so, you will not be able to access the panel in the way you are trying.
Windows control are not thread safe. You can only access them (with a couple
of exceptions) on the thread that they were created on. You need to use
Panel1.Invoke in order to pass a delegate to the panel which it can then use
for adding the button.

HTH

Charles
"BoloBaby" <bo******@hotmail.com> wrote in message
news:1o********************@comcast.com...
我建立一个只有基本元素的超级简化的应用程序。它是
一个带有面板控件和一个按钮的表单。

form_load中的东西是让我的CardInserted事件触发。当我插入智能卡时,卡已插入 msgbox命令关闭,
添加按钮 msgbox命令关闭,但按钮已添加 msgbox
命令不会触发。当从Add1_Click调用Add子时它会触发。

所以*某些东西*不允许执行panel1.controls.add(pButton)命令
。 />
表单背后的代码如下所示:

Private mintIndex As Integer
Private WithEvents mBECardReaderManager As BECardReaderManager

私有Sub Button1_Click(ByVal发送者)作为System.Object,ByVal e As
System.EventArgs)处理Button1.Click
AddButton()
End Sub
私有子mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String,ByVal eCardReaderNumber As Integer,ByVal eCardID As String)
处理mBECardReaderManager.BECardInserted
MsgBox(" Card Inserted")
AddButton()
End Sub

Private Sub AddButton()
Dim pButton作为新按钮
pButton.Height = 50
pButton.Width = 100
pButton.Left =(mintIndex Mod 5) * 100
pButton.Top =(mintIndex \ 5) * 50
pButton.Text =" Button#" &安培; mintIndex
mintIndex = mintIndex + 1
MsgBox(添加按钮)
Panel1.Controls.Add(pButton)
MsgBox(添加按钮)
End Sub

Private Sub Form1_Load(ByVal sender As Object,ByVal e As
System.EventArgs)处理MyBase.Load
Dim mBEReg作为新的BERegistrySettings
mBEReg.LoadRegistrySettings ()
mBECardReaderManager =新BECardReaderManager
mBECardReaderManager.Initialize(mBEReg)
End Sub
I set up a super-streamlined app with only the essential elements. It''s one form with a panel control and a button on it.

The stuff in form_load is to get my CardInserted event to fire. When I
insert a smart card, the "Card Inserted" msgbox command goes off, the
"Adding Button" msgbox command goes off, but the "Button Added" msgbox
command does NOT fire. It does fire when the Add sub is called from
Button1_Click.

So *something* is not allowing that panel1.controls.add(pButton) command to execute.

The code behind the form looks like this:

Private mintIndex As Integer
Private WithEvents mBECardReaderManager As BECardReaderManager

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddButton()
End Sub

Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String) Handles mBECardReaderManager.BECardInserted
MsgBox("Card Inserted")
AddButton()
End Sub

Private Sub AddButton()
Dim pButton As New Button
pButton.Height = 50
pButton.Width = 100
pButton.Left = (mintIndex Mod 5) * 100
pButton.Top = (mintIndex \ 5) * 50
pButton.Text = "Button #" & mintIndex
mintIndex = mintIndex + 1
MsgBox("Adding Button")
Panel1.Controls.Add(pButton)
MsgBox("Button Added")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mBEReg As New BERegistrySettings
mBEReg.LoadRegistrySettings()
mBECardReaderManager = New BECardReaderManager
mBECardReaderManager.Initialize(mBEReg)
End Sub



I没有在我的应用程序中设置任何多线程。


奇怪的是,我可以访问面板控件来显示一个

panel.controls.count甚至执行一个panel.controls.remove - 只是不能

add。

" Charles Law" < BL *** @ nowhere.com>在留言中写道

新闻:eL ************** @ tk2msftngp13.phx.gbl ...
I have not set up any multithreading within my application.

Oddly enough, I can access the panel control to display a
panel.controls.count or even perform a panel.controls.remove - just can''t
add.
"Charles Law" <bl***@nowhere.com> wrote in message
news:eL**************@tk2msftngp13.phx.gbl...


你的CardInserted事件是否由另一个线程引发?

如果是这样,你将无法以你尝试的方式访问该面板。
Windows控件不是线程安全。您只能在创建它们的线程上访问它们(带有
两个例外)。您需要使用
Panel1.Invoke才能将委托传递给面板,然后可以使用
来添加按钮。

HTH
Charles

" BoloBaby" <博****** @ hotmail.com>在消息中写道
新闻:1o ******************** @ comcast.com ...
Hi

Does your CardInserted event get raised by a different thread?

If so, you will not be able to access the panel in the way you are trying.
Windows control are not thread safe. You can only access them (with a couple of exceptions) on the thread that they were created on. You need to use
Panel1.Invoke in order to pass a delegate to the panel which it can then use for adding the button.

HTH

Charles
"BoloBaby" <bo******@hotmail.com> wrote in message
news:1o********************@comcast.com...
我成立了一个超级 - 流线型应用程序,只有基本元素。它是
I set up a super-streamlined app with only the essential elements. It''s


一个带有面板控件和按钮的


one

表单。

form_load中的东西是获取我的CardInserted事件开火。当我插入智能卡时,卡已插入 msgbox命令关闭,
添加按钮 msgbox命令关闭,但按钮已添加 msgbox
命令不会触发。当从Add1_Click调用Add子时它会触发。

所以*某些东西*不允许使用panel1.controls.add(pButton)命令
form with a panel control and a button on it.

The stuff in form_load is to get my CardInserted event to fire. When I
insert a smart card, the "Card Inserted" msgbox command goes off, the
"Adding Button" msgbox command goes off, but the "Button Added" msgbox
command does NOT fire. It does fire when the Add sub is called from
Button1_Click.

So *something* is not allowing that panel1.controls.add(pButton) command


执行。

表单背后的代码如下所示:

Private mintIndex As Integer
Private WithEvents mBECardReaderManager As BECardReaderManager System.EventArgs)处理Button1.Click
AddButton()
End Sub
<私有子mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String,ByVal eCardReaderNumber As Integer,ByVal eCardID As String)
execute.

The code behind the form looks like this:

Private mintIndex As Integer
Private WithEvents mBECardReaderManager As BECardReaderManager

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
AddButton()
End Sub

Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String)


句柄

mBECardReaderManager.BECardInserted
MsgBox(插入卡片)
AddButton()
End Sub

私人子AddButton()
昏暗pButton作为新按钮
pButton.Height = 50
pButton.Width = 100
pButton.Left =(mintIndex Mod 5)* 100
pButton.Top =(mintIndex \ 5)* 50
pButton.Text =" Button#" &安培; mintIndex
mintIndex = mintIndex + 1
MsgBox(添加按钮)
Panel1.Controls.Add(pButton)
MsgBox(添加按钮)
End Sub

Private Sub Form1_Load(ByVal sender As Object,ByVal e As
mBECardReaderManager.BECardInserted
MsgBox("Card Inserted")
AddButton()
End Sub

Private Sub AddButton()
Dim pButton As New Button
pButton.Height = 50
pButton.Width = 100
pButton.Left = (mintIndex Mod 5) * 100
pButton.Top = (mintIndex \ 5) * 50
pButton.Text = "Button #" & mintIndex
mintIndex = mintIndex + 1
MsgBox("Adding Button")
Panel1.Controls.Add(pButton)
MsgBox("Button Added")
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As


System.EventArgs)


System.EventArgs)

Handles MyBase.Load
昏暗的mBEReg作为新的BERegistrySettings
mBEReg.LoadRegistrySettings()
mBECardReaderManager =新的BECardReaderManager
mBECardReaderManager.Initialize(mBEReg)
End Sub
Handles MyBase.Load
Dim mBEReg As New BERegistrySettings
mBEReg.LoadRegistrySettings()
mBECardReaderManager = New BECardReaderManager
mBECardReaderManager.Initialize(mBEReg)
End Sub




这篇关于.NET面板控件中的错误?或者我错过了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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