예제 #1
0
        private void acceptButton_Click(object sender, EventArgs e)
        {
            string ValueString = ValueTextBox.Text;

            switch (ScanType)
            {
            case ScanDataType.Binary:
                if (CheckSyntax.BinaryValue(ValueString))
                {
                    Value = Conversions.ToUnsigned(ValueString, ScanType);
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Byte:
                //Check syntax
                if (CheckSyntax.ByteValue(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        //Take unsigned value
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else     //Take unsigned value after converting it from hex
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToByte(ValueString).ToString(), ScanType);
                    }
                }
                else     //Invalid syntax
                {
                    failed = true;
                }
                break;

            case ScanDataType.Int16:
                if (CheckSyntax.Int32Value(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToShort(ValueString).ToString(), ScanType);
                    }
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Int32:
                if (CheckSyntax.Int32Value(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToInt(ValueString).ToString(), ScanType);
                    }
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Int64:
                if (CheckSyntax.Int64Value(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToUInt64(ValueString).ToString(), ScanType);
                    }
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Single:
                if (CheckSyntax.SingleValue(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToSingle(ValueString).ToString(), ScanType);
                    }
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Double:
                if (CheckSyntax.DoubleValue(ValueString, IsHexCB.Checked))
                {
                    if (!IsHexCB.Checked)
                    {
                        Value = Conversions.ToUnsigned(ValueString, ScanType);
                    }
                    else
                    {
                        Value = Conversions.ToUnsigned(Conversions.HexToDouble(ValueString).ToString(), ScanType);
                    }
                }
                else
                {
                    failed = true;
                }
                break;

            case ScanDataType.Text:
                //TODO: everything
                Value = ValueString;
                break;
            }

            if (failed)
            {
                MessageBox.Show("Invalid value format.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }