tecla
Erfahrenes Mitglied
Hallo zusammen
Ich möchte gern mit Klick auf ein Menu-Item ein neues Label erstellen, welches dann am Cursor hängt und mit Drop auf meinem TableLayoutPanel platziert werden kann.
Vorhandene Labels mit DragDrop rumschieben klappt einwandfrei.
Es gelingt mir jedoch nicht, ein neues Label zu erzeugen und rumzuschieben, ohne es vorher irgendwo platziert zu haben.
Ein bestehendes Label schiebe ich so herum:
Der DragDrop-Event, der das Label in das TableLayoutPanel einfügt:
Mei kläglicher Versuch, ein neues Label zu erstellen und per DragDrop zu platzieren (was nicht funktioniert, es passiert nämlich gar nichts):
Irgendwie scheint sich das neue Label vom DoDragDrop überhaupt nicht angesprochen zu fühlen....
Ich möchte gern mit Klick auf ein Menu-Item ein neues Label erstellen, welches dann am Cursor hängt und mit Drop auf meinem TableLayoutPanel platziert werden kann.
Vorhandene Labels mit DragDrop rumschieben klappt einwandfrei.
Es gelingt mir jedoch nicht, ein neues Label zu erzeugen und rumzuschieben, ohne es vorher irgendwo platziert zu haben.
Ein bestehendes Label schiebe ich so herum:
Code:
private void labelSendungstyp2_MouseDown(object sender, MouseEventArgs e)
{
try
{
if (e.Button == MouseButtons.Left)
{
this.tableLayoutPanel1.AllowDrop = true;
((Control)sender).DoDragDrop(sender, DragDropEffects.All);
}
}
catch (Exception ex)
{
}
}
Der DragDrop-Event, der das Label in das TableLayoutPanel einfügt:
Code:
private void tableLayoutPanel1_DragDrop(object sender, DragEventArgs e)
{
Point scrollPoint = tableLayoutPanel1.AutoScrollPosition;
try
{
Label label = ((Label)e.Data.GetData(typeof(System.Windows.Forms.Label)));
cellpos = tableLayoutPanel1.GetCellPosition(label);
if (tableLayoutPanel1.Controls.Count < tableLayoutPanel1.RowCount * tableLayoutPanel1.ColumnCount)
{
Point loc = this.tableLayoutPanel1.PointToClient(new Point(e.X, e.Y));
loc.Y = loc.Y - scrollPoint.Y;
int ColumnIndex = -1;
int RowIndex = -1;
int x = 0;
int y = 0;
while (ColumnIndex <= this.tableLayoutPanel1.ColumnCount)
{
if (loc.X < x)
{
break;
}
ColumnIndex++;
x += this.tableLayoutPanel1.GetColumnWidths()[ColumnIndex];
}
while (RowIndex <= this.tableLayoutPanel1.RowCount)
{
if (loc.Y < y)
{
break;
}
RowIndex++;
y += this.tableLayoutPanel1.GetRowHeights()[RowIndex];
}
this.tableLayoutPanel1.Controls.Add(label, ColumnIndex, RowIndex);
//tableLayoutPanel1.PerformLayout();
}
else
{
return;
}
}
catch (Exception ex)
{
}
}
Mei kläglicher Versuch, ein neues Label zu erstellen und per DragDrop zu platzieren (was nicht funktioniert, es passiert nämlich gar nichts):
Code:
private void newLabelToolStripMenuItem_Click(object sender, EventArgs e)
{
this.tableLayoutPanel1.AllowDrop = true;
Label newLabel = new Label();
newLabel.Text = "Hallo";
newLabel.DoDragDrop(newLabel, DragDropEffects.All);
}
Irgendwie scheint sich das neue Label vom DoDragDrop überhaupt nicht angesprochen zu fühlen....
