Skip to main content
1 of 3
Alexander
  • 603
  • 4
  • 11

PriceDialog - Prompt & price validation

In a recent CodeReview I presented a Product Manager that manages product listings and prices. Based on the great feedback I have refactored the mentioned Prompt and validator class and want feedback about the code and naming.

Utilities/ProductValidator.cs

namespace Ferienaufgabe_2.Utilities
{
class ProductValidator
{
    public bool IsValidPriceFormat(string value)
    {
        decimal result;
        return (decimal.TryParse(value, out result) && result >= 0 );
    }
}
}

View/PriceDialog.cs

using Ferienaufgabe_2.Utilities;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Ferienaufgabe_2.View
{
class PriceDialog
{
    private ProductValidator ProductValidator { get; set; }
    private Form DialogBox { get; set; }
    private Label TextLabel { get; set; }
    private TextBox InputBox { get; set; }
    private Button ButtonConfirmation { get; set; }

    private const string DefaultTextInputBox = "z.B.: 10,00";
    private const string MessageInvalidPriceFormat = "Ungültige Preiseingabe! Z.B.: 10,00";
    private const string TextOfLabel = "Preis:";
    private const string TextOfConfirmButton = "Prozess abschliessen";

    public PriceDialog()
    {
        this.InitializeComponent();
        this.DesignComponent();
        this.AddEventsToComponent();
        this.AddComponentToDialog();
    }

    private void InitializeComponent()
    {
        this.ProductValidator = new ProductValidator();
        this.DialogBox = new Form();
        this.TextLabel = new Label();
        this.InputBox = new TextBox();
        this.ButtonConfirmation = new Button();
    }

    private void DesignComponent()
    {
        this.DialogBox.Width = 300;
        this.DialogBox.Height = 120;
        this.DialogBox.StartPosition = FormStartPosition.CenterParent;
        this.DialogBox.FormBorderStyle = FormBorderStyle.None;

        this.TextLabel.Location = new Point(110, 15);
        this.TextLabel.Text = TextOfLabel;
        this.TextLabel.Font = new Font("Arial", 18);

        this.InputBox.Location = new Point(40, (this.TextLabel.Location.Y + this.TextLabel.Height + 10));
        this.InputBox.Width = 205;
        this.InputBox.Text = DefaultTextInputBox;
        this.InputBox.ForeColor = Color.Gray;

        this.ButtonConfirmation.Location = new Point(40, (this.InputBox.Location.Y + this.InputBox.Height + 10));
        this.ButtonConfirmation.Width = InputBox.Width;
        this.ButtonConfirmation.Text = TextOfConfirmButton;
    }

    private void AddEventsToComponent()
    {
        this.InputBox.KeyUp += (sender, e) => {
            if (e.KeyCode == Keys.Enter && this.ProductValidator.IsValidPriceFormat(this.InputBox.Text))
            {
                this.DialogBox.Close();
            }
            else if (e.KeyCode == Keys.Enter)
            {
                this.TextLabel.Focus(); // Otherwise MessageBox cannot be canceled via {enter}
                MessageBox.Show(MessageInvalidPriceFormat);
            }
        };

        this.ButtonConfirmation.Click += (sender, e) => {
            if (this.ProductValidator.IsValidPriceFormat(InputBox.Text))
            {
                this.DialogBox.Close();
            }
            else
            {
                this.TextLabel.Focus(); // Otherwise MessageBox cannot be canceled via {enter}
                MessageBox.Show(MessageInvalidPriceFormat);
            }
        };

        this.InputBox.GotFocus += (sender, e) =>
        {
            if (this.InputBox.Text == DefaultTextInputBox)
            {
                this.InputBox.Text = "";
                this.InputBox.ForeColor = Color.Black;
            }
        };
        this.InputBox.LostFocus += (sender, e) =>
        {
            if (this.InputBox.Text.Trim().Length == 0)
            {
                this.InputBox.Text = DefaultTextInputBox;
                this.InputBox.ForeColor = Color.Gray;
            }
        };
    }

    private void AddComponentToDialog()
    {
        this.DialogBox.Controls.Add(this.TextLabel);
        this.DialogBox.Controls.Add(this.InputBox);
        this.DialogBox.Controls.Add(this.ButtonConfirmation);
    }

    public decimal Show()
    {
        this.DialogBox.ShowDialog();
        // This goes well anytime due to a check in the dispose actions.
        return Convert.ToDecimal(this.InputBox.Text);
    }
}
}
Alexander
  • 603
  • 4
  • 11