How to Convert Double Byte String to Single Byte S

2019-04-15 04:13发布

I am a newbie at C#.
I have a Textbox and it is allowed to accept double_byte string and it is used to input/show Date Value. Now I am having a problem and I don't know how to solve this.I googled about it,and can't find any solution for it.
When I wrote Double_Byte Characters( 2012/12/31) ,I want to change this value to (2012/12/31) at Leave_Event of TextBox or Text_Changed Event of this TextBox. So, How can I solve the problem.

Edit->My Solution is Window Application.

Thanks in advance.

标签: c# textbox
3条回答
在下西门庆
2楼-- · 2019-04-15 04:22

Thank you for all of your answers and interests.
I searched a solution for this and not found any answer .Finally ,I got a nice answer from my colleague. Therefore, I share the answer of this problem.

  1. Add Reference Microsoft.VisualBasic
  2. write code like this(test like sa_ddam213's answer)-

using Microsoft.VisualBasic;

private void button1_Click(object sender, EventArgs e)
{
      string inputText = textBox1.Text;
      string singleByteString =  Strings.StrConv(inputText, VbStrConv.Narrow, 0);

      textBox2.Text = singleByteString;
      textBox3.Text = inputText;
}
查看更多
够拽才男人
3楼-- · 2019-04-15 04:28

When you send your text box value in database , parse it to Date.

Make sure that you are storing that text box'x value in database as DateTime datatype.

parsing can be done as follows>

double d = double.Parse(txtDate.text);
DateTime conv = DateTime.FromOADate(d);

Or simplest way use>>

DateTime.Parse(txtDate.Text);

Hope this will help you.

查看更多
来,给爷笑一个
4楼-- · 2019-04-15 04:38

You should be able to use Encoding.Default to convert the double_byte string to a single_byte string

 string singleByteString = Encoding.Default.GetString(Encoding.Default.GetBytes(inputText));

Tests:

    private void button1_Click(object sender, EventArgs e)
    {
        string inputText = textBox1.Text;
        string singleByteString = Encoding.Default.GetString(Encoding.Default.GetBytes(inputText));

        textBox2.Text = singleByteString;
        textBox3.Text = inputText;
    }

Result:

enter image description here

查看更多
登录 后发表回答