1

使用 C#.NET 4.5

  • 我试图进入时打开用户控件changevScrollbar 属性,在离开控件 时关闭

  • 我使用Mouse_Enterand Mouse_Leave但是当离开另一个(确实显示)时,滚动条消失了control or the scroll bar

问题: 如何检查鼠标是否在用户控制区域内?

问题2:如何禁用用户控件底部的滚动条?(水平滚动条)

如果您需要更多信息或者我不清楚某处,请告诉我。任何帮助将不胜感激,在此先感谢!

编辑:这是用户控件的代码:

public partial class EnemyStats : UserControl
{
    public EnemyStats()
    {
        InitializeComponent();
        label1.Left = (this.Width / 2) - (label1.Width / 2);
        hpBar1.Width = this.Width - 8;


        // Here i add the event that shows the scrollbar to all controls;
        foreach (Control con in this.Controls)
        {
            con.MouseEnter += new EventHandler(EnemyStats_MouseEnter);
        }
    }

    public double enemyMaxHP
    {
        get
        {
            return hpBar1.maxValue;
        }
        set
        {
            hpBar1.maxValue = value;
        }
    }

    public double enemyHP
    {
        get
        {
            return hpBar1.Value;
        }
        set
        {
            hpBar1.Value = value;
        }
    }

    private void EnemyStats_SizeChanged(object sender, EventArgs e)
    {
        if (this.Width < label1.Width) this.Width = label1.Width;

        label1.Left = (this.Width / 2) - (label1.Width / 2);

        hpBar1.Width = this.Width - 8;
    }

    private void EnemyStats_MouseEnter(object sender, EventArgs e)
    {
        // This scrollbar was added by dragging it from the toolbox onto the user control in the designer
        vScrollbar1.Visible = true;
    }

    private void EnemyStats_MouseLeave(object sender, EventArgs e)
    {
        // This scrollbar was added by dragging it from the toolbox onto the user control in the designer
        vScrollbar1.Visible = false;
    }

}

但是滚动条不起作用:

public void randomMethodInUserControl()
{
    this.ScrollBars = ScrollBars.Vertical;
}
4

1 回答 1

1

这是我发现的(假设您使用的是文本框)

  • 将 Textbox1 设置为多行(并且设计器视图中没有滚动条并预先填充了文本(足够长以使用滚动条))

    private void textBox1_MouseEnter(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.Vertical;
    }
    
    private void textBox1_MouseLeave(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.None;
    }
    
  • 我是怎么发现的,虽然这有效,但当我没有完成Scrollbar Side, it stayed on(类似于你检查其他控件)时。我给你的解决方案(虽然这是一种解决方法)是给自己一个(背景颜色:透明)label并用它来包裹你的文本框。(让它在设计器中显示在您的文本框周围有 2-5px 的边距,并让文本框处理鼠标输入和鼠标离开事件。

    private void label1_MouseLeave(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.None;
    }
    
    private void label1_MouseEnter(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.Vertical;
    }
    
于 2013-09-24T14:55:01.507 回答