Search code examples
c#winformsz-orderbringtofront

c# bringtofront() and senttoback() not working


I am new to c# and I'm trying to understand z-index concept. So far I have a simple form created using ConsoleApplication project in visual studio. There are 3 cs files. Here's the code:

In Algorithm.cs (inherited from 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");
}

}

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



}

In Main.cs:

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

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

The problem I'm having is that the test button is not visible. If i remove the bottom panel and add the test button directly on the form then it is visible. Why doesn't it appear on the bottom panel even after I used bringtofront() ?


Solution

  • Why doesn't it appear on the bottom panel even after I used bringtofront() ?

    Because you've placed it outside the visual boundary of the panel:

    test.Location = new Point(0, 800);
    

    That sets the position of the button to a horizontal offset of 0 and a vertical offset of 800. The bottom panel is only 140 pixels high, so 800 is well below the bottom of the visible area.

    It's not really clear what you meant to do. Given that the window width is 1600 pixels and 800 is half that, maybe you just transposed the X and Y coordinates and meant this instead:

    test.Location = new Point(800, 0);
    

    That would place the button in the middle of the panel, aligned to the top.

    Other than that, I can't imagine why if you wanted the button to be visible, you would hard-code a location for it that is not in a visible area.