在Windows窗体中调用函数c# [英] calling a function in windows form c#

查看:275
本文介绍了在Windows窗体中调用函数c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用串行通信发送操纵杆轴数据,我有一些问题,因为这是第一次在Windows窗体中使用C#。在代码的底部我有一个名为的函数:public void sending,但是当我编译程序时没有执行该函数。所以我要做的就是执行它。







i need to send joystick axis data using serial communication, and i have some problems since this is first time im using C# in windows form. On the bottom of the code i have a function called: public void sending, but when i compile the program is not executing that function. so what i have to do to execute it.



namespace JoystickSample
{

   
    public partial class frmMain : Form
    {
        
        private JoystickInterface.Joystick jst;
       
        public frmMain()
        {
            InitializeComponent();
        }

    
        private void frmMain_Load(object sender, EventArgs e)
        {
            // grab the joystick
            jst = new JoystickInterface.Joystick(this.Handle);
            string[] sticks = jst.FindJoysticks();
            jst.AcquireJoystick(sticks[0]);

            // add the axis controls to the axis container
            for (int i = 0; i < jst.AxisCount; i++)
            {
                Axis ax = new Axis();
                ax.AxisId = i + 1;
                flpAxes.Controls.Add(ax);
            }

            // add the button controls to the button container
            for (int i = 0; i < jst.Buttons.Length; i++)
            {
                JoystickSample.Button btn = new Button();
                btn.ButtonId = i + 1;
                btn.ButtonStatus = jst.Buttons[i];
                flpButtons.Controls.Add(btn);
            }

            // start updating positions
            tmrUpdateStick.Enabled = true;
        }

        private void tmrUpdateStick_Tick(object sender, EventArgs e)
        {
            // get status
            jst.UpdateStatus();

            // update the axes positions
            foreach (Control ax in flpAxes.Controls)
            {
                if (ax is Axis)
                {
                    switch (((Axis)ax).AxisId)
                    {
                        case 1:
                            ((Axis)ax).AxisPos = jst.AxisA;
                            
                            break;
                        case 2:
                            ((Axis)ax).AxisPos = jst.AxisB;

                            break;
                        case 3:
                            ((Axis)ax).AxisPos = jst.AxisC;
                            break;
                        case 4:
                            ((Axis)ax).AxisPos = jst.AxisD;
                            break;
                        case 5:
                            ((Axis)ax).AxisPos = jst.AxisE;
                            break;
                        case 6:
                            ((Axis)ax).AxisPos = jst.AxisF;
                            break;
                    }                
                 
                }
            }

            // update each button status
            foreach (Control btn in flpButtons.Controls)
            {
                if (btn is JoystickSample.Button)
                {
                    ((JoystickSample.Button)btn).ButtonStatus =
                        jst.Buttons[((JoystickSample.Button)btn).ButtonId - 1];
                }
            }
        }

        private void flpAxes_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            SerialPort.PortName = "COM3";
            SerialPort.BaudRate = 9600;

            SerialPort.Open();
           if (SerialPort.IsOpen)
            {
                buttonStart.Enabled = false;
                buttonStop.Enabled = true;
               
            }
        }
        
        private void buttonStop_Click(object sender, EventArgs e)
        {
            if (SerialPort.IsOpen)
            {
                SerialPort.Close();
                buttonStart.Enabled = true;
                buttonStop.Enabled = false;

            }
        }

      
            public void sending(object sender, EventArgs e)
            {
                if (!SerialPort.IsOpen) return;
                //slanje AxisPos=jst.AxisA(ili možda axisA?) 

                string strAxisA = Convert.ToString(jst.AxisA);
                SerialPort.Write(strAxisA);

                string strAxisB = Convert.ToString(jst.AxisB);
                SerialPort.Write(strAxisB);

                string strAxisC = Convert.ToString(jst.AxisC);
                SerialPort.Write(strAxisC);

                string strAxisD = Convert.ToString(jst.AxisD);
                SerialPort.Write(strAxisD);
            }
       
       
    }
}

推荐答案

既然你不喜欢不使用任何方法的参数发件人 EventArgs ,您只需调用
Since you don't use any of the method's arguments sender and EventArgs, you can simply call
sending(null, null);

来自代码中的任何位置。

另一方面,因为没有使用任何参数,你可以完全省略它们。



Windows Forms是一种事件驱动的架构。决定从哪个事件调用你的方法留给你。



如果没有事件,但你想要数据只是自己来,你必须使用计时器来生成这样的事件。有三个 时间表 可用。有关它们的不同之处,请查看链接的MSDN页面。

from anywhere in your code.
On the other hand, since neither of the arguments is used, you could omit them altogether.

Windows Forms is an event-driven architecture. Deciding what event to call your method from is left to you.

Should there be no event but you want the data just to be coming on its own, you have to use a timer to generate such an event. There are three Timers available that I know of. For their differences, have a look at the linked MSDN pages.


我没有看到您从串口读取和写入的任何代码?
I did not see any code where your reading and writing from Serial Port?


这是一个我想要发送的功能。所以你能告诉我应该如何发送吗?

here is a function that i want to be send. so can you show me how that should be send?
public void sending(object sender, EventArgs e)
           {
               if (!SerialPort.IsOpen) return;
               //slanje AxisPos=jst.AxisA(ili možda axisA?)

               string strAxisA = Convert.ToString(jst.AxisA);
               SerialPort.Write(strAxisA);

               string strAxisB = Convert.ToString(jst.AxisB);
               SerialPort.Write(strAxisB);

               string strAxisC = Convert.ToString(jst.AxisC);
               SerialPort.Write(strAxisC);

               string strAxisD = Convert.ToString(jst.AxisD);
               SerialPort.Write(strAxisD);
           }


这篇关于在Windows窗体中调用函数c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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