当光标在用户控件内时,使滚动条(在用户控件上)可见

时间:2013-09-24 13:53:57

标签: c# winforms

使用C#.NET 4.5

  • 进入时,我正在尝试change 用户控件 vScrollbar属性 开启离开控件时,关闭

  • 我在另一个Mouse_Enter(确实显示)时使用了Mouse_Leavecontrol or the scroll bar但是,滚动条消失了。

问题: 如何检查鼠标是否在用户控制字段内?​​

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

  

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

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

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;
}

1 个答案:

答案 0 :(得分: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-5像素的边距,让文本框处理鼠标输入和鼠标离开事件。

    private void label1_MouseLeave(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.None;
    }
    
    private void label1_MouseEnter(object sender, EventArgs e)
    {
        textBox1.ScrollBars = ScrollBars.Vertical;
    }
    
相关问题