c# bringtofront() 和 senttoback() 不工作
c# bringtofront() and senttoback() not working
我是 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() 之后它也没有出现在底部面板上?
Why doesn't it appear on the bottom panel even after I used bringtofront() ?
因为您已将其放置在面板的视觉边界之外:
test.Location = new Point(0, 800);
将按钮的位置设置为水平偏移 0
和垂直偏移 800
。 bottom
面板只有 140 像素高,因此 800
比可见区域底部 好 。
不太清楚你的意思。假设 window 宽度为 1600 像素,而 800 是它的一半,也许你只是调换了 X 和 Y 坐标,意思是:
test.Location = new Point(800, 0);
这会将按钮放置在面板的中间,与顶部对齐。
除此之外,我无法想象为什么如果你想让按钮可见,你会为它硬编码一个不在可见区域的位置。
我是 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() 之后它也没有出现在底部面板上?
Why doesn't it appear on the bottom panel even after I used bringtofront() ?
因为您已将其放置在面板的视觉边界之外:
test.Location = new Point(0, 800);
将按钮的位置设置为水平偏移 0
和垂直偏移 800
。 bottom
面板只有 140 像素高,因此 800
比可见区域底部 好 。
不太清楚你的意思。假设 window 宽度为 1600 像素,而 800 是它的一半,也许你只是调换了 X 和 Y 坐标,意思是:
test.Location = new Point(800, 0);
这会将按钮放置在面板的中间,与顶部对齐。
除此之外,我无法想象为什么如果你想让按钮可见,你会为它硬编码一个不在可见区域的位置。