Hello there,
Please see the below given code and error below. I am getting the error when I am selecting currency property. Please help.
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using NPMS.Library.Routines;
namespace NPMS.Library.Controls
{
#region cTextBox
publicclasscTextBox : TextBox
{
publiccTextBox()
{
TextBoxType = TextBoxTypeConstants.TextBox;
IsCompulsory = _CanEmpty.No;
}
publicenumTextBoxTypeConstants
{
TextBox = 0,
Numeric,
Date,
Currency,
YesNo
}
publicenum_CanEmpty
{
No = 0,
Yes
}
TextBoxTypeConstantsTextBoxType;
[Description("Yes if you want to check for empty values.")]
public_CanEmpty IsCompulsory { get; set; }
[Description("Select the type of textbox (Date, Textbox, Numeric, Currency, YesNo).")]
publicTextBoxTypeConstants BoxType
{
get { returnTextBoxType; }
set
{
TextBoxType = value;
if (TextBoxType == TextBoxTypeConstants.TextBox)
{
AutoCompleteSource = AutoCompleteSource.None;
AutoCompleteMode = AutoCompleteMode.None;
AutoCompleteCustomSource.Clear();
TextAlign = HorizontalAlignment.Left;
MaxLength = 32767;
Text = string.Empty;
}
elseif (TextBoxType == TextBoxTypeConstants.Currency)
{
AutoCompleteSource = AutoCompleteSource.None;
TextAlign = HorizontalAlignment.Right;
MaxLength = 25;
Text = "0.00";
}
elseif (TextBoxType == TextBoxTypeConstants.Numeric)
{
AutoCompleteSource = AutoCompleteSource.None;
TextAlign = HorizontalAlignment.Right;
MaxLength = 25;
Text = "0";
}
elseif (TextBoxType == TextBoxTypeConstants.YesNo)
{
TextAlign = HorizontalAlignment.Left;
AutoCompleteCustomSource.Clear();
AutoCompleteMode = AutoCompleteMode.SuggestAppend;
AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteCustomSource.Add("Yes");
AutoCompleteCustomSource.Add("No");
MaxLength = 3;
Text = "Yes";
}
elseif (TextBoxType == TextBoxTypeConstants.Date)
{
AutoCompleteSource = AutoCompleteSource.None;
AutoCompleteMode = AutoCompleteMode.None;
AutoCompleteCustomSource.Clear();
TextAlign = HorizontalAlignment.Left;
MaxLength = 10;
DateTime _dt;
try
{
_dt = DateTime.Parse(DateTime.Now.ToShortDateString());
Text = _dt.ToString("dd-MM-yyyy");
} catch (Exception ex)
{
Text = string.Empty;
LogEvent.WriteErrorLog(ex);
}
}
Invalidate();
}
}
protectedoverridevoidOnEnter(EventArgs e)
{
base.OnEnter(e);
BackColor = Color.Black;
ForeColor = Color.White;
if (TextBoxType == TextBoxTypeConstants.Currency)
if (Text == "0.00")
Text = String.Format("{0:0.00}",
Convert.ToDecimal(Clipboard.GetText(TextDataFormat.Text)));
SelectAll();
}
protectedoverridevoidOnLeave(EventArgs e)
{
base.OnLeave(e);
BackColor = Color.White;
ForeColor = Color.Black;
Text = Text.Trim();
if (IsCompulsory == _CanEmpty.Yes)
{
if (string.IsNullOrEmpty(Text))
Focus();
else
if (TextBoxType == TextBoxTypeConstants.YesNo)
if (!AutoCompleteCustomSource.Contains(TCase(Text)))
Text = "Yes";
}
else
{
if (TextBoxType == TextBoxTypeConstants.YesNo)
if (!AutoCompleteCustomSource.Contains(TCase(Text)))
Text = "Yes";
}
}
stringTCase(string s)
{
CultureInfo CI = CultureInfo.CurrentCulture;
TextInfo TI = CI.TextInfo;
string txt = TI.ToTitleCase(s);
return txt.Trim();
}
protectedoverridevoidOnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (TextBoxType == TextBoxTypeConstants.Currency)
{
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8&& e.KeyChar != 46))
{
e.Handled = true;
return;
}
if (e.KeyChar == 46)
{
if (Text.IndexOf(e.KeyChar) != -1)
e.Handled = true;
}
}
elseif (TextBoxType == TextBoxTypeConstants.Numeric)
{
if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8))
{
e.Handled = true;
return;
}
}
}
protectedoverridevoidOnValidated(EventArgs e)
{
base.OnValidated(e);
if (TextBoxType == TextBoxTypeConstants.Numeric)
{
try
{
Text = String.Format("{0:0}", Convert.ToDecimal(Text));
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
}
}
elseif (TextBoxType == TextBoxTypeConstants.Currency)
{
if (string.IsNullOrEmpty(Text))
Text = "0.00";
try
{
Text = String.Format("{0:0.00}", Convert.ToDecimal(Text));
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
}
}
elseif (TextBoxType == TextBoxTypeConstants.Date)
{
DateTime _dt;
try
{
if (string.IsNullOrEmpty(Text))
_dt = DateTime.Parse(DateTime.Now.ToShortDateString());
else
_dt = DateTime.Parse(Text);
Text = _dt.ToString("dd-MM-yyyy");
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
_dt = DateTime.Parse(DateTime.Now.ToShortDateString());
Text = _dt.ToString("dd-MM-yyyy");
}
}
else
Text = TCase(Text);
}
protectedoverridebool ShowFocusCues { get { returnfalse; } }
protectedoverridevoidOnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Escape)
{
e.SuppressKeyPress = true;
Text = string.Empty;
}
elseif (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
if (TextBoxType == TextBoxTypeConstants.Numeric)
{
try
{
Text = String.Format("{0:0}", Convert.ToDecimal(Text));
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
}
}
elseif (TextBoxType == TextBoxTypeConstants.Currency)
{
if (string.IsNullOrEmpty(Text))
Text = "0.00";
try
{
Text = String.Format("{0:0.00}", Convert.ToDecimal(Text));
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
}
}
elseif (TextBoxType == TextBoxTypeConstants.Date)
{
DateTime _dt;
try
{
if (string.IsNullOrEmpty(Text))
_dt = DateTime.Parse(DateTime.Now.ToShortDateString());
else
_dt = DateTime.Parse(Text);
Text = _dt.ToString("dd-MM-yyyy");
}
catch (Exception ex)
{
LogEvent.WriteErrorLog(ex);
_dt = DateTime.Parse(DateTime.Now.ToShortDateString());
Text = _dt.ToString("dd-MM-yyyy");
}
}
else
Text = TCase(Text);
}
}
}
#endregion
}
ERROR While Running (Even not displaying the form):
System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Decimal.Parse(String s, IFormatProvider provider)
at System.Convert.ToDecimal(String value)
at NPMS.Library.Controls.cTextBox.OnEnter(EventArgs e)
at System.Windows.Forms.Control.NotifyEnter()
at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
at System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)
at System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)
at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)
at System.Windows.Forms.ContainerControl.SetActiveControl(Control ctl)
at System.Windows.Forms.ContainerControl.set_ActiveControl(Control value)
at System.Windows.Forms.Control.Select(Boolean directed, Boolean forward)
at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Control.SelectNextControlInternal(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
at System.Windows.Forms.Form.set_Active(Boolean value)
at System.Windows.Forms.Form.WmActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.RunDialog(Form form)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at System.Windows.Forms.Form.ShowDialog()
at NPMS.Program.Main()