简体   繁体   English

如何在form1中使用Class.cs

[英]How to use Class.cs in form1

This is how my tab control looks.这就是我的选项卡控件的外观。 Basically, I have a tabcontrol of let's say 4 items, detector camera lens focus .基本上,我有一个选项卡控件,可以说 4 个项目, detector camera lens focus Instead of writing a bunch of codes all together in my form1.cs, I've created a class and wrote my codes in classes detector.cs , camera.cs lens.cs and focus.cs , however now I`m stuck as to how do I combine them together with my form1 so that the code will work?我没有在我的 form1.cs 中编写一堆代码,而是创建了一个类并在类detector.cs camera.cscamera.cs lens.csfocus.cs编写了我的代码,但是现在我坚持我如何将它们与我的 form1 结合在一起,以便代码可以工作?

*If I put everything in form1.cs it will work. *如果我将所有内容都放在 form1.cs 中,它将起作用。 However, when I create a new class and shift the codes over it doesn't work anymore.但是,当我创建一个新类并将代码转移到它时,它不再起作用。 I've never separated them before.我从来没有把他们分开过。 But I feel that separating them is a lot neater when viewing the codes.但是我觉得在查看代码时将它们分开会更整洁。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CamTest
{
    class Camera
    {
        private void CboOut_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboOut.SelectedIndex;
            cmd = 0x101E;
            len = 0x0001;

            ///Code
            if (num == 0)
            {
                val = 0x03;
            }
            else if (num == 1)
            {
                val = 0x02;
            }
            else if (num == 2)
            {
                val = 0x01;
            }
            else
            {
                val = 0x00;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void CboPol_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboPol.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1030;
            }
            else
            {
                cmd = 0x1031;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void CboAmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboAmp.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x102A;
            }
            else if (num == 1)
            {
                cmd = 0x102B;
            }
            else if (num == 2)
            {
                cmd = 0x102C;
            }
            else
            {
                cmd = 0x102D;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void CboPro_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboPro.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1017;
            }
            else
            {
                cmd = 0x1016;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void CboFlip_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboFlip.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1050;
            }
            else if (num == 1)
            {
                cmd = 0x1051;
            }
            else if (num == 2)
            {
                cmd = 0x1052;
            }
            else
            {
                cmd = 0x1053;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void chkboxDDE_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }
            else
            {
                cmd = 0x1018;
                len = 0x0000;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void TrkbarFine_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }

            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void TrkbarCoarse_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void chkboxMan_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            cmd = 0x1020;
            len = 0x0004;

            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                cmd = 0x1021;
                len = 0x0000;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void TrkbarCon_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            cmd = 0x1020;
            len = 0x0004;

            ///Code
            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void TrkbarBright_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            len = 0x0004;
            cmd = 0x1020;

            ///Code
            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        private void chkboxCursor_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            len = 0x0001;
            cmd = 0x1034;

            ///Code
            if (chkboxCursor.Checked)
            {
                val = 0x01;
            }
            else
            {
                val = 0x00;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void SendPosition(int Ypos, int Xpos)
        {
            ///Declaration
            cmd = 0x1035;
            len = 0x0004;

            ///Code
            val = (uint)((Ypos << 16) + Xpos);

            ///Pack
            Packs(len, cmd, val);
        }

        private void btnTt_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if ((y - 16) < 0)
            {
                return;
            }
            else
            {
                y -= 16;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        private void BtnT_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if (y <= 0)
            {
                return;
            }
            else
            {
                y--;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        private void BtnD_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if (y >= (height - 1))
            {
                return;
            }
            else
            {
                y++;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        private void BtnDd_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if ((y + 16) > (height - 1))
            {
                return;
            }
            else
            {
                y += 16;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        private void BtnL_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if (x <= 0)
            {
                return;
            }
            else
            {
                x--;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        private void BtnLl_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if ((x - 16) < 0)
            {
                return;
            }
            else
            {
                x -= 16;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        private void BtnR_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if (x >= (width - 1))
            {
                return;
            }
            else
            {
                x++;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        private void BtnRr_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if ((x + 16) > (width - 1))
            {
                return;
            }
            else
            {
                x += 16;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        private void BtnMid_Click(object sender, EventArgs e)
        {
            txtboxX.Text = (width / 2).ToString();
            txtboxY.Text = (height / 2).ToString();

            int x = int.Parse(txtboxX.Text);
            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }
    }
}

*note all the buttons and textbox are in form1, hence unable to be detected. *注意所有按钮和文本框都在form1中,因此无法检测到。 I just want to link them together so the entire code works.我只是想将它们链接在一起,以便整个代码都能正常工作。

class.Func();类.Func(); Func have to be Public. Func 必须是公共的。 Thats all.就这样。

namespace CamTest
{
    class Camera
    {
        public void funCForBoOut_SelectedIndexChanged()
        {
            ///Declaration
            int num = cboOut.SelectedIndex;
            cmd = 0x101E;
            len = 0x0001;

            ///Code
            if (num == 0)
            {
                val = 0x03;
            }
            else if (num == 1)
            {
                val = 0x02;
            }
            else if (num == 2)
            {
                val = 0x01;
            }
            else
            {
                val = 0x00;
            }

            ///Pack
            Packs(len, cmd, val);
        }
}

if you write your class in a dffrent namespace, you have to add "using " if your form class is written in CamTest namespace, you dont have to add it, this is just an example.如果您在 dffrent 命名空间中编写类,则必须添加“using”,如果您的表单类是在 CamTest 命名空间中编写的,则不必添加它,这只是一个示例。

using CamTest
namespace form
{
class form1
        {
            private void CboOut_SelectedIndexChanged(object sender, EventArgs e)
            {Camera.funCForBoOut_SelectedIndexChanged();}
        }

}

Every function that you want to call from outside your class must be declared with the public accessibility modifier.您要从类外部调用的每个函数都必须使用public可访问性修饰符声明。

From Microsoft's docs : 来自微软的文档

All types and type members have an accessibility level, which controls whether they can be used from other code in your assembly or other assemblies.所有类型和类型成员都有一个可访问性级别,该级别控制是否可以从程序集中的其他代码或其他程序集中使用它们。 You can use the following access modifiers to specify the accessibility of a type or member when you declare it:您可以在声明类型或成员时使用以下访问修饰符来指定它的可访问性:

public民众

The type or member can be accessed by any other code in the same assembly or another assembly that references it.同一程序集或引用它的另一个程序集中的任何其他代码都可以访问该类型或成员。

private私人的

The type or member can be accessed only by code in the same class or struct.类型或成员只能由同一类或结构中的代码访问。

Here I've modified your code accordingly:在这里,我相应地修改了您的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CamTest
{
    class Camera
    {
        public void CboOut_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboOut.SelectedIndex;
            cmd = 0x101E;
            len = 0x0001;

            ///Code
            if (num == 0)
            {
                val = 0x03;
            }
            else if (num == 1)
            {
                val = 0x02;
            }
            else if (num == 2)
            {
                val = 0x01;
            }
            else
            {
                val = 0x00;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void CboPol_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboPol.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1030;
            }
            else
            {
                cmd = 0x1031;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void CboAmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboAmp.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x102A;
            }
            else if (num == 1)
            {
                cmd = 0x102B;
            }
            else if (num == 2)
            {
                cmd = 0x102C;
            }
            else
            {
                cmd = 0x102D;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void CboPro_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboPro.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1017;
            }
            else
            {
                cmd = 0x1016;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void CboFlip_SelectedIndexChanged(object sender, EventArgs e)
        {
            ///Declaration
            int num = cboFlip.SelectedIndex;
            len = 0x0000;

            ///Code
            if (num == 0)
            {
                cmd = 0x1050;
            }
            else if (num == 1)
            {
                cmd = 0x1051;
            }
            else if (num == 2)
            {
                cmd = 0x1052;
            }
            else
            {
                cmd = 0x1053;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void chkboxDDE_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }
            else
            {
                cmd = 0x1018;
                len = 0x0000;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void TrkbarFine_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }

            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void TrkbarCoarse_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            byte lit_val, lar_val;
            cmd = 0x1019;
            len = 0x0001;

            ///Code
            if (chkboxDDE.Checked)
            {
                lit_val = (byte)trkbarFine.Value;
                lar_val = (byte)trkbarCoarse.Value;
                val = (byte)((lar_val << 4) + lit_val);
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void chkboxMan_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            cmd = 0x1020;
            len = 0x0004;

            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                cmd = 0x1021;
                len = 0x0000;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void TrkbarCon_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            cmd = 0x1020;
            len = 0x0004;

            ///Code
            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void TrkbarBright_Scroll(object sender, EventArgs e)
        {
            ///Declaration
            uint gain, offset;
            len = 0x0004;
            cmd = 0x1020;

            ///Code
            if (chkboxMan.Checked)
            {
                gain = (uint)trkbarCon.Value;
                offset = (uint)trkbarBright.Value;
                val = (gain << 16) + offset;
            }
            else
            {
                return;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void chkboxCursor_CheckedChanged(object sender, EventArgs e)
        {
            ///Declaration
            len = 0x0001;
            cmd = 0x1034;

            ///Code
            if (chkboxCursor.Checked)
            {
                val = 0x01;
            }
            else
            {
                val = 0x00;
            }

            ///Pack
            Packs(len, cmd, val);
        }

        public void SendPosition(int Ypos, int Xpos)
        {
            ///Declaration
            cmd = 0x1035;
            len = 0x0004;

            ///Code
            val = (uint)((Ypos << 16) + Xpos);

            ///Pack
            Packs(len, cmd, val);
        }

        public void btnTt_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if ((y - 16) < 0)
            {
                return;
            }
            else
            {
                y -= 16;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        public void BtnT_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if (y <= 0)
            {
                return;
            }
            else
            {
                y--;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        public void BtnD_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if (y >= (height - 1))
            {
                return;
            }
            else
            {
                y++;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        public void BtnDd_Click(object sender, EventArgs e)
        {
            int y;

            y = int.Parse(txtboxY.Text);

            if ((y + 16) > (height - 1))
            {
                return;
            }
            else
            {
                y += 16;
                txtboxY.Text = y.ToString();
            }

            int x = int.Parse(txtboxX.Text);
            SendPosition(y, x);
        }

        public void BtnL_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if (x <= 0)
            {
                return;
            }
            else
            {
                x--;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        public void BtnLl_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if ((x - 16) < 0)
            {
                return;
            }
            else
            {
                x -= 16;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        public void BtnR_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if (x >= (width - 1))
            {
                return;
            }
            else
            {
                x++;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        public void BtnRr_Click(object sender, EventArgs e)
        {
            int x;

            x = int.Parse(txtboxX.Text);

            if ((x + 16) > (width - 1))
            {
                return;
            }
            else
            {
                x += 16;
                txtboxX.Text = x.ToString();
            }

            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }

        public void BtnMid_Click(object sender, EventArgs e)
        {
            txtboxX.Text = (width / 2).ToString();
            txtboxY.Text = (height / 2).ToString();

            int x = int.Parse(txtboxX.Text);
            int y = int.Parse(txtboxY.Text);
            SendPosition(y, x);
        }
    }
}

Only specify private if you only plan to call that function from within the same class.如果您只打算从同一类中调用该函数,则仅指定private Do note, however, that no accessibility modifier means that it's private.但是请注意,没有可访问性修饰符意味着它是私有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM