简体   繁体   English

当我单击 C# Windows 表单中的按钮时,如何从来自文本框的另一个 class 中获取值?

[英]how can I reach the value from another class coming from textbox when I click the button in C# Windows form?

I am trying to write a code for changing contrast.我正在尝试编写更改对比度的代码。 I create a class for this but I should get the value from texbox and write the value for the process in Contrast.cs but I could not get that.我为此创建了一个 class 但我应该从 texbox 获取值并将该过程的值写入Contrast.cs但我无法得到。 How can I send the value to the Contrast.cs when I click to the btn_contrast ?当我单击btn_contrast时,如何将值发送到Contrast.cs

PS:There are some instances like for sending it from form to another form but I could not find any instance about sending it to class and using it. PS:有一些例子,比如将它从一个表单发送到另一个表单,但我找不到任何关于将它发送到 class 并使用它的实例。

Here is the code for Form1:这是Form1的代码:

using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace imageprocessing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public FilterInfoCollection devices;
        public VideoCaptureDevice camera;
        private void Form1_Load(object sender, EventArgs e)
        {
            devices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            foreach (FilterInfo item in devices)
            {
                cb_show.Items.Add(item.Name);

            }
            camera = new VideoCaptureDevice();
            cb_show.SelectedIndexChanged += cb_show_SelectedIndexChanged;
        }
        private void cb_show_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (camera.IsRunning == false)
                {
                    camera = new VideoCaptureDevice(devices[cb_show.SelectedIndex].MonikerString);
                    camera.NewFrame += Camera_NewFrame;
                    camera.Start();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message + "");
            }
        }
        List<byte> alldata = new List<byte>();
        public void Camera_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            Bitmap image = (Bitmap)eventArgs.Frame.Clone();
            byte[] maindata = new byte[image.Width * 3];

            if (btn_grayWasClicked == true)
            {
                alldata.Clear();
                for (int i = 0; i < image.Height; i++)
                {
                    int count = 0;
                    for (int j = 0; j < image.Width; j++)
                    {
                        Color color = image.GetPixel(j, i);
                        maindata[count] = color.R;
                        maindata[count + 1] = color.G;
                        maindata[count + 2] = color.B;
                        count += 3;
                    }
                    byte[] processGray = Gray.GrayFilter(maindata, image.Width);
                    alldata.AddRange(processGray);
                }
            }
           
        }
        private bool btn_contrastWasClicked = false;

        private void btn_contrast_Click(object sender, EventArgs e)
        {
            btn_contrastWasClicked = true;
        }
     

Here is the code for Contrast.cs这是 Contrast.cs 的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace imageprocessing
{
    class Contrast
    {
        public static byte[] Contrast(byte[] data, int width)
        {
            List<byte> alldataa = new List<byte>();
            double cont =(100.0 + (Convert.ToInt32()-100))/100.0)*(100.0 + (Convert.ToInt32() - 100)) / 100.0);
            for (int i = 0; i < width - 1; i += 3)
            {
                data[i]=((((data[i]/255)-0.5)*cont)+0.5)*255;
                if (data[i]<0)
                {
                    data[i] = 0;
                }
                if(data[i]>255)
                {
                    data[i] = 255;
                }
            }
            return data;
        }
    }
}

I need the value that is coming from tb_contrast in this code:我需要此代码中来自 tb_contrast 的值:

double cont =(100.0 + (Convert.ToInt32()-100))/100.0)*(100.0 + (Convert.ToInt32() - 100)) / 100.0);

Keep the value in Session , then use it wherever you want.保留Session中的值,然后在任何地方使用它。

First Make your class public and the create a object for your class in Form1 like below:首先public您的 class 并在 Form1 中为您的 class 创建一个 object,如下所示:

Contrast con = new Contrast();

now you can call your class methods wherever you wan.现在您可以在任何地方调用您的 class 方法。

private void btn_contrast_Click(object sender, EventArgs e)
        {
            btn_contrastWasClicked = true;
            con.Contrast('SomeVal','Your Required TextBoxValue');
        }

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

相关问题 如何在C#中使用linq查询将文本框中的值与字典进行匹配,并在按钮单击时在另一个文本框中显示它 - How to match value from textbox to dictionary and display it in another textbox on button click with linq query in C# 如何使用C#将数据从其他表单调用到文本框? - How can I call the data from another form to a textbox using C#? 如何将文本从表单返回到文本框C#? - How can I return text from a form to a textbox C#? C#:如何将文本附加到类中的表单上的文本框? - C#: How can I append text to a textbox on a form from a class? C#如何从另一个类调整表单中的textbox属性? - C# how to adjust textbox property in a form from another class? 我如何从另一个类调用按钮单击方法 - how can i call a button click method from another class 如何在C#中将文件/文件路径从一个Button_Click事件传递到另一个事件? - How can I pass a file/file path from one Button_Click event to another in C#? 我想从C#Windows窗体按钮单击的Chrome和IE中的所有选项卡中获取URL - I want to get the url from all tabs in Chrome and IE from C# windows form button click 如何通过单击按钮运行c#代码? - How can I run c# code from a button click? 如何在C#中的另一个类中找到表单 - How do I find a Form from another class in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM