Nel seguente codice, solo il secondo metodo funziona per me (.NET 4.0). FormStartPosition.CenterParent
non centra il modulo figlio sul relativo genitore. Perché?FormStartPosition.CenterParent non funziona
Fonte: this SO question
using System;
using System.Drawing;
using System.Windows.Forms;
class Program
{
private static Form f1;
public static void Main()
{
f1 = new Form() { Width = 640, Height = 480 };
f1.MouseClick += f1_MouseClick;
Application.Run(f1);
}
static void f1_MouseClick(object sender, MouseEventArgs e)
{
Form f2 = new Form() { Width = 400, Height = 300 };
switch (e.Button)
{
case MouseButtons.Left:
{
// 1st method
f2.StartPosition = FormStartPosition.CenterParent;
break;
}
case MouseButtons.Right:
{
// 2nd method
f2.StartPosition = FormStartPosition.Manual;
f2.Location = new Point(
f1.Location.X + (f1.Width - f2.Width)/2,
f1.Location.Y + (f1.Height - f2.Height)/2
);
break;
}
}
f2.Show(f1);
}
}
esso non funziona anche se parli di chi è il suo genitore .. – nawfal
Come posso fare questo? 'f2.Parent = f1;' non può essere compilato. Si prega di consultare la domanda SO di cui sopra, c'è un esempio di codice in cui il genitore non è impostato in modo esplicito e FormStartPosition.CenterParent funziona. – kol
Bel tentativo, ma questa non è un'applicazione MDI :) Basta guardare il codice sopra: questo è l'intero programma, ed è chiaramente non MDI; e chiamo Show con f1; e questo è .NET 4. – kol