自动自动检查c#中复选框列表中的每个新项目>> [

问题描述 投票:1回答:2
我正在尝试自动检查我的复选框列表中的每个新项目,我有一个按钮会自动选择所有按钮,但是我们不希望这种方式由按钮来控制。我们需要它,因此对于我们自动获得的每个新项目,都将进行检查。

这是我的按钮,如果有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扫描条形码并将其放入清单列表,现在此处将数据插入清单列表的主要方法是方法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(); } } }

所以现在就像我之前说的,对于每个新项目,它都会插入到清单列表框中,因此我希望每次将新数据添加到清单列表框中时,都会自动选择它。

我正在尝试自动检查我的复选框列表中的每个新项目,我有一个按钮会自动选择所有按钮,但是我们不希望这种方式由按钮来控制。对于...

c# winforms checkedlistbox
2个回答
1
投票
由于所涉及的列表框的性质,设置Checked的新添加项的CheckedListBox状态的问题通常很简单,但有些复杂。

0
投票
为此,我建议您在表单的InitializeComponent()之后创建一个您订阅的事件,如下所示:
© www.soinside.com 2019 - 2024. All rights reserved.