c#bringtofront()和senttoback()无法正常工作

时间:2016-09-18 00:31:00

标签: c# winforms z-order bringtofront

我是c#的新手,我正在尝试理解z-index概念。到目前为止,我在Visual Studio中使用ConsoleApplication项目创建了一个简单的表单。有3个cs文件。这是代码:

在Algorithm.cs中(继承自UI.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

public class Algorithm : UI
{
private Timer refresh = new Timer();


//Ball settings
private const int offset = 25;
private const int rate = 200;
private int ball_bounding_box_x_coord;
private int ball_bounding_box_y_coord;
private int x;
private int y;
private const int width = 50;
private const int height = 50;
Brush brush = new SolidBrush(Color.Blue);

public bool isStartClicked = false;



Button test = new Button();
public Algorithm()
{
    test.Text = "test";
    test.Location = new Point(0, 800);
    test.Size = new Size(100, 50);
    test.TabIndex = 0;

    bottom.Controls.Add(test);
    test.BringToFront();

    ball_bounding_box_x_coord = middle.Size.Width / 2 - width / 2;
    ball_bounding_box_y_coord = middle.Size.Height / 2 - height / 2;

    middle.Paint += new PaintEventHandler(Draw);
    start.Click += new EventHandler(Start);

}

private void Calculate()
{
    Graphics g = middle.CreateGraphics();
    //g.FillEllipse(brush, )        
}




private void Draw(object sender, PaintEventArgs e)
{
    e.Graphics.FillEllipse(brush, ball_bounding_box_x_coord , ball_bounding_box_y_coord , width, height);
}


public void Start(object sender, EventArgs e)
{
    MessageBox.Show("Button clicked");
}

}

在UI.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

public class UI : Form
{
//Window settings
private const int WINDOW_WIDTH = 1600;
private const int WINDOW_HEIGHT = 900;
private Panel top = new Panel();
public Panel middle = new Panel();
public Panel bottom = new Panel();
private Label title = new Label();

//Text box for degree input
private Label degree_input_label = new Label();
private TextBox degree_input = new TextBox();
public double input;

//Label to display x and y coordinates of the ball
public Label ball_x_coord = new Label();
public Label ball_y_coord = new Label();

//Buttons
public Button start = new Button();
private Button quit = new Button();


public UI()
{
    //total height of 3 areas != window_height????????????????
    //Setup window form
    this.Width = WINDOW_WIDTH;
    this.Height = WINDOW_HEIGHT;
    this.Text = "Project 2";


    //Add a badass title
    title.Text = "Designed by Me";
    title.AutoSize = true;
    title.Location = new Point(600, 20);
    title.Font = new Font(title.Font.Name, 24, FontStyle.Bold);
    title.BackColor = Color.Red;
    Controls.Add(title);

    top.Location = new Point(0, 0);
    top.Size = new Size(WINDOW_WIDTH, 80);
    top.BackColor = Color.Red;

    middle.Location = new Point(0, top.Location.Y + top.Size.Height);
    middle.Size = new Size(WINDOW_WIDTH, 680);
    middle.BackColor = Color.Cyan;




    bottom.Location = new Point(0, top.Location.Y + top.Size.Height + middle.Size.Height);
    bottom.Size = new Size(WINDOW_WIDTH, WINDOW_HEIGHT - top.Height - middle.Height);
    bottom.BackColor = Color.Green;

    degree_input_label.Text = "Enter a degree:";
    degree_input_label.Location = new Point(100, bottom.Location.Y + 20);
    degree_input_label.AutoSize = true;
    Controls.Add(degree_input_label);

    degree_input.Size = new Size(50, 50);
    degree_input.Location = new Point(200, bottom.Location.Y + 20);
    degree_input.Leave += new EventHandler(TextChange);
    degree_input.TabIndex = 2;
    Controls.Add(degree_input);

    ball_x_coord.Text = "Ball X Coord";
    ball_x_coord.Location = new Point(400, bottom.Location.Y + 20);
    ball_x_coord.AutoSize = true;
    Controls.Add(ball_x_coord);

    ball_y_coord.Text = "Ball y coord";
    ball_y_coord.Location = new Point(500, bottom.Location.Y + 20);
    ball_y_coord.AutoSize = true;
    Controls.Add(ball_y_coord);


    start.Text = "Start";
    start.Location = new Point(1100, bottom.Location.Y + 20);
    start.Size = new Size(100, 50);
    start.TabIndex = 1;
    Controls.Add(start);

    quit.Text = "Quit";
    quit.Location = new Point(1400, bottom.Location.Y + 20);
    quit.Size = new Size(100, 50);
    quit.Click += new EventHandler(Quit);
    Controls.Add(quit);


    //ADD BACKGROUND CONTROLS
    Controls.Add(top);
    Controls.Add(middle);
    Controls.Add(bottom);









}//end constructor


private void TextChange(object sender, EventArgs e)
{
    if(degree_input.TextLength <= 0)
    {
        degree_input.Text = "0";
        MessageBox.Show("Please enter a degree");
        degree_input.Focus();
    }else
    {
        input = double.Parse(degree_input.Text);
        //MessageBox.Show(input.ToString());
    }

}







void Quit(object sender, EventArgs e)
{
    Application.Exit();
}



}

在Main.cs中:

class Program
{
static void Main(string[] args)
{
    Algorithm al = new Algorithm();
    UI a = new UI();

    //Application.Run(a);
    Application.Run(al);
}
}

我遇到的问题是测试按钮不可见。如果我删除底部面板并直接在表单上添加测试按钮,则可见。即使我使用了bringtofront()之后,为什么它不出现在底部面板上?

1 个答案:

答案 0 :(得分:0)

  

即使我使用了bringtofront()后,为什么它不会显示在底部面板上?

因为您已将其放置在面板的可视边界之外:

0

将按钮的位置设置为800的水平偏移和bottom的垂直偏移。 800面板只有140像素高,因此test.Location = new Point(800, 0); 位于可见区域的底部。

目前还不清楚你打算做什么。鉴于窗口宽度为1600像素而800是其一半,您可能只是转换了X和Y坐标并改为:

startTime < yourTime AND yourTime < endTime

这会将按钮放在面板中间,与顶部对齐。

除此之外,我无法想象为什么如果你想让按钮可见,你会硬编码一个不在可见区域内的位置。