this.Invoke(new MethodInvoker(delegate { this.owner = owner; }));
public partial class MemoryBox : Form { // Internal values MemoryBoxResult lastResult = MemoryBoxResult.Cancel; MemoryBoxResult result = MemoryBoxResult.Cancel; Control owner; // Enums // Results /// <summary> /// The default constructor for MemoryBox. /// </summary> public MemoryBox(Control owner) { InitializeComponent(); this.owner = owner; } /// <summary> /// Call this function instead of ShowDialog, to check for remembered /// result. /// </summary> /// <returns></returns> public MemoryBoxResult ShowMemoryDialog() { result = MemoryBoxResult.Cancel; if (lastResult == MemoryBoxResult.NoToAll) { result = MemoryBoxResult.No; } else if (lastResult == MemoryBoxResult.YesToAll) { result = MemoryBoxResult.Yes; } else { this.PostInParentContext(delegate { this.ShowInTaskbar = false; this.ShowDialog(this.owner); }); } return result; } public String LabelText { get; set; } public MemoryBoxResult ShowMemoryDialog(String label, string title) { this.PostInParentContext(delegate { this.Text = title; }); LabelText = label; return ShowMemoryDialog(); } private void PostInParentContext(MethodInvoker del) { if (this.owner.InvokeRequired) { this.owner.Invoke(del); } else { del.Invoke(); } } }
public partial class Crtr : Form { private Thread threadTraitement; [...] public Crtr() { [...] c_Thread = new C_Thread(this); [...] } [...] }
public class C_Thread { [...] private MemoryBox memoryBox; private Control owner; [...] public C_Thread(Control owner) { this.owner = owner; } [...] public void ma fonction() { [...] memoryBox = new MemoryBox(owner); [...] } [...] }
public partial class MemoryBox : Form { [...] private Control owner; [...] public MemoryBox(Control owner) { InitializeComponent(); this.owner = owner; } [...] public MemoryBoxResult ShowMemoryDialog() { [...] this.PostInParentContext(delegate { this.ShowInTaskbar = false; this.ShowDialog(this.owner); }); [...] } [...] private void PostInParentContext(MethodInvoker del) { if (this.owner.InvokeRequired) { this.owner.Invoke(del); } else { del.Invoke(); } } [...] }
Impossible d'appeler Invoke ou BeginInvoke sur un contrôle tant que le handle de fenêtre n'a pas été créé.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionthis.ShowDialog(this.owner);
public partial class MemoryBox : Form { // Internal values MemoryBoxResult lastResult = MemoryBoxResult.Cancel; MemoryBoxResult result = MemoryBoxResult.Cancel; IWin32Window owner; // Enums // Results /// <summary> /// The default constructor for MemoryBox. /// </summary> public MemoryBox(IWin32Window owner) { InitializeComponent(); this.Invoke(new MethodInvoker(delegate { this.owner = owner; })); }
#region Public Methods /// <summary> /// Call this function instead of ShowDialog, to check for remembered /// result. /// </summary> /// <returns></returns> public MemoryBoxResult ShowMemoryDialog() { result = MemoryBoxResult.Cancel; if (lastResult == MemoryBoxResult.NoToAll) { result = MemoryBoxResult.No; } else if (lastResult == MemoryBoxResult.YesToAll) { result = MemoryBoxResult.Yes; } else { this.ShowInTaskbar = false; this.ShowDialog(this.owner); } return result; } public MemoryBoxResult ShowMemoryDialog(String label, string title) { this.Text = title; LabelText = label; return ShowMemoryDialog(); } #endregion
private C_Thread c_Thread; private Thread threadTraitement; [...] c_Thread = new C_Thread(this); [...] this.threadTraitement = new Thread(new ThreadStart(this.c_Thread.lancementTraitement)); this.threadTraitement.Start();
private MemoryBox memoryBox; private IWin32Window owner; [...] public C_Thread(IWin32Window owner) { this.owner = owner; } [...] memoryBox = new MemoryBox(owner); [...] MemoryBoxResult result memoryBox.ShowMemoryDialog("le fichier " + nomFichier + " éxiste déjà!> écraser? ", "Ecrasement"); [...]