如何从类文件中读取值到表单 - c# [英] How to read a value from class file to form - c#

查看:82
本文介绍了如何从类文件中读取值到表单 - c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用C#的新手。我从在线获得这个程序用于串行通信,下面的代码片段是一个方法(属于class.cs),当缓冲区中有数据等待时将调用该方法。我需要自定义这个程序,即我想在我的form1中单独读取ByteToHex(comBuffer)或comBuffer变量的值。检查comBuffer的返回值后,我的程序中有一些需要相应运行的条件。请建议并展示实现它的方法。





I am very new for using C#. I got this program from online for serial communication and the below snippet is a method ( belongs to class.cs) that will be called when there is data waiting in the buffer. I need to customize this program i.e.i want to read the value of "ByteToHex(comBuffer)" or "comBuffer" variable alone in my form1. Upon checking the return value of "comBuffer" I have some conditions inside my program that needs to run accordingly. Please suggest and show a way to achieve it.


void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            //determine the mode the user selected (binary/string)
            switch (CurrentTransmissionType)
            {
                //user chose string
                case TransmissionType.Text:
                    //read data waiting in the buffer
                    string msg = comPort.ReadExisting();
                    //display the data to the user
                    DisplayData(MessageType.Incoming, msg + "\n");
                    break;
                //user chose binary
                case TransmissionType.Hex:
                    //retrieve number of bytes in the buffer
                    int bytes = comPort.BytesToRead;
                    //create a byte array to hold the awaiting data
                    byte[] comBuffer = new byte[bytes];
                    //read the data and store it
                    comPort.Read(comBuffer, 0, bytes);
                    //display the data to the user
                    DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
                    break;
                default:
                    //read data waiting in the buffer
                    string str = comPort.ReadExisting();
                    //display the data to the user
                    DisplayData(MessageType.Incoming, str + "\n");
                    break;

              
            }
        }





我的尝试:



我浏览了一些文档,解释为类创建一些构造函数并在form1中使用相同的构造函数。但是我无法理解。



What I have tried:

I went through some documentations which explained to create some constructors for the class and use the same in form1. But I couldn't understand quite.

推荐答案

假设Windows窗体并假设您的方法和字段位于名为Class.cs的文件中并且有一个名为该方法所属的Class1。以下应该有效。



你的课程如下。



Assuming windows forms and also assuming that your method and field is in a file called Class.cs and there is a class called "Class1" that the method is part of. The following should work.

Your class would be like below.

public class Class1
   {
       public string comPort;
       public Class1()
       {

       }
       void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
       {
           //determine the mode the user selected (binary/string)
           switch (CurrentTransmissionType)
           {
               //user chose string
               case TransmissionType.Text:
                   //read data waiting in the buffer
                   string msg = comPort.ReadExisting();
                   //display the data to the user
                   DisplayData(MessageType.Incoming, msg + "\n");
                   break;
               //user chose binary
               case TransmissionType.Hex:
                   //retrieve number of bytes in the buffer
                   int bytes = comPort.BytesToRead;
                   //create a byte array to hold the awaiting data
                   byte[] comBuffer = new byte[bytes];
                   //read the data and store it
                   comPort.Read(comBuffer, 0, bytes);
                   //display the data to the user
                   DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
                   break;
               default:
                   //read data waiting in the buffer
                   string str = comPort.ReadExisting();
                   //display the data to the user
                   DisplayData(MessageType.Incoming, str + "\n");
                   break;


           }
       }
   }





构造函数是片段





The constructor is the snippet

public Class1()
       {
 
       }





然后你的form1.cs文件看起来像这样。





And then your form1.cs file would look like this.

public partial class Form1 : Form
    {
        public Class1 dc = new Class1();
        public Form1()
        {
            InitializeComponent();
            var tmp = dc.comPort;
            
        }
    }


这篇关于如何从类文件中读取值到表单 - c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