我是 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() 之后它也不会出现在底部面板上?