Skip to content

Commit

Permalink
WinControl: .net6.0, refactor ChildFormTag
Browse files Browse the repository at this point in the history
  • Loading branch information
2mik committed Nov 25, 2021
1 parent 858b4cc commit 15fba79
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Src/WinControl/GroupPanel/GroupPanel.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Mikhail Shiryaev</Authors>
<Company />
<PackageProjectUrl></PackageProjectUrl>
Expand Down
2 changes: 1 addition & 1 deletion Src/WinControl/GroupPanelSample/GroupPanelSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

Expand Down
40 changes: 27 additions & 13 deletions Src/WinControl/WinControl/ChildFormTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace WinControl
/// <para>Представляет объект, связанный с дочерней формой.</para>
/// </summary>
/// <remarks>
/// Author: Mikhail Shiryaev, 2010, 2018
/// Author: Mikhail Shiryaev, 2010, 2018, 2021
/// </remarks>
public class ChildFormTag
{
Expand Down Expand Up @@ -64,40 +64,42 @@ public bool Modified


/// <summary>
/// Raises the ModifiedChanged event.
/// Raises a ModifiedChanged event.
/// </summary>
protected void OnModifiedChanged(EventArgs e)
{
ModifiedChanged?.Invoke(this, e);
}

/// <summary>
/// Raises the ChildFormMessage event.
/// Raises a MessageFromChildForm event.
/// </summary>
protected void OnChildFormMessage(FormMessageEventArgs e)
protected void OnMessageFromChildForm(FormMessageEventArgs e)
{
MessageFromChildForm?.Invoke(this, e);
ChildFormMessage?.Invoke(this, e);
}

/// <summary>
/// Raises the MainFormMessage event.
/// Raises a MessageToChildForm event.
/// </summary>
protected void OnMainFormMessage(FormMessageEventArgs e)
protected void OnMessageToChildForm(FormMessageEventArgs e)
{
MessageToChildForm?.Invoke(this, e);
MainFormMessage?.Invoke(this, e);
}

/// <summary>
/// Sends the message from a child form to a main form or vice versa.
/// Sends the message from a child form to other forms or vice versa.
/// </summary>
public bool SendMessage(Form source, string message, Dictionary<string, object> arguments = null)
public bool SendMessage(Control source, string message, Dictionary<string, object> arguments = null)
{
FormMessageEventArgs eventArgs = new FormMessageEventArgs(source, message, arguments);
FormMessageEventArgs eventArgs = new(source, message, arguments);

if (source == ChildForm)
OnChildFormMessage(eventArgs);
OnMessageFromChildForm(eventArgs);
else
OnMainFormMessage(eventArgs);
OnMessageToChildForm(eventArgs);

return !eventArgs.Cancel;
}
Expand All @@ -108,14 +110,26 @@ public bool SendMessage(Form source, string message, Dictionary<string, object>
/// </summary>
public event EventHandler ModifiedChanged;

/// <summary>
/// Occurs when the child form sends a message to another form that subscribed to this event.
/// </summary>
public event EventHandler<FormMessageEventArgs> MessageFromChildForm;

/// <summary>
/// Occurs when another form sends a message to the child form.
/// </summary>
public event EventHandler<FormMessageEventArgs> MessageToChildForm;

/// <summary>
/// Occurs when a child form sends a message.
/// </summary>
[Obsolete("Use the MessageFromChildForm event.")]
public event EventHandler<FormMessageEventArgs> ChildFormMessage;

/// <summary>
/// Occurs when a main form sends a message.
/// Occurs when the main form or another child form sends a message.
/// </summary>
[Obsolete("Use the MessageToChildForm event.")]
public event EventHandler<FormMessageEventArgs> MainFormMessage;
}
}
8 changes: 4 additions & 4 deletions Src/WinControl/WinControl/FormMessageEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ namespace WinControl
/// <para>Предоставляет данные для события сообщения формы.</para>
/// </summary>
/// <remarks>
/// Author: Mikhail Shiryaev, 2018-2019
/// Author: Mikhail Shiryaev, 2018-2019, 2021
/// </remarks>
public class FormMessageEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the class.
/// </summary>
public FormMessageEventArgs(Form source, string message, Dictionary<string, object> arguments)
public FormMessageEventArgs(Control source, string message, Dictionary<string, object> arguments)
{
Source = source;
Message = message;
Expand All @@ -26,9 +26,9 @@ public FormMessageEventArgs(Form source, string message, Dictionary<string, obje


/// <summary>
/// Gets the form initiated the event.
/// Gets the control or form initiated the event.
/// </summary>
public Form Source { get; }
public Control Source { get; }

/// <summary>
/// Gets the message to send.
Expand Down
8 changes: 5 additions & 3 deletions Src/WinControl/WinControl/WinControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,10 @@ public void AddForm(Form form, string hint, Image image, TreeNode treeNode)
// tests the form for duplicating
// проверка дублирования формы
foreach (TabPage tab in tabPageList)
{
if (tab.ChildForm != null && tab.ChildForm == form)
throw new ArgumentException("The form is duplicated.");
}

// hides the child form that was selected
// сокрытие дочерней формы, которая была выбрана ранее
Expand Down Expand Up @@ -734,14 +736,14 @@ public void AddForm(Form form, string hint, Image image, TreeNode treeNode)
// настройка формы
if (form is IChildForm childForm)
{
childForm.ChildFormTag = new ChildFormTag()
childForm.ChildFormTag = new ChildFormTag
{
TreeNode = treeNode,
TabPanel = pnlNewTab,
ChildForm = form
};
childForm.ChildFormTag.ModifiedChanged += ChildFormTag_ModifiedChanged;
childForm.ChildFormTag.ChildFormMessage += ChildFormTag_ChildFormMessage;
childForm.ChildFormTag.MessageFromChildForm += ChildFormTag_MessageFromChildForm;
}

// shows the form
Expand Down Expand Up @@ -1221,7 +1223,7 @@ private void ChildFormTag_ModifiedChanged(object sender, EventArgs e)
}
}

private void ChildFormTag_ChildFormMessage(object sender, FormMessageEventArgs e)
private void ChildFormTag_MessageFromChildForm(object sender, FormMessageEventArgs e)
{
OnChildFormMessage(e);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/WinControl/WinControl/WinControl.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Authors>Mikhail Shiryaev</Authors>
<Company />
<PackageProjectUrl></PackageProjectUrl>
Expand Down
2 changes: 1 addition & 1 deletion Src/WinControl/WinControlSample/WinControlSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

Expand Down

0 comments on commit 15fba79

Please sign in to comment.