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:
tableLayoutPanel = new TableLayoutPanel()
{
RowCount = 1,
ColumnCount = 1,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
Location = new Point(12, 12),
Dock = DockStyle.Fill,
AutoScroll = true,
};
this.Controls.Add(tableLayoutPanel);
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--;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Panel space = new Panel()
{
BackColor = SystemColors.ActiveCaption,
BorderStyle = BorderStyle.FixedSingle,
Margin = new Padding(0),
Size = new Size(45, 45)
};
space.MouseClick += new MouseEventHandler(clickOnSpace);
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
};
space.Controls.Add(info);
tableLayoutPanel.Controls.Add(space, j, i);
}
}
And the code that creates the backlight panel:
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 (tableLayoutPanel.GetControlFromPosition(c, r).Controls.Count == 1)
{
Panel highlight = new Panel()
{
Name = "highlight",
Size = new Size(30, 30),
BackColor = Color.Yellow,
Anchor = AnchorStyles.None
};
highlight.MouseDoubleClick += new MouseEventHandler(doubleClickOnSpace);
tableLayoutPanel.GetControlFromPosition(c, r).Controls.Add(highlight);
tableLayoutPanel.GetControlFromPosition(c, r).Controls[1].BringToFront();
}
}
}