繁体   English   中英

C#事件未触发

[英]C# Event not firing

我正在尝试创建一个自定义可滚动面板,因为TableLayoutPanels滚动功能不是非常可自定义的。

我有一个自定义类,该类继承自Microsoft.Visualbasics.Powerpacks.RectangleShape。 此类用于创建滚动条对象。 它包含一个MouseDrag事件,该事件应该在滚动条上按下鼠标时触发,并在鼠标再次抬起时终止。

在从Forms.Panel继承的另一个自定义类中实例化此ScrollBar对象。

在主窗体方法中,将自定义面板实例化,并将MouseDrag事件添加到ScrollBar。 当我单击ScrollBar时,没有任何反应。 我什至使用内置的Click事件进行了测试,但仍然没有任何反应。 任何帮助将非常感激。

滚动条类:

    class ScrollBar : RectangleShape
{
    public event MouseEventHandler MouseDrag;
    private bool mouseHeld = false;
    public bool MouseHeld { get => mouseHeld; set => mouseHeld = value; }
    public ScrollBar()
    {
        InitializeObject();
    }
    public ScrollBar(int x, int y, int width, int height) : base(x, y, width, height)
    {
        InitializeObject();
    }
    private void InitializeObject()
    {
        this.MouseDown += new MouseEventHandler(mouseClickEvent);
    }
    public void mouseClickEvent(object sender, MouseEventArgs e)
    {

        MouseHeld = true;
        MouseDrag(this, null);

    }
}

自定义面板类:

class CustomPanel : Panel
{
    private ScrollBar verticalScrollBar;
    public ScrollBar VerticalScrollBar { get => verticalScrollBar; set => verticalScrollBar = value; }
    public CustomPanel()
    {
        PanelSetup();
    }
    public CustomPanel(Size _size)
    {
        this.Size = _size;
        PanelSetup();
    }
    private void PanelSetup()
    {
        //Panel setup
        this.BackColor = Color.White;
        this.Location = new Point(125, 125);
        this.BorderStyle = BorderStyle.FixedSingle;

        //Behind scrollbar graphic
        RectangleShape behindScrollGraphic = new RectangleShape();
        behindScrollGraphic.Width = 21;
        behindScrollGraphic.Height = this.Height;
        behindScrollGraphic.Location = new Point(this.Width - behindScrollGraphic.Width, 0);
        behindScrollGraphic.FillStyle = FillStyle.Solid;
        behindScrollGraphic.FillColor = SystemColors.Control;
        behindScrollGraphic.BorderColor = Color.Transparent;

        //adding behind scroll bar to panel
        ShapeContainer shapeContainer = new ShapeContainer();
        shapeContainer.Shapes.Add(behindScrollGraphic);
        this.Controls.Add(shapeContainer);

    }
    public virtual void AddVerticalScrollBar()
    {
        ShapeContainer rectangleShapeContainer = new ShapeContainer();
        rectangleShapeContainer.Shapes.Add(VerticalScrollBar);
        this.Controls.Add(rectangleShapeContainer);
    }
    public virtual void CreateScrollBar(int _barWidth, int _barHeight)
    {
        int barWidth = _barWidth;
        int barHeight = _barHeight;
        VerticalScrollBar = new ScrollBar(this.Width - barWidth - 7, 5, 12, 30);
        VerticalScrollBar.FillStyle = FillStyle.Solid;
        VerticalScrollBar.FillColor = SystemColors.ControlDark;
        VerticalScrollBar.BorderColor = Color.Transparent;
    }

}

主要表格类别:

public partial class Form1 : Form
{
    private CustomPanel panel;
    public Form1()
    {
        InitializeComponent();
        CheckForIllegalCrossThreadCalls = false;

        //Form setup
        this.Size = new Size(500, 500);
        this.BackColor = Color.White;

        //Panel setup
        panel = new CustomPanel(new Size(250, 250));
        panel.CreateScrollBar(10, panel.Height - 2);
        panel.AddVerticalScrollBar();

        //Scroll Bar
        panel.VerticalScrollBar.MouseDrag += new MouseEventHandler(mouseHeldMethod);

        //Add panel to form
        this.Controls.Add(panel);
    }
    private void mouseHeldMethod(object sender, MouseEventArgs e)
    {
        Console.WriteLine("test");
        while (panel.VerticalScrollBar.MouseHeld)
        {
            Console.WriteLine("Held");
        }
    }

}

在任何人浪费时间之前先解决问题,该控件被另一个控件阻塞,即使其他控件明显位于其后,该事件调用也没有错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM