2016-09-18 5 views
0

저는 C#에 익숙하지 않으며 Z- 인덱스 개념을 이해하려고합니다. 지금까지 Visual Studio ConsoleApplication 프로젝트를 사용하여 만든 간단한 양식이 있습니다. 3 개의 CS 파일이 있습니다.# bringtofront() 및 senttoback()이 작동하지 않습니다.

Algorithm.cs에서 (UI.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"); 
} 

} 

: Main.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(); 
} 



} 

:

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

    //Application.Run(a); 
    Application.Run(al); 
} 
} 
여기에 코드입니다

내가 겪고있는 문제는 테스트 버튼이 보이지 않는다는 것입니다. 아래쪽 패널을 제거하고 양식에 직접 테스트 버튼을 추가하면 표시됩니다. bringtofront()를 사용한 후에도 왜 맨 아래 패널에 나타나지 않습니까?

+0

[ask]를 읽고 문제를 재현하는 [mcve]를 제공해주십시오. 표시하는 변수가 0x0 패널을 만들거나 컨테이너의 경계 외부에있는 경우 (즉, 보이지 않는 경우) – CodeCaster

+0

테스트 버튼은 어디에 있습니까? 나는 버튼 이름 테스트 나 그러한 버튼을 언급하는 주석을 보지 못했다. – mok

+0

@mok : "a"버튼, a.Text = "test". 혼란을 가져 주어서 죄송합니다. – Fierid

답변

0

bringtofront()를 사용한 후에도 왜 맨 아래 패널에 나타나지 않습니까? 하면 패널의 시각적 경계 외부로 배치 했으므로

:

0 오프셋 800 수평 및 수직 오프셋의 버튼의 위치를 ​​설정
test.Location = new Point(0, 800); 

. bottom 패널의 높이는 140 픽셀이므로 800입니다. 표시 영역 아래쪽 아래쪽에는입니다.

당신이하고자하는 것이 분명하지 않습니다.

이 패널의 중앙에있는 버튼을 배치 할
test.Location = new Point(800, 0); 

의 정렬 : 윈도우 폭이 1600 개 픽셀과 800 점을 감안하면 어쩌면 그냥 X를 전치와 Y 좌표 대신이 의미 절반이다 상단.

그 외에도 단추를 표시하려면 왜 보이는 영역에 있지 않은 위치를 하드 코딩해야하는지 생각할 수 없습니다.

+0

고맙습니다. 문제가 해결되었습니다. 그것은 내게 너무 부주의했다. – Fierid