如何“看到”从usercontrol单击事件 [英] how to "see" click event from a usercontrol

查看:64
本文介绍了如何“看到”从usercontrol单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以最简单的形式,假设我创建了一个usercontrol,

包含按钮的WSToolBarButton。我想最终根据在其他地方获得的一些

初始化信息在运行时动态创建

的WSToolBarButton副本。基本上,我要打开
创建我自己的动态工具栏,其中工具栏按钮可以更改。我不是使用VB工具栏的
,因为更改诸如

backcolor之类的限制(我不能让它改变,但那是'另一个故事)。


出于测试目的,我只是将

usercontrol工具箱中的WSToolBarButton副本拖到表单中。我设置了一个子来处理WSToolBarButton的点击事件

。此子显示消息框usercontrol click

fired。问题是只有当我在WSToolBarButton中的某处单击

而不是在

WSToolBarButton中的按钮时才会触发click事件。如果我点击按钮没有任何反应。


所以,我进入了WSToolBarButton的代码并为

设置了一个单击事件按钮,显示一个msgbox按钮单击已触发。运行

程序,如果我单击按钮,我会点击按钮点击按钮就这样。我

没有得到usercontrol click fired。但是,如果我点击

WSToolBarButton中的某个地方而不是按钮,我会得到usercontrol click fired。


我的问题是我怎样才能设置WSToolBarButton,这样如果用户

点击控件中的任何地方,我将执行

MYUSERCONTROL的click事件(那个说usercontrol click fired的那个) 。我想我

不得不从按钮内点击WSToolBarButton点击事件

点击事件,但我只是不知道如何设置它。我曾经玩过

RAISEEVENT,但根本没有运气。我尝试直接从Button click事件子调用

WSToolBarButton_Click子,但是它没有冒泡到主窗体上的click事件。


我在这里完全没有想法......任何人都可以解释我需要什么来实现这一点?我已经在

msg的末尾添加了代码片段。


谢谢,

John

*********************此代码在我的用户控件中

*********** *****************

公共类WSToolbarButton

继承System.Windows.Forms.UserControl

#Region" Windows窗体设计器生成的代码 < snip>

#End Region

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

System.EventArgs)Handles Button1.Click

MsgBox(按钮点击触发)

WSToolbarButton_Click(发件人,e)

结束子


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

System.EventArgs)处理MyBase.Click

MsgBox(" wstoolbarbutton click raised" )

''此时主表单上此用户控件的点击事件

仅在此例程被触发时才被触发

''通过单击除按钮之外的某个位置的WSToolBarButton。如果

这个例程是从

''button1_click例程中调用的,那么

主窗体上的这个用户控件的click事件将不会执行。< br $>
结束次级

结束班级


******************这个代码来自我的主要表格

***************************

私人Sub WsToolbarButton1_Click(ByVal sender As Object,ByVal e As

System.EventArgs)处理WsToolbarButton1.Click

MsgBox(" usercontrol click fired")

End Sub

解决方案

John,


通常您也使用select case来查找单击了什么按钮。

我将它与标签一起使用,而不是像在此消息中那样。
http://groups-beta.google.com/group/...295fd1e1?hl=en


这是完全不同的由Herfried发布的nt解决方案,我发现很好

很好(当上面这个不能用,因为这个当然花了很多

更多的处理时间)。
http: //groups-beta.google.com/group/...3c24b4af?hl=en

我希望这会有所帮助,


Cor


" JohnR" <乔****** @ hotmail.com> schrieb:

我没有使用VB工具栏,因为改变事物的局限性如backcolor(我不能让它改变,但这是另一个故事) 。




虽然这就是你说的另一个故事,但很容易改变一个

