琪y 2022-03-17 14:50 采纳率: 100%
浏览 36
已结题

c#在textbox控件里面输入,然后在读出来

问题遇到的现象和发生背景

img

问题相关代码,请勿粘贴截图

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Srr110uLib;
using Srr1100U;
namespace WpfApplication1
{

public partial class MainWindow : Window
{

    SrrReader uhf = new SrrReader ("com8");
    private string cc2;

    public MainWindow ()
    {

        InitializeComponent ();
    }

    private void button_Click (object sender, RoutedEventArgs e)
    {

        /* SsuControl ssr = new SsuControl ();
         ssr.Init ("com8");
          byte[] cc=null;*/

        uhf.ConnDevice ();
        uhf.Read (c);


    }
    void c (string i)
    {
        this.Dispatcher.Invoke (() => { listBox.Items.Add (i); });
        uhf.CloseDevice ();

    }

    private void button1_Click (object sender, RoutedEventArgs e)
    {
        SsuControl cc = new SsuControl ();
        cc.Init ("com8");

        byte[] a = cc.ReadDate ();

        cc.WriteDate (a);
        foreach (byte i in a)
            textBox.Text += i.ToString ("X2");
        cc.Close ();
    }

    private void textBox1_TextChanged (object sender, TextChangedEventArgs e)
    {

    }

    private void button2_Click (object sender, RoutedEventArgs e)
    {
        SsuControl aa = new SsuControl ();
        aa.Init ("com8");



        /*byte[] b = {“”原本是在这里输入0x01这些“” };*/


        /*string cc = textBox1.Text.ToString ();
        cc2 = "00000000";
        cc = cc2.Substring (0, 8 - cc2.Length) + cc;
        byte[] b = System.Text.Encoding.Default.GetBytes (cc);这里是从网上摘抄的*/

        aa.WriteDate (b);
        aa.Close();
       






    }

    private void textBox_TextChanged (object sender, TextChangedEventArgs e)
    {

    }
}

}

运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果

byte[] b = {原本是在这里输入0x01这些“};,然后读出来,现在我想启动之后,在textbox里面写,然后读写出来,怎么弄各位大咖

  • 写回答

2条回答 默认 最新

  • 於黾 2022-03-17 15:30
    关注
    string s=textbox.Text.Replace(" ","");//去掉多余的空格
    if(s.Length%2>0)
    {
    //不是偶数,不能转换
    return;
    }
    byte[] b=new byte[s.Length/2];
    for (int i=0;i<b.Length;i++)
    {
    string s1=s.Substring(i*2,2);
    bool isbyte=byte.TryParse(s1,System.Globalization.NumberStyles.HexNumber,null,out b[i]);
    //这里要再判断一下isbyte,如果输入的没法转换成16进制数,后面不要发送
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月25日
  • 已采纳回答 3月17日
  • 创建了问题 3月17日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效