Winforms C#: Scrolling changes location of my butt

2019-09-06 12:16发布

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

It does not work.

3条回答
Fickle 薄情
2楼-- · 2019-09-06 12:26

As scheien has suggested, set the location of the button where you want and set the Anchor property to desired values (Left, Top, Right, Bottom). This will fix the location of the button relative to the edges of the form selected.

If you mean you don't want the button to move even when the user scrolls and reads maybe some text, put the button in a panel and Dock the panel to the bottom of the form (or wherever you want it). You can also use a splitter control and place buttons in one section and content in the other.

查看更多
闹够了就滚
3楼-- · 2019-09-06 12:27

Set the Anchor property on the button to Top, Left . Then it is always the same length away from the top and left side. This is also a default setting.

Make sure the Dock property is set to None:

        button1.Dock = DockStyle.None;
        button1.Location = new System.Drawing.Point(50, 50);
        button1.Anchor = (AnchorStyles.Top | AnchorStyles.Left);

When you add the button to your form, it is then drawn 50 pixels from the top, and 50 pixels from the left, according to the control which holds it. In this case, the form:

this.Controls.Add(button1);

Are you sure the x and y variables aren't messing up the location?

查看更多
Animai°情兽
4楼-- · 2019-09-06 12:40

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:

First create form variable to hold the original Top value:

int oldButton1Top;

Then set it in the Load event:

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

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.

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

查看更多
登录 后发表回答