Winforms C#:滚动更改按钮的位置?

编程入门 行业动态 更新时间:2024-10-26 10:39:11
本文介绍了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#:滚动更改按钮的位置?

本文发布于:2023-10-08 08:56:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   位置   Winforms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!