首页 > 解决方案 > 在c#中自动自动检查checkboxlist中的每个新项目

问题描述

我正在尝试自动检查我的复选框列表中的每个新项目我有一个按钮,它会自动全选,但我们不希望它被按钮控制。我们想要它,因此对于我们自动获得的每个新项目都会进行检查。

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

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

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,但在这里找不到任何符合我要求的信息。

这是我有按钮、checkedlistbox 等逻辑的主类。按钮方法 btn_ConnectT_Click 调用第二类的方法

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 扫描条形码并将其放入checkedlistbox,现在将数据插入checkedlistbox的主要方法是方法serversocket和readwrite方法

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添加新数据时都会自动选择它。

标签: c#winformscheckedlistbox

解决方案


为此,我建议在表单的 InitializeComponent() 之后创建一个您订阅的事件,如下所示:

    public Form1()
    {
        InitializeComponent();
        lst_BarcodeScanEvents.ControlAdded += new ControlEventHandler(AddedNewSelect);
    }

    private void AddedNewSelect(object sender, ControlEventArgs e)
    {
        lst_BarcodeScanEvents.SetItemChecked(e.Control.TabIndex, true);
    }

推荐阅读