Center the panel inside the panel, which is in the TableLayoutPanel, in C #

So basically I have a game board presented by TableLayoutPanel. Each TableLayoutPanel cell represents a space on the board and contains a panel. Each panel has a shortcut in it to display what is currently in this space (for example, a symbol or building), and its BackColor represents what landscape this space is (for example, Earth, Water, etc.). When a player tries to move a character, every possible space that this character can move will become “highlighted”. Then the player double-clicks one of the selected spaces to move the symbol there.

To highlight a space, I add a panel to an existing panel (one that already has a shortcut in it).

So, to remind, TableLayoutPanel -> Panel -> Label, Panel.

The main problem that I am facing is that I cannot get a “backlight-panel” in the center of my parent panel; it always has an upper left center. I want to see a part of the BackColor of the parent panel so that the player can decide where to go. Therefore, setting up Dock to Fill is not an option. The backlight panel is slightly smaller than the parent panel, thereby allowing you to see the edges of the BackColor parent panel. Yes, Anchor is set to None.

The second problem is that I can not change any color of the text labels. I tried to change it when initializing or more directly in a specific place using "tableLayoutPanel.GetControlFromPosition (16, 4) .Controls [0] .ForeColor = Color.White;" but nothing works.

Honestly, I'm not interested in super complex hack fixes. But if there is something simple or something that I missed, I would really appreciate input. Thank.

This is the code that creates the TableLayoutPanel, the panels inside this and the labels inside each panel:

// Create TableLayoutPanel to hold Panels
tableLayoutPanel = new TableLayoutPanel()
{
    RowCount = 1,
    ColumnCount = 1,
    AutoSize = true,
    AutoSizeMode = AutoSizeMode.GrowAndShrink,
    Location = new Point(12, 12),
    Dock = DockStyle.Fill,
    AutoScroll = true,  
};

// Add tableLayoutPanel to the form
this.Controls.Add(tableLayoutPanel);

// Reset and add rows/columns + styles
tableLayoutPanel.RowStyles.Clear();
tableLayoutPanel.ColumnStyles.Clear();

for (int i = 0; i < rows; i++)
{
    tableLayoutPanel.RowCount++;
    tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
}
tableLayoutPanel.RowCount--;

for (int j = 0; j < cols; j++)
{
    tableLayoutPanel.ColumnCount++;
    tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
}
tableLayoutPanel.ColumnCount--;

// Add Panels to TableLayoutPanel
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        // Create new Panel
        Panel space = new Panel()
        {
            BackColor = SystemColors.ActiveCaption,
            BorderStyle = BorderStyle.FixedSingle,
            Margin = new Padding(0),
            Size = new Size(45, 45)
        };

        space.MouseClick += new MouseEventHandler(clickOnSpace);

        // Create new Label
        Label info = new Label()
        {
            Size = new Size(93, 93),
            Text = "Info",
            Dock = DockStyle.Fill,
            TextAlign = ContentAlignment.MiddleCenter,
            Enabled = false,
            Font = new Font("Microsoft Sans Serif", 6),
            ForeColor = Color.White
        };

        // Add Label to Panel
        space.Controls.Add(info);

        // Add Panel to TableLayoutPanel
        tableLayoutPanel.Controls.Add(space, j, i);
    }
}

And the code that creates the backlight panel:

// Highlight potential positions using possibleMoves
private void Highlight(List<Tuple<int, int>> possibleMoves, int remaining)
{
    int r = 0;
    int c = 0;

    foreach (Tuple<int, int> pair in possibleMoves)
    {
        r = pair.Item1;
        c = pair.Item2;

        // If highlight-Panel doesn't already exist
        if (tableLayoutPanel.GetControlFromPosition(c, r).Controls.Count == 1)
        {
            // Create a Panel to highlight the space
            Panel highlight = new Panel()
            {
                Name = "highlight",
                Size = new Size(30, 30),
                BackColor = Color.Yellow,
                Anchor = AnchorStyles.None
            };

            highlight.MouseDoubleClick += new MouseEventHandler(doubleClickOnSpace);

            // Add highlight Panel to space Panel
            tableLayoutPanel.GetControlFromPosition(c, r).Controls.Add(highlight);
           // Bring highlight Panel to front
            tableLayoutPanel.GetControlFromPosition(c, r).Controls[1].BringToFront();
        }
    }
}
+4
source share
2 answers

