为类添加鼠标事件参数 [英] Add mouse event arguments for a class

查看:68
本文介绍了为类添加鼠标事件参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我已经建立了一个产生矩形的类

30个,每组3个.10。



我有什么问题是?

当我实例化说当地副本



例如:

Ok I have built a class that produces rectangles
30 of them in a 3 groups of 10.

What I am having issue with is ?
when I instantiate say a local copy

example:

Dim betline1 As New TicketGroupNodeControl

           betline1.NewNumericUpAndDownXValue() = NumericUpDown1.Value
           betline1.NewNumericUpAndDownYValue() = NumericUpDown2.Value
           betline1.NewNumericUpAndDownXXValue() = NumericUpDown3.Value
           betline1.NewNumericUpAndDownYYValue() = NumericUpDown4.Value

           betline1.FibTrainerOhioPk3BetLineNodeGroup()

           For i = 0 To betline1.BetLineNodeGroup.Length - 1
               e.Graphics.DrawRectangle(Pens.Blue, betline1.BetLineNodeGroup(i))
               e.Graphics.FillRectangle(Brushes.Blue, betline1.BetLineNodeGroup(i))

           Next i





当我尝试使用鼠标事件时

它要求我必须全局声明betline1变量才能使其工作。

当试图使用mousedown和mousemove时。



我想要完成的是能够选择组中的第一个矩形并使用鼠标将其拖动到位置。



什么我试过了:



这实际上有效,但正如我所说它要求我将变量全局化。

我需要它保持在本地,所以当它没有被使用它不存在。



我可能有100个这些复制用于不同的设置。

但是只说10个设置在任何给定时间都有效。







When i try to use mouse event args
it requires me to have to declare betline1 variable globally in order for it to work.
When trying to use mousedown and mousemove.

What I am trying to accomplish is being able to select the first rectangle in the group and drag it to location using the mouse.

What I have tried:

This actually worked but as i stated it requires me to make the variable global.
I need it to remain local so when its not being used it doesnt exist.

I will possibly have 100s of these copys for different setups .
but only say 10 setups will be active at any given time.


Private Sub RadDiagram1_MouseMove(sender As Object, e As MouseEventArgs) Handles RadDiagram1.MouseMove
       Label1.Text = "X:" & e.X.ToString & "     " & "Y:" & e.Y.ToString

       'If e.Button = Windows.Forms.MouseButtons.Left AndAlso betConfig1.BetConfigNodeGroup(0).Contains(e.Location) Then
       '    NumericUpDown21.Value = e.X + x
       '    NumericUpDown22.Value = e.Y + y
       '    RadDiagram1.Invalidate()
       'End If










Private Sub RadDiagram1_MouseDown(sender As Object, e As MouseEventArgs) Handles RadDiagram1.MouseDown
       'If e.Button = Windows.Forms.MouseButtons.Left AndAlso betConfig1.BetConfigNodeGroup(0).Contains(e.Location) Then
       '    x = NumericUpDown21.Value - e.X
       '    y = NumericUpDown21.Value - e.Y
       'End If
   End Sub





如果我将betconfig1设为全局,则可行。



如何将它添加到类或可能添加某种处理程序。



可以给我一个例子,我仍然可以从我在picturebox1或raddiagram 1的paint例程中实例化的类的瞬间调用鼠标参数



当我尝试使用此变量来触发mousemove事件时。我不能在那个子中使用这个变量,因为它在绘图程序中是本地的。



任何帮助都会受到赞赏。

我完全是自学成才,不知道哪个方向可以自己解决这个问题。



谢谢Jeff



If I make betconfig1 a global this works.

How can i add it to a class or possibly add a handler of some sort .

Can some one give me an example of how i can still call the mouse argument from an instant of a class that I instantiate in the paint routine of picturebox1 or "raddiagram 1"

When I try to use this variable to fire the mousemove event . I cannot use this variable in that sub because it was local in the paint routine.

Any help would be appreciated.
I am completely self taught and just hung not for sure which way to head to read my way out of this problem.

Thanks Jeff

推荐答案

