SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Input String Not In Correct Format.... Please help.

Last post 06-04-2015 1:26 PM by bastarmedia. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 06-04-2015 1:26 PM

    Input String Not In Correct Format.... Please help.

    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
        public class cTextBox : TextBox
        {
            public cTextBox()
            {
                TextBoxType = TextBoxTypeConstants.TextBox;
                IsCompulsory = _CanEmpty.No;
            }
            
            public enum TextBoxTypeConstants
            {
                TextBox = 0,
                Numeric,
                Date,
                Currency,
                YesNo
            }
            
            public enum _CanEmpty
            {
                No = 0,
                Yes
            }
            
            TextBoxTypeConstants TextBoxType;
            
            [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).")]
            public TextBoxTypeConstants BoxType
            {
                get { return TextBoxType; }
                set
                {
                    TextBoxType = value;
                    
                    if (TextBoxType == TextBoxTypeConstants.TextBox)
                    {
                        AutoCompleteSource = AutoCompleteSource.None;
                        AutoCompleteMode = AutoCompleteMode.None;
                        AutoCompleteCustomSource.Clear();
                        TextAlign = HorizontalAlignment.Left;
                        MaxLength = 32767;
                        
                        Text = string.Empty;
                    }
                    else if (TextBoxType == TextBoxTypeConstants.Currency)
                    {
                        AutoCompleteSource = AutoCompleteSource.None;
                        TextAlign = HorizontalAlignment.Right;
                        MaxLength = 25;
                        
                        Text = "0.00";
                    }
                    else if (TextBoxType == TextBoxTypeConstants.Numeric)
                    {
                        AutoCompleteSource = AutoCompleteSource.None;
                        TextAlign = HorizontalAlignment.Right;
                        MaxLength = 25;
                        
                        Text = "0";
                    }
                    else if (TextBoxType == TextBoxTypeConstants.YesNo)
                    {
                        TextAlign = HorizontalAlignment.Left;
                        
                        AutoCompleteCustomSource.Clear();
                        AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                        AutoCompleteSource = AutoCompleteSource.CustomSource;
                        AutoCompleteCustomSource.Add("Yes");
                        AutoCompleteCustomSource.Add("No");
                        MaxLength = 3;
                        
                        Text = "Yes";
                    }
                    else if (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();
                }
            }
            
            protected override void OnEnter(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();
            }
            
            protected override void OnLeave(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";
                }
            }
            
            string TCase(string s)
            {
                CultureInfo CI = CultureInfo.CurrentCulture;
                TextInfo TI = CI.TextInfo;
                string txt = TI.ToTitleCase(s);

                return txt.Trim();
            }
            
            protected override void OnKeyPress(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;
                    }
                }
                else if (TextBoxType == TextBoxTypeConstants.Numeric)
                {
                    if (((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8))
                    {
                        e.Handled = true;
                        return;
                    }
                }
            }
            
            protected override void OnValidated(EventArgs e)
            {
                base.OnValidated(e);

                if (TextBoxType == TextBoxTypeConstants.Numeric)
                {
                    try
                    {
                        Text = String.Format("{0:0}", Convert.ToDecimal(Text));
                    }
                    catch (Exception ex)
                    {
                        LogEvent.WriteErrorLog(ex);
                    }
                }
                else if (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);
                    }
                }
                else if (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);
            }
            
            protected override bool ShowFocusCues { get { return false; } }
            
            protected override void OnKeyDown(KeyEventArgs e)
            {
                base.OnKeyDown(e);
                
                if (e.KeyCode == Keys.Escape)
                {
                    e.SuppressKeyPress = true;
                    Text = string.Empty;
                }
                else if (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);
                        }
                    }
                    else if (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);
                        }
                    }
                    else if (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()

Page 1 of 1 (1 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.