Winforms C#:滚动更改按钮的位置? [英] Winforms C#: Scrolling changes location of my buttons?

查看:122
本文介绍了Winforms C#:滚动更改按钮的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Winforms上的一个项目,最近我意识到,如果我向下滚动然后导致另一个按钮出现,则该按钮将出现在与我预期不同的位置.我需要知道如何解决这个问题,我的意思是如何做到这一点,以使按钮出现在整个表单的某个位置.

I'm working on a project on winforms, and I recently realized that if I scroll down then cause another button to appear, it appears in a different location than I meant. I need to know how to fix that, I mean how to make it so that the button appears in a location on the entire form.

我有两个按钮,一个使另一个出现 这是一个代码示例:

I have two buttons, one makes the other appear Here is an example of a code:

public Form1()
{
    InitializeComponent();
    this.AutoScroll = true;
    this.Controls.Remove(button2);
}

private void button1_Click(object sender, EventArgs e)
{
    this.button2.Dock = DockStyle.None;
    this.button2.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
    this.button2.Location = new System.Drawing.Point(110, 96);
    this.Controls.Add(button2);
}

private void button2_Click(object sender, EventArgs e)
{
    this.Controls.Remove(button2);
}

它不起作用.

推荐答案

请使自己更加清晰:您写道滚动表格,对不对?

Please make yourself clearer: You wrote that you scroll the form, right?

正常行为是每个控件,包括所有按钮,都会随之移动.

Normal behaviour is that every control, including all buttons, will move with it.

是您的问题还是在发生其他运动"?

Is that your problem or is there another 'movement' going on?

由于先前的回答没有帮助,因此这里是固定按钮(有些异国情调)的解决方案:

Since the previous responses didn't help here is a solution for the (somewhat exotic) case of a fixed button:

首先创建一个表单变量以保留原始的Top值:

First create form variable to hold the original Top value:

int oldButton1Top;

然后在Load事件中进行设置:

Then set it in the Load event:

public Form1()
{
   InitializeComponent();
   //..
   oldButton1Top = button1.Top;
   //
}

然后对表单的Scroll事件进行编码,如下所示:

And then code the Scroll event of the form like this:

private void Form1_Scroll(object sender, ScrollEventArgs e)
{
   button1.Top = oldButton1Top + e.NewValue - e.NewValue;
}

这应该将您的按钮固定在其原始位置.

This should keep your button fixed at its original position.

顺便说一句:在一个答案中,系统告诉您在相反边缘将锚点设置为true.当窗口调整大小并且肯定不是您想要的值时,这将使按钮增长缩小

BTW: In one answer you were told to set the anchor to true on opposing edges. This is will make the button grow or shrink when the windows resizes and is ceratinly not what you want

这篇关于Winforms C#:滚动更改按钮的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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