C#事件未触发
我正在尝试创建一个自定义的可滚动面板,因为TableLayoutPanels滚动功能不太可自定义 我有一个从Microsoft.Visualbasics.Powerpacks.RectangleShape继承的自定义类。此类用于创建滚动条对象。它包含一个MouseDrag事件,该事件应该在鼠标按下滚动条时触发,并在鼠标返回时终止 此滚动条对象在从Forms.Panel继承的另一个自定义类中实例化 在main form方法中,自定义面板被实例化,MouseDrag事件被添加到滚动条中。当我点击滚动条时,什么也没发生。我甚至用内置的Click事件进行了测试,但还是没有发生任何事情。任何帮助都将不胜感激 滚动条类别:C#事件未触发,c#,forms,events,C#,Forms,Events,我正在尝试创建一个自定义的可滚动面板,因为TableLayoutPanels滚动功能不太可自定义 我有一个从Microsoft.Visualbasics.Powerpacks.RectangleShape继承的自定义类。此类用于创建滚动条对象。它包含一个MouseDrag事件,该事件应该在鼠标按下滚动条时触发,并在鼠标返回时终止 此滚动条对象在从Forms.Panel继承的另一个自定义类中实例化 在main form方法中,自定义面板被实例化,MouseDrag事件被添加到滚动条中。当我点击滚动
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");
}
}
}
在任何人浪费时间之前解决了问题,控件被另一个控件阻塞,即使另一个控件明显在它后面,事件调用也没有问题。在任何人浪费时间之前解决了问题,该控件被另一个控件阻止,即使很明显另一个控件在它后面,事件调用没有任何问题