这取决于你触发鼠标事件的位置 - 如果它是TicketGroupNodeControl的一部分,那么触发事件的控件实例将在 sender 事件处理程序的参数 - 所有你需要做的就是将它强制转换为适当的类型并使用它。

如果不是,那么当鼠标停止时控制鼠标结束单击按钮将被提供 - 所以如果你的组控件包含你在评论中搜索的控件,那么它已经可以通过 sender 参数获得。



使用调试器,看看参数 - 它可能会有所帮助你弄清楚发生了什么。





我不确定你在那里谈论什么?

我正在使用一个类并在picturebox1的paint例程中创建这个类的实例说。



这是油漆

It depends on where you mouse event is being triggered - if it's part of the TicketGroupNodeControl, then the instance of the control that triggered the event is passed to you in the sender parameter of the event handler - all you have to do is cast it to the appropriate type and use it.
If it isn't, then the control that the mouse was over when the button was clicked will be supplied - so if your group control contains the controls you are searching in the comments, then it's already available via the sender parameter.

Use the debugger, and have a look at the parameter - it may help you work out what is going on.


am not sure what your talking about there ?
I am using a class and creating an instance of this class in side of the paint routine of picturebox1 say.

This is in paint
Dim betline1 As New TicketGroupNodeControl





然后我绘制了30个矩形。



我想使用mousedown事件和mousemove事件

来检查当按下鼠标左键时鼠标指针是否在第一个rectangle.contains(e.location)中。

如果是这样拖动方块然后在释放时更新位置。



但是当我使用鼠标事件时。

betline1.variable或实例在此范围内无效。因为它是在paint sub中创建的。



我想我的问题是我如何通过它来让鼠标事件起作用。






好​​的......我们将不得不在这里做几步,因为你要做的不正确!



首先,你不应该在Paint事件处理程序中创建对象,原因有两个:

1)它需要尽可能短而且活泼或者它会严重影响你的UI。

2)Paint是一个常旅客事件 - 它会被触发 很多 :系统每次都需要向用户显示更改。



这意味着不应在Paint事件中创建用于显示的对象 - 它们需要在处理Paint事件时准备就绪。想一想:当你拖动你的一个方格时,你需要处理油漆事件以显示它的移动! :笑:



所以首先在容器中为正方形创建正方形(可能是你的表格,但我自己也会使用UserControl - 别担心关于那个,你以后会来找他们。)

你的方块需要在Paint事件中可用(所以它们可以绘制)以及其他事件内部 - MouseDown和MouseMove需要它们好吧 - 否则他们不能一起工作来移动和显示你的物品。

所以创建你的方块作为包含类的一部分 - 可能是表格 - 并在处理程序中使用它们。看看将Square作为Form.Shown事件的一部分添加,并在想要更新显示时调用Invalidate。

这有意义吗?



Then after I have drawn the 30 rectangles .

I am wanting to use the mousedown event and mousemove event
to check if the mouse pointer is in the first rectangle.contains(e.location) when the left mouse button is pressed .
If so drag the square then update the location when released.

But when I use the mouse event args.
betline1.variable or instance is not valid in this scope. because it was created in paint sub.

I guess my question is how do i pass this on to allow the mouse events to work .



OK...we're going to have to go back a couple of steps here, because what you are trying to do is not correct!

The first thing is that you should not create your objects in the Paint event handler, for two reasons:
1) It needs to be as short and "snappy" as it can or it will impact your UI badly.
2) Paint is a "frequent flyer" event - it gets triggered a lot: every single time the system needs to show the user a change.

Which means that your objects for display should not be created in the Paint event - they need to be ready when the Paint event is handled. Think about it: when you "drag" one of your squares, you need the paint event handled in order to show it moving! :laugh:

So start off by creating your squares in the container for your squares (probably your form, but I'd use a UserControl myself - don't worry about that, you'll come to them later).
Your squares need to be available inside the Paint event (so they can be drawn) and inside other events as well - MouseDown and MouseMove need them as well - otherwise they can't all work together to move and display your items.
So create your squares as part of the containing class - probably the form - and use them in the handlers. Look at adding the squares as part of the Form.Shown event and call Invalidate when you want to update the display.
Does that make sense?


这篇关于为类添加鼠标事件参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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