Hallo zusammen,
ich komme gerade nicht so recht weiter und hoffe hier auf Hilfe.
Ich erstelle zur Laufzeit über eine SQL Abfrage n TableLayoutPanels und x Buttons.
Diese möchte ich jetzt gerne so verheiraten, dass auf jedem Panel 20 Buttons sind und dann auf dem nächsten Panel die nächsten 20, usw.
Kann mir da jemand helfen?
public partial class Main : Form
{
private SQLBase sql = new SQLBase();
public Main()
{
sql.Connect();
double numOfTours = sql.NumOfTours();
sql.Close();
numOfTours /= 20;
int numOfPanel = Convert.ToInt16(Math.Ceiling(numOfTours));
CreatePanel(numOfPanel);
CreateButtons();
InitializeComponent();
}
private void CreatePanel(int numOfPanel)
{
for (int i = 0; i < numOfPanel; i++)
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();
tableLayoutPanel.Location = new System.Drawing.Point(0, 0);
tableLayoutPanel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
tableLayoutPanel.Padding = new System.Windows.Forms.Padding(38, 5, 38, 5);
tableLayoutPanel.TabIndex = 10 + i;
tableLayoutPanel.ColumnCount = 5;
tableLayoutPanel.Dock = DockStyle.Fill;
tableLayoutPanel.RowCount = 5;
tableLayoutPanel.Name = "tableLayoutPanel" + i.ToString();
tableLayoutPanel.Visible = true;
Controls.Add(tableLayoutPanel);
}
}
private void CreateButtons()
{
sql.Connect();
SqlDataReader touren = sql.ReadTour();
while (touren.Read())
{
Button btn = new Button();
btn.Name = "btn" + touren["ID"].ToString();
btn.Text = touren["TOUR"].ToString();
Console.WriteLine("btn.Name " + btn.Name + " btn.Text " + btn.Text);
}
sql.Close();
}
}
Liebe Grüße Michael
Naja, les Dir mal Deinen Beitrag durch.
Du hast zwar geschrieben was Du tun willst, aber nicht wo das Problem ist, wo Du nicht weiter kommst.
Was ist denn die Frage an die Helfer? Oder erwartest Du nun, dass wir Dir den Code vervollständigen?
Du hast nur Code gepostet - ohne eine inhaltliche Frage.
[Hinweis] Wie poste ich richtig? Punkt 5
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Hallo Abt,
meine Frage ist, wie ich die Buttons nun auf die Panels bekomme. Pro Panel 20 Stück.
Klar , sicher über eine Schleife aber ich sehe da gerade ich den Wald vor lauter Bäumen nicht.
LG Michael
Du könntest über alle Controls laufen und prüfen, ob es sich um ein TableLayoutPanel handelt und wenn ja, welches.
Dafür musst Du die Panels über irgendeinen Parameter unterscheidbar machen (Tag?).
Oder Du sammelst erst die Panels in einer Liste und fügst die Buttons hinzu bevor Du die Panels in "Controls" packst.
Mittels
tableLayoutPanel.Controls.Add(button, column, row);
Edit:
@Gimmick: Das ist doch viel zu umständlich.
Einfach in einer doppelten Schleife jeweils ein TableLayoutPanel
mit je 20 Buttons erstellen (bzw. beim letzten entsprechend weniger).
Hi,
an eine bzw wohl eher mehrere Schleifen hatte ich ja auch schon gedacht nur die Reihenfolge ist mir unklar.
Ich muss ja auch registrieren bei welchem Button ich gerade bin und das ab dem 20sten auf das 2te Panel von dort an gegangen wird.
Columns und Rows kann ich dann ja einfach hochzählen.
Naja; das sind ja im Endeffekt nur ineinander verschachtelte Schleifen:
Du hast ja irgendwo eine Collection von allen Buttons, die Du hinzufügen willst.
Diese Collection teilst in 20er Chunks auf und iterierst zuerst über die 20er Chunks und dann über die darin enthaltene Buttons.
Im Endeffekt nur einer Verkettung von quasi Basis C# Befehlen 😉
- performance is a feature -
Microsoft MVP - @Website - @AzureStuttgart - github.com/BenjaminAbt - Sustainable Code
Erledigt 😉
public Main()
{
sql.Connect();
double numOfTours = sql.NumOfTours();
sql.Close();
numOfTours /= 20;
int numOfPanel = Convert.ToInt16(Math.Ceiling(numOfTours));
CreatePanel(numOfPanel);
CreateButtons();
AddToPanel(Controls);
InitializeComponent();
}
private void CreatePanel(int numOfPanel)
{
for (int i = 0; i < numOfPanel; i++)
{
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel
{
Location = new System.Drawing.Point(0, 24),
Padding = new System.Windows.Forms.Padding(25, 3, 25, 3),
TabIndex = 10 + i,
ColumnCount = 5,
RowCount = 4,
Dock = DockStyle.Fill,
Name = "tableLayoutPanel" + i.ToString(),
BackColor = Color.Gold,
Size = new System.Drawing.Size(1238, 890),
Visible = true
};
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));
tableLayoutPanel.Anchor = (((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
Controls.Add(tableLayoutPanel);
}
}
private void CreateButtons()
{
sql.Connect();
SqlDataReader touren = sql.ReadTour();
while (touren.Read())
{
Button btn = new Button
{
Name = "btn" + touren["ID"].ToString(),
Text = touren["TOUR"].ToString(),
Dock = DockStyle.Fill,
BackColor = Color.Green,
};
Controls.Add(btn);
}
sql.Close();
}
private void AddToPanel(Control.ControlCollection controls)
{
List<Button> btn = new List<Button>();
List<TableLayoutPanel> panel = new List<TableLayoutPanel>();
for (int i = 0; i < controls.Count; i++)
{
if (controls[i] is Button)
{
btn.Add((Button)controls[i]);
}
if (controls[i] is TableLayoutPanel)
{
panel.Add((TableLayoutPanel)controls[i]);
}
}
var btnlst = SplitList(btn, 20);
int pcount = 0;
int btnlstcnt = 0;
while (pcount < panel.Count)
{
Console.WriteLine(panel[pcount].Name);
while (btnlstcnt < btnlst.Count)
{
foreach (Button button in btnlst[btnlstcnt]) {
Console.WriteLine(button.Name);
panel[pcount].Controls.Add(button);
}
pcount++;
btnlstcnt++;
}
}
}
public static List<List<T>> SplitList<T>(List<T> list, int chunkSize)
{
var returnList = new List<List<T>>();
var index = 0;
while (index < list.Count)
{
var count = list.Count - index > chunkSize ? chunkSize : list.Count - index;
returnList.Add(list.GetRange(index, count));
index += chunkSize;
}
return returnList;
}