Fixed: cross threading for panelbook

This commit is contained in:
Ray 2020-06-18 12:37:06 +01:00
parent 3782bbfc44
commit b6f03b6f59
2 changed files with 55 additions and 46 deletions

View File

@ -1,36 +0,0 @@
namespace RyzStudio.Windows.Forms
{
partial class PanelBook
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -7,7 +7,7 @@
using System.Windows.Forms; using System.Windows.Forms;
[ToolboxItem(true)] [ToolboxItem(true)]
public partial class PanelBook : UserControl public class PanelBook : UserControl
{ {
public class PanelCollection : CollectionBase public class PanelCollection : CollectionBase
{ {
@ -66,6 +66,7 @@
} }
private System.ComponentModel.IContainer components = null;
protected PanelCollection panelCollection = null; protected PanelCollection panelCollection = null;
@ -76,6 +77,21 @@
panelCollection = new PanelCollection(this); panelCollection = new PanelCollection(this);
} }
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
protected void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Panel ActivePanel { get; protected set; } = null; public Panel ActivePanel { get; protected set; } = null;
@ -118,7 +134,7 @@
if (value > (panelCollection.Count - 1)) return; if (value > (panelCollection.Count - 1)) return;
if (value == this.SelectedIndex) return; if (value == this.SelectedIndex) return;
ActivatePanel(value); ActivatePage(value);
} }
} }
@ -129,7 +145,7 @@
{ {
if (panelCollection.Count <= 0) if (panelCollection.Count <= 0)
{ {
ActivatePanel(-1); ActivatePage(-1);
return; return;
} }
@ -138,11 +154,11 @@
throw new ArgumentOutOfRangeException("PageIndex", value, "The page index must be between 0 and " + Convert.ToString(panelCollection.Count - 1)); throw new ArgumentOutOfRangeException("PageIndex", value, "The page index must be between 0 and " + Convert.ToString(panelCollection.Count - 1));
} }
ActivatePanel(value); ActivatePage(value);
} }
} }
protected internal void ActivatePanel(int index) protected internal void ActivatePage(int index)
{ {
if ((panelCollection.Count == 0) && (index >= panelCollection.Count) && (index <= 0)) if ((panelCollection.Count == 0) && (index >= panelCollection.Count) && (index <= 0))
{ {
@ -158,7 +174,16 @@
{ {
if (this.ActivePanel != null) if (this.ActivePanel != null)
{ {
if (this.ActivePanel.InvokeRequired)
{
this.ActivePanel.Invoke(new MethodInvoker(() => {
this.ActivePanel.Visible = false; this.ActivePanel.Visible = false;
}));
}
else
{
this.ActivePanel.Visible = false;
}
} }
this.ActivePanel = page; this.ActivePanel = page;
@ -170,14 +195,34 @@
this.Container.Add(this.ActivePanel); this.Container.Add(this.ActivePanel);
} }
if (this.ActivePanel.InvokeRequired)
{
this.ActivePanel.Invoke(new MethodInvoker(() => {
this.ActivePanel.Dock = DockStyle.Fill;
this.ActivePanel.Visible = true;
this.ActivePanel.BringToFront();
}));
}
else
{
this.ActivePanel.Dock = DockStyle.Fill; this.ActivePanel.Dock = DockStyle.Fill;
this.ActivePanel.Visible = true; this.ActivePanel.Visible = true;
this.ActivePanel.BringToFront(); this.ActivePanel.BringToFront();
} }
}
if (this.ActivePanel != null) if (this.ActivePanel != null)
{ {
if (this.ActivePanel.InvokeRequired)
{
this.ActivePanel.Invoke(new MethodInvoker(() => {
this.ActivePanel.Invalidate(); this.ActivePanel.Invalidate();
}));
}
else
{
this.ActivePanel.Invalidate();
}
} }
else else
{ {