Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#42 - add StylableInputBox #50

Merged
merged 9 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ At last, you can show the dialog using `mBox.ShowDialog()`

**Note:** If you change the size on the controls (e.g. increase the font size), please call `UpdateSize()` to update the UI to the new settings. Otherwise, the Ui may look weird.

### Interaction.InputBox => StylableInputBox
With the `StylableInputBox`, you can create forms similar to VB.NETs `Interaction.InputBox` but the handling is a bit different (as it's practically a slightly different `StylableMessageBox`).
Therefore, we allow you to style and adjust the form before you show it.

Let's create an input box first:
```csharp
StylableNumericInputBox iBox = StylableNumericInputBox.BUILDER
.WithTitle("Numeric Test", MessageBoxIcon.Information)
.WithText("Please enter a random number between -100 and 100")
.WithHelpButton(new Uri("https://github.com/Assorted-Development/winforms-stylable-controls"))
.WithTimeout(TimeSpan.FromSeconds(30), DialogResult.Cancel)
.ForNumericValue(0, -100, 100);
```
This will create an input box for numeric values (currently, we support text via `TextBox` and numeric input via `NumericUpDown`).
Now, let's style the form as we want to: `iBox.StylableControls.Text.ForeColor = Color.Red;`
At last, you can show the dialog using `iBox.ShowDialog()` and either use its `DialogResult` or `iBox.Value` to get the input entered by the user.

**Note:** If you change the size on the controls (e.g. increase the font size), please call `UpdateSize()` to update the UI to the new settings. Otherwise, the Ui may look weird.


## Contributions

Please view the [contributing guide](/CONTRIBUTING.md) for more information.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,36 @@ public FrmDefault()

private void stylableButton1_Click(object sender, EventArgs e)
{
StylableMessageBox mBox = StylableMessageBox.BUILDER
StylableMessageBox messageBox = StylableMessageBox.BUILDER
.WithTitle("This is a text", MessageBoxIcon.Information)
.WithText("This is an example of a stylable MessageBox")
.WithButtons(MessageBoxButtons.YesNoCancel)
.WithCheckBox("Do you like it?")
.WithHelpButton(new Uri("https://github.com/Assorted-Development/winforms-stylable-controls"))
.WithTimeout(TimeSpan.FromSeconds(30), DialogResult.Cancel)
.Build();
mBox.ShowDialog();
messageBox.ShowDialog();
}

private void stylableButton2_Click(object sender, EventArgs e)
{
StylableNumericInputBox inputBox = StylableNumericInputBox.BUILDER
.WithTitle("Numeric Test", MessageBoxIcon.Question)
.WithText("Please enter a random number between -100 and 100")
.WithButtons(MessageBoxButtons.OKCancel)
.WithHelpButton(new Uri("https://github.com/Assorted-Development/winforms-stylable-controls"))
.WithTimeout(TimeSpan.FromSeconds(30), DialogResult.Cancel)
.ForNumericValue(0, -100, 100);

if (inputBox.ShowDialog() == DialogResult.OK)
{
StylableMessageBox mBox = StylableMessageBox.BUILDER
.WithTitle("Result value", MessageBoxIcon.Information)
.WithText($"You entered the following value: {inputBox.Value}")
.WithButtons(MessageBoxButtons.OK)
.Build();
mBox.ShowDialog();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
namespace StylableWinFormsControls
{
/// <summary>
/// A stylable version of VB.NETs Interaction.InputBox
/// </summary>
/// <typeparam name="T">the type of the input control</typeparam>
/// <typeparam name="U">the type of the value returned from the input control</typeparam>
public abstract class StylableInputBox<T, U> : StylableInteractionBox<T> where T : Control
{
/// <summary>
/// Returns a builder object to configure the <see cref="StylableInputBox"/>
/// </summary>
public static StylableInputBoxBuilder BUILDER => new();
/// <summary>
/// accessor to return the current value
/// </summary>
private Func<T, U> _accessor;

/// <summary>
/// Returns the value of the input control used with this instance. <br/>
/// Return type: <see cref="decimal"/> for <see cref="NumericUpDown"/> | <see cref="string"/>
/// for <see cref="TextBox"/>.
/// </summary>
/// <remarks>May be null if no input control has been specified (yet).</remarks>
/// <exception cref="NotSupportedException">
/// Throws if <typeparamref name="T"/> hasn't been implemented for this property.
/// </exception>
public U Value => _accessor((T)StylableControls.InputControl);

/// <summary>
/// constructor. not available to others as they should use the <see cref="StylableMessageBoxBuilder"/>
/// </summary>
/// <param name="caption">the caption</param>
/// <param name="icon">the icon in the title bar</param>
/// <param name="text">the prompt text</param>
/// <param name="defaultButton">defines which button should be selected by default</param>
/// <param name="helpUri">the url to open when the user clicks on the help button</param>
/// <param name="timeout">defines the intervall after which the messagebox is closed automatically</param>
/// <param name="timeoutResult">defines the <see cref="DialogResult"/> to return when the timeout hits</param>
/// <param name="inputControl">the control to input the value</param>
/// <param name="accessor">accessor to read the current value from the control</param>
protected StylableInputBox(
string caption,
MessageBoxIcon icon,
string text,
MessageBoxButtons buttons,
MessageBoxDefaultButton defaultButton,
Uri? helpUri,
TimeSpan? timeout,
DialogResult timeoutResult,
T inputControl,
Func<T, U> accessor
) : base(caption, icon, text, buttons, defaultButton, helpUri, timeout, timeoutResult)
{
_accessor = accessor;
StylableControls.InputControl = handleInput(inputControl);
UpdateSize();
stretchInputControlWidth(inputControl);
}

protected override Point OnUpdateControlSizeMid(int marginLeft, int marginTop, Point currentContentPos)
{
currentContentPos.Y += StylableControls.Text is null ? 0 : StylableControls.Text.Height + 6;

if (StylableControls.InputControl is not null)
{
// Margins on CheckBoxes seem to not work directly
StylableControls.InputControl.Left = currentContentPos.X + marginLeft;
StylableControls.InputControl.Top = currentContentPos.Y + marginTop;
currentContentPos.Y += StylableControls.InputControl.Height + 6;
}

currentContentPos.Y += 16;
return currentContentPos;
}

/// <summary>
/// Sets width of the input control to full length of the containing window (with margin).
/// </summary>
/// <param name="inputControl">Input Control to stretch</param>
private void stretchInputControlWidth(Control inputControl)
{
// Set width to complete width, but leave as much space to the right of the control as to the left.
inputControl.Width = ClientRectangle.Width - inputControl.Left - inputControl.Margin.Left - inputControl.Margin.Right;
UpdateSize();
}

/// <summary>
/// adds the input control
/// </summary>
/// <param name="input">the input control</param>
private T handleInput(T input)
{
Controls.Add(input);
return input;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Cancel" xml:space="preserve">
<value>Abbrechen</value>
</data>
<data name="OK" xml:space="preserve">
<value>OK</value>
</data>
</root>
Loading