Benutzer mit den meisten Antworten
Zeile und Spalte von tabelLyoutPanel einfärben

Frage
-
Hallo Leute, folgendes Abbild zeigt ein tabelLyoutPanel mit 25 Zeilen und 20 Spalten. Meine Frage:
Wie lautet die dazugehörige Property um
- eine einzelne Zeile
- eine einzelne Spalte
- ein einzelnes Kästchen
einzufärben ?
Folgender Code sollte meine Intention eigentlich bewerkstelligen. tut es aber nüscht. Ich bekomme nur zwei gekreuzte rote Striche im Panel zu sehen. was mache ich falsch?
public partial class Form1 : Form { private const int elements = 10; private int[] toene = new int[elements]; Color[,] bgColors = new Color[2, 2] { { SystemColors.Control, SystemColors.Control }, { SystemColors.Control, SystemColors.Control } . . private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = ""; bgColors[1, 1] = Color.Blue; tableLayoutPanel1.Refresh(); . . private void TableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) { /*if ((e.Column + e.Row) % 2 == 1) e.Graphics.FillRectangle(Brushes.Black, e.CellBounds); else e.Graphics.FillRectangle(Brushes.White, e.CellBounds); */ using (var b = new SolidBrush(bgColors[e.Column, e.Row])) { e.Graphics.FillRectangle(b, e.CellBounds); } }
- Bearbeitet tklustig Montag, 7. Mai 2018 17:55
Antworten
-
In meinem Beispiel habe ich das Array lediglich für eine Tabelle von 3x3 erstellt. Ich habe gerade gesehen, das du ja 20x25 brauchst. Da müsstest du das ganze ein "wenig" aufpumpen.
Stellt sich mir allerdings gleich die Frage, ob das dann noch die beste Methode ist... Das wird das eine relativ große Initialisierung. Mal schauen, ob das kürzer geht...
So:
Color[,] bgColors = new Color[20, 25]; // in Form-Load for (int i = 0; i <= bgColors.GetUpperBound(0); i++) { for (int x = 0; x <= bgColors.GetUpperBound(1); x++) { bgColors[i, x] = SystemColors.Control; } }
Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP
- Bearbeitet Stefan Krömer Montag, 7. Mai 2018 19:19
- Als Antwort markiert tklustig Dienstag, 8. Mai 2018 06:24
Alle Antworten
-
Hi, so wie du es schon geschrieben hast oder so:
private void button5_Click(object sender, EventArgs e) { // column ,row bgColors[0, 1] = Color.Red; bgColors[1, 1] = Color.Blue; bgColors[2, 0] = Color.Black; tableLayoutPanel1.Refresh(); } Color[,] bgColors = new Color[3, 3] { { SystemColors.Control, SystemColors.Control, SystemColors.Control }, { SystemColors.Control, SystemColors.Control, SystemColors.Control }, { SystemColors.Control, SystemColors.Control, SystemColors.Control } }; private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) { using (var b = new SolidBrush(bgColors[e.Column, e.Row])) { e.Graphics.FillRectangle(b, e.CellBounds); } }
...als Beispiel
Gruß
Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP
-
Das klappt ja eben nicht. Bekomme nur zwei rote Balken als Anzeige. Vielleicht habe ich das Panel irgendwie durch eine falsche Einstellung in den Properties des Explorers für diesen Code zerschossen?
Folgender Code:
using System; using System.Windows.Forms; using System.Drawing; using System.Threading; namespace Primsound { public partial class Form1 : Form { private const int elements = 10; private int[] toene = new int[elements]; Color[,] bgColors = new Color[3, 3] { { SystemColors.Control, SystemColors.Control, SystemColors.Control }, { SystemColors.Control, SystemColors.Control, SystemColors.Control }, { SystemColors.Control, SystemColors.Control, SystemColors.Control } }; //delegate void delAddText(RichTextBox rtb, string txt, Color col); public Form1() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = true; } private void play(int freq, int dauer) { PlaySound p = new PlaySound(); p.WindowsBeep(freq, dauer); } private void button1_Click(object sender, EventArgs e) { richTextBox1.Text = ""; bgColors[1, 1] = Color.Blue; bgColors[2, 2] = Color.Red; bgColors[3, 3] = Color.Violet; tableLayoutPanel1.Refresh(); Random Rnd = new Random(); // initialisiert die Zufallsklasse for (int i = 0; i < elements; i++) { //Frequenzbereich:70 - 1950 int zufallszahl = Rnd.Next(70, 1950); toene[i] = zufallszahl; } for (int i = 0; i < toene.Length; i++) { this.play(toene[i], trackBar1.Value); this.AddText(i, richTextBox1, toene[i].ToString(), Color.Red); Thread.Sleep(trackBar1.Value); } } private void trackBar1_Scroll(object sender, EventArgs e) { label2.Text = "Dauer in Milisekunden:" + trackBar1.Value.ToString(); } private void AddText(int i, RichTextBox rtb, string txt, Color col) { int pos = rtb.TextLength; if (i == toene.Length - 1) rtb.AppendText(txt); else rtb.AppendText(txt + ","); rtb.Select(pos, txt.Length); rtb.SelectionColor = col; rtb.Select(); } private void TableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) { /*if ((e.Column + e.Row) % 2 == 1) e.Graphics.FillRectangle(Brushes.Black, e.CellBounds); else e.Graphics.FillRectangle(Brushes.White, e.CellBounds); */ using (var b = new SolidBrush(bgColors[e.Column, e.Row])) { e.Graphics.FillRectangle(b, e.CellBounds); } } } }
liefert folgende Ausgabe:
- Bearbeitet tklustig Montag, 7. Mai 2018 18:15
-
In meinem Beispiel habe ich das Array lediglich für eine Tabelle von 3x3 erstellt. Ich habe gerade gesehen, das du ja 20x25 brauchst. Da müsstest du das ganze ein "wenig" aufpumpen.
Stellt sich mir allerdings gleich die Frage, ob das dann noch die beste Methode ist... Das wird das eine relativ große Initialisierung. Mal schauen, ob das kürzer geht...
So:
Color[,] bgColors = new Color[20, 25]; // in Form-Load for (int i = 0; i <= bgColors.GetUpperBound(0); i++) { for (int x = 0; x <= bgColors.GetUpperBound(1); x++) { bgColors[i, x] = SystemColors.Control; } }
Freiberufler im Bereich Softwareentwicklung Von der PLC und Robotik zu VB.NET & C#, vorrangig WPF und UWP
- Bearbeitet Stefan Krömer Montag, 7. Mai 2018 19:19
- Als Antwort markiert tklustig Dienstag, 8. Mai 2018 06:24
-
Ah sooo, dann klappt das Ganze also nur dann, wenn die Initialisierung des zweidimensionalen Arrays 20*25 mal erfolgt. Initialisiere ich nicht 500 mal, bekomme ich obige Ausgabe quittiert. Korrekt?
Ich initialisiere das zweidimensionale Array also im Konstruktor wie oben aufgezeigt und kann dann jedes einzelne Kästchen dynamisch, je nachdem, ob die Zahl eine Primzahl ist, oder nicht, farblich untermalen. Famos. Daß das Array genau so oft initialisiert werden muss, wie das Panel Kästchen hat, wurde hier nirgends aufgezeigt.
Jetzt funktioniert das Ganze tadellos. Vielen Dank!
- Bearbeitet tklustig Dienstag, 8. Mai 2018 06:24