工具栏的背景color:将控件''''Appearance''属性设置为

''Flat''并更改工具栏所在容器的背景颜色。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/petition/>


Hi Herfried,


我成功地改变了工具栏的背景颜色,但是它在格式化方面仍然看起来非常严格。

例如,你能不能告诉我以下是否可行(如果可能的话,

如何做?)。


1.更改按钮之间的间距,以获得更加用户满意的外观

2.更改背景实际按钮的颜色稍微有点

突出显示工具栏背景颜色

3.在按钮周围放置一个边框,再次给它一个更令人愉悦的

外观

4.增加指定为分隔符样式的按钮的宽度(作为一个

方式来真正按下真正的按钮更宽的)。


似乎像微软硬编码(或隐藏)很多属性和方法

制作工具栏所需要的'' s按钮易于格式化。这个

是我想创建我的usercontrol(由一个

按钮和标签组成)并模拟我自己的工具栏的主要原因。它将比使用MS工具栏更灵活。


鉴于此,我仍然有我原来的问题...如果我创建我的usercontrol

由一个按钮组成,例如,如果用户点击按钮,我可以如何传播点击事件,以便用户点击该按钮

我的usercontrol(请参阅原始帖子中的代码片段)。


如果您或其他任何人可以向我解释这一点,我将非常感激。

谢谢,

John


" Herfried K. Wagner [MVP]" <喜*************** @ gmx.at>在消息中写道

news:uz ************* @ TK2MSFTNGP10.phx.gbl ...

" JohnR" <乔****** @ hotmail.com> schrieb:

我没有使用VB工具栏,因为改变事物的局限性如backcolor(我不能让它改变,但这是另一个故事) 。



虽然这就像你说的那样,另一个故事,改变一个
工具栏的背景颜色非常容易:设置控件'' ''外观''属性
''Flat''并改变工具栏放置的容器的背景颜色。

-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/>



In it''s simplest form, assume that I have created a usercontrol,
WSToolBarButton that contains a button. I would like to eventually create
copies of WSToolBarButton dynamically at run time based on some
initialization information obtained elsewhere. Basically, I''m going to
create my own dynamic toolbar where the toolbarbuttons can change. I''m not
using the VB toolbar because of limitations in changing things like
backcolor (I can''t get it to change, but that''s another story).

For testing purposes I simply dragged a copy of WSToolBarButton from the
usercontrol toolbox to the form. I set up a sub to handle the click event
of the WSToolBarButton. This sub displays a messagebox "usercontrol click
fired". The problem is that the click event is fired only when I click
somewhere in WSToolBarButton but NOT in the button that is part of
WSToolBarButton. If I click on the button nothing happens.

So, I went into the code for WSToolBarButton and set up a click event for
the button which display a msgbox "button click fired". Running the
program if I click the button I get "button click fired" and that''s all. I
don''t get "usercontrol click fired". However, if I click somewhere in
WSToolBarButton but not in the button I do get "usercontrol click fired".

My question is how exactly can I set up WSToolBarButton so that if a user
clicks anywhere within the control I will execute the click event of the
MYUSERCONTROL (the one that says "usercontrol click fired") . I guess I
somehow have to fire the WSToolBarButton click event from within the button
click event but I just don''t know how to set it up. I''ve played with
RAISEEVENT but had no luck at all. I tried calling the
WSToolBarButton_Click sub directly from the Button click event sub, but it
doesn''t bubble up to the click event on the main form.

I''m totally out of ideas here... Can anybody explain what I would need to
do to get this to work? I''ve attached the code snippets at the end of the
msg.

Thanks,
John
********************* this code is in my usercontrol
****************************
Public Class WSToolbarButton
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code " <snip>
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
MsgBox("button click fired")
WSToolbarButton_Click(sender, e)
End Sub

Private Sub WSToolbarButton_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
MsgBox("wstoolbarbutton click raised")
''at this point the click event for this usercontrol on the main form
is fired ONLY IF this routine was caused
''by clicking the WSToolBarButton somewhere other than the button. If
this routine was called from the
''button1_click routine the click event for this usercontrol on the
main form WILL NOT execute.
End Sub
End Class

****************** this code is from my main form
***************************
Private Sub WsToolbarButton1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles WsToolbarButton1.Click
MsgBox("usercontrol click fired")
End Sub

解决方案

John,

Normally do you have too use the select case to find what button is clicked.
I use it with a tag and than like in this message.
http://groups-beta.google.com/group/...295fd1e1?hl=en

This is a complete different solution posted by Herfried, that I found very
nice (when the above cannot be used because this one cost of course much
more processing time).
http://groups-beta.google.com/group/...3c24b4af?hl=en

I hope this helps,

Cor


"JohnR" <Jo******@hotmail.com> schrieb:

I''m not using the VB toolbar because of limitations in changing things
like backcolor (I can''t get it to change, but that''s another story).



Although this is, as you say, another story, it''s very easy to change a
toolbar''s background color: Set the control''s ''Appearance'' property to
''Flat'' and change the backcolor of the container the toolbar is placed in.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


Hi Herfried,

I successfully changed the background color of the toolbar, however it
still seems VERY restrictive in what you can do in terms of formatting it.
For example, could you tell me if the following is possible (and if it is,
how to do it?).

1. change the spacing between buttons for a more user pleasing appearance
2. change the background color of the actual button to have it slightly
standout against the toolbar background color
3. put a border around the button, again to give it a more pleasing
appearance
4. increase the width of a button designated as a ''separator'' style (as a
way to space the real buttons a little wider).

Seems like microsoft hard coded (or hid) a lot of the properties and methods
that are needed to make the toolbar and it''s buttons easy to format. This
is the main reason why I wanted to create my usercontrol (composed of a
button and label) and simulate my own toolbar. It will be much more
flexible than using the MS toolbar.

Given that, I still have my original problem... If I create my usercontrol
consisting of a button, for example, if the user clicks on the button how
can I propagate the click event so that it appears that the user clicked on
my usercontrol (see original post for code snippets).

If you or anybody else can explain this to me I would be very grateful.

Thanks,
John

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uz*************@TK2MSFTNGP10.phx.gbl...

"JohnR" <Jo******@hotmail.com> schrieb:

I''m not using the VB toolbar because of limitations in changing things
like backcolor (I can''t get it to change, but that''s another story).



Although this is, as you say, another story, it''s very easy to change a
toolbar''s background color: Set the control''s ''Appearance'' property to
''Flat'' and change the backcolor of the container the toolbar is placed in.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>



这篇关于如何“看到”从usercontrol单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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