在c#中自动检查checkboxlist中的每个新项目 [英] Automatically auto-check every new item in a checkedboxlist in c#

查看:16
本文介绍了在c#中自动检查checkboxlist中的每个新项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动检查我的复选框列表中的每个新项目我有一个按钮可以自动选择所有项目,但我们不希望它以这种方式由按钮控制.我们希望它为我们自动获得的每一个新项目都会被检查.

I am trying to auto check every new item in my checkedboxlist I have a button that does that it will auto select all, but we don't want it that way to be controlled by a button. we want it so for every new item we get automatically will get checked.

这是我的按钮,如果有新的 20 个项目,它会自动全选,这将自动全选,然后提交这些新项目

This is my button that auto select all if there are new 20 items this will auto select all and then submit those new items

这是它的工作代码,但不是我想要自动选择每个进入的新项目,因为我有另一个进程会不断向复选框列表添加新项目,而且 lst_BarcodeScanEvents 是复选框列表名称

Here is the code it works but is not I want I want to auto select for every new items coming in, because I have another process that will keep adding new items to the checkedboxlist, also the lst_BarcodeScanEvents is the checkedboxlist name

private void btn_SelectALLScans_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < lst_BarcodeScanEvents.Items.Count; i++)
            {
                lst_BarcodeScanEvents.SetItemChecked(i, true);
            }
        }

我检查了其他来源,如 google 和 stackoverflow,但在这里找不到任何符合我要求的内容.

I checked other sources like google and stackoverflow but I could not find anything to my request here.

这里是我有按钮逻辑的主类,checkedlistbox 等等.按钮方法 btn_ConnectT_Click 调用第二个类的方法

Here is the main class where I have the logic of buttons, checkedlistbox and more. and the button method btn_ConnectT_Click calls the methods from the second class

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Web;
using BarcodeReceivingApp.Core.Domain;
using BarcodeReceivingApp.Functionality;

namespace BarcodeReceivingApp
{
    public partial class BarcodeReceivingForm : Form
    {
        //GLOBAL VARIABLES
        private const string Hostname = "myip";
        private const int Port = 23;
        private TelnetConnection _connection;
        private ParseReceivingBarcode _parseReceivingBarcode;
        private ButtonsDisplay _buttonsDisplay;


        public BarcodeReceivingForm()
        {
            InitializeComponent();
            //FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            WindowState = FormWindowState.Maximized;
        }

        private void btn_ConnectT_Click(object sender, EventArgs e)
        {
            _connection = new TelnetConnection(Hostname, Port);
            _connection.ServerSocket(Hostname, Port, this);
        }

        private void btn_StopConnection_Click(object sender, EventArgs e)
        {
            _connection.Exit();
        }

        private void btn_RemoveItemFromListAt_Click(object sender, EventArgs e)
        {
            if (lst_BarcodeScanEvents.CheckedItems.Count != 0)
                for (var i = lst_BarcodeScanEvents.CheckedItems.Count; i > 0; i--)
                    lst_BarcodeScanEvents.Items.RemoveAt(lst_BarcodeScanEvents.CheckedIndices[i - 1]);
            else
                MessageBox.Show(@"Element(s) Not Selected...");
        }

        private void BarcodeReceivingForm_Load(object sender, EventArgs e)
        {
            _buttonsDisplay = new ButtonsDisplay(this);
            _buttonsDisplay.ButtonDisplay();
        }

        private void btn_ApplicationSettings_Click(object sender, EventArgs e)
        {
            var bcSettingsForm = new BarcodeReceivingSettingsForm();
            bcSettingsForm.Show();
        }

        private void btn_ClearBarcodeList_Click(object sender, EventArgs e)
        {
            lst_BarcodeScanEvents.Items.Clear();
        }

        private void lst_BarcodeScanEvents_ItemAdded(object sender, ListBoxItemEventArgs e)
        {
            MessageBox.Show(@"Item was added at index " + e.Index + @" and the value is " + lst_BarcodeScanEvents.Items[e.Index].ToString());                          
        }

        private void btn_SubmitData_Click(object sender, EventArgs e)
        {
            var receivingFullBarcode = new List<string>();
            _connection.GetBarcodeList();
        }

        private void btn_SelectALLScans_Click(object sender, EventArgs e)
        {
            for (var i = 0; i < lst_BarcodeScanEvents.Items.Count; i++)
            {
                lst_BarcodeScanEvents.SetItemChecked(i, true);
            }
        }

    }
}

这里是第二个类,我使用 telnet 端口 23 扫描条形码并将其放入 checklistbox,现在这里将数据插入到 checklistbox 的主要方法是方法 serversocket 和 readwrite 方法