, . , Panel Panel, , , . , , CustomLabel .

CustomLabel:

public class CustomLabel : Label
{
// Constructors

    // Default Constructor
    public CustomLabel() : base() { }

    public CustomLabel(bool drawBorder, int borderThickness, Color borderColor, Color textColor) : base()
    {
        if (drawBorder)
        {
            BorderThickness = borderThickness;
            BorderColor = borderColor;
        }

        Size = new Size(36, 36);
        Text = "Info";
        Anchor = (AnchorStyles.Left | AnchorStyles.Right);
        AutoSize = false;
        TextAlign = ContentAlignment.MiddleCenter;
        Enabled = false;
        Font = new Font("Microsoft Sans Serif", 6);
        ForeColor = TextColor;
        BorderStyle = BorderStyle.FixedSingle;
        Dock = DockStyle.Fill;

    }

    // Creates a border of specified thickness and color
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        if (BorderStyle == BorderStyle.FixedSingle)
        {
            int halfThickness = BorderThickness / 2;
            using (Pen p = new Pen(BorderColor, BorderThickness))
            {
                e.Graphics.DrawRectangle(p, new Rectangle(halfThickness,
                     halfThickness,
                     ClientSize.Width - BorderThickness, ClientSize.Height - BorderThickness));
            }
        }
    }

    public int BorderThickness { get; set; }
    public Color BorderColor { get; set; }
}

CustomLabel :

// Add Panels to TableLayoutPanel
for (int i = 0; i < rows; i++)
{
    for (int j = 0; j < cols; j++)
    {
        // Create new Panel
        Panel space = new Panel()
        {
            Size = new Size(45, 45),
            Dock = DockStyle.Fill,
            Margin = new Padding(0),
            ForeColor = Color.Red
        };

        space.MouseClick += new MouseEventHandler(MouseDownOnSpace);

        CustomLabel info = new CustomLabel(false, 0, Color.Empty, Color.Red);  // Create new CustomLabel
        space.Controls.Add(info);   // Add CustomLabel to Panel
        tlp.Controls.Add(space, j, i);      // Add Panel to TableLayoutPanel
    }
}

CustomLabel:

((CustomLabel)tlp.GetControlFromPosition(col, row).Controls[0]).BorderThickness = 6;

((CustomLabel)tlp.GetControlFromPosition(col, row).Controls[0]).BorderColor = Color.Yellow;

tlp.GetControlFromPosition(col, row).Controls[0].Text = tlp.GetControlFromPosition(col, row).Controls[0].Text;  // Transfer space information

tlp.GetControlFromPosition(col, row).Refresh();     // Refresh Panel to show changes

: ( , ForeColor - .)

enter image description here

0

... , Dock to Fill .

, Dock Fill !

. a TableLayoutPanel, 2 2 . Pannel, Panel, Label. , , Dock Fill!

Center panel in panel in TableLayoutPanel

, Red, Black White.

:

  • , a Panel :

    BackColor Red
    Margin , 0
    Padding , 5
    Dock Fill

  • , a Panel :

    BackColor Black
    Margin , 0
    Padding , 5
    Dock Fill

  • , a Label :

    BackColor , White
    Margin , 0
    Padding , 0
    Dock Fill

, BackColor of Label (, ) Transparent, BackColor Panel ( ).

Dock Fill Padding . , TableLayoutPanel, 1 1 , Anchor None. , TableLayoutPanel. :

+1

Source: https://habr.com/ru/post/1651419/


All Articles