Here is the second class where I use a telnet port 23 that scan barcodes where and puts it to the checkedlistbox, now the main emethods here that inserts the data to the checkedlistbox is the method serversocket and the readwrite method

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace BarcodeReceivingApp.Functionality
{
    public class TelnetConnection
    {
        private Thread _readWriteThread;
        private TcpClient _client;
        private NetworkStream _networkStream;
        private string _hostname;
        private int _port;
        private BarcodeReceivingForm _form;
        private bool _isExiting = false;

        public TelnetConnection(string hostname, int port)
        {
            this._hostname = hostname;
            this._port = port;
        }

        public void ServerSocket(string ip, int port, BarcodeReceivingForm f)
        {
            this._form = f;
            try
            {
                _client = new TcpClient(ip, port);
            }
            catch (SocketException)
            {
                MessageBox.Show(@"Failed to connect to server");
                return;
            }
            _networkStream = _client.GetStream();
            _readWriteThread = new Thread(ReadWrite);
            //_readWriteThread = new Thread(() => ReadWrite(f));
            _readWriteThread.Start();
        }

        public void Exit()
        {
            _isExiting = true;
        }

        public void ReadWrite()
        {
            do
            {
                var received = Read();
                if (received == null)
                    break;

                if (_form.lst_BarcodeScanEvents.InvokeRequired)
                {
                    var received1 = received;
                    _form.lst_BarcodeScanEvents.Invoke(new MethodInvoker(delegate
                    {
                        _form.lst_BarcodeScanEvents.AddItem(received1 + Environment.NewLine);
                    }));
                }
            } while (!_isExiting);

            //var material = received.Substring(10, 5);
            //_form.label5.Text += string.Join(Environment.NewLine, material);

            CloseConnection();

        }

        public List<string> GetBarcodeList()
        {
            var readData = new List<string>();

            foreach (string list in _form.lst_BarcodeScanEvents.Items)
            {
                readData.Add(list);
                MessageBox.Show(list);
            }

            return readData;
        }
        public string Read()
        {
            var data = new byte[1024];
            var received = "";

            var size = _networkStream.Read(data, 0, data.Length);
            if (size == 0)
                return null;

            received = Encoding.ASCII.GetString(data, 0, size);

            return received;
        }

        public void CloseConnection()
        {
            MessageBox.Show(@"Closed Connection",@"Important Message");
            _networkStream.Close();
            _client.Close();
        }
    }
}

所以现在就像我之前所说的,对于它插入到checkedlistbox 的每个新项目,我希望它在每次向checkedlistbox 添加新数据时自动选择.

so now like I said before for every new items it gets inserted to the checkedlistbox I want it to be auto selected everytime it adds a new data to the checkedlistbox.

推荐答案

设置 CheckedListBox 新添加项的 Checked 状态的问题,通常很简单, 由于所讨论的 Listbox 的性质而有些复杂.

The problem, usually simple, of setting the Checked state of a newly added Item of a CheckedListBox, was somewhat complicated because of the nature of the Listbox in question.

CheckedListBox 是具有自定义事件和属性的自定义控件.
它实际上是一种相对常见的自定义(可以在此 MSDN 论坛帖子中看到一个实现:
在添加或删除项目时让 ListBox 有一个事件?)但它可能会使事件的解释复杂化.

The CheckedListBox is a Custom Control with custom events and properties.
It's actually a relatively common customization (one implementation can be see in this MSDN Forum post:
Have ListBox an event when addid or removing Items?) but it might complicate the interpretation of the events.

自定义控件在向列表中添加新项(来自承载控件的窗体以外的类)时,引发自定义 ItemAdded 事件

The Custom Control, when adding a new Item to the List (from a class other than the Form that hosts the Control), raises the custom ItemAdded event

private void [CheckedListBox]_ItemAdded(object sender, ListBoxItemEventArgs e)

添加的项目的索引由自定义 ListBoxItemEventArgs 对象的 e.Index 属性引用.
通过提供的代码,可以确定这个事件处理程序是自然可以设置新Item的Checked状态的地方:

The Index of the Item added is referenced by the e.Index property of the custom ListBoxItemEventArgs object.
With the code provided, it can be determined that this event handler is the natural place where the Checked state of the new Item can be set:

private void lst_BarcodeScanEvents_ItemAdded(object sender, ListBoxItemEventArgs e)
{
    lst_BarcodeScanEvents.SetItemChecked(e.Index, true);
}

这篇关于在c#中自动检查checkboxlist中的每个新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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