C# ListBox中添加CheckBox并遍历添加的CheckBox

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/github_39611196/article/details/90171478

在使用ListBox时,有时需要动态的添加CheckBox,并在添加后根据CheckBox的选中情况执行相应的事件。
首先是添加的代码:

DirectoryInfo TheFolder = new DirectoryInfo("G:/proj/ActiveDemoEarth/WindowsFormsApplication1/bin/x64/Release/line/");
            //遍历文件
            int x1 = this.listBox1.Location.X;
            int y1 = 10;

            int y2 = 10;

            int y3 = 10;
            CheckBox checkbox = new CheckBox();
            checkbox.Font = new System.Drawing.Font("宋体", 10F);
            checkbox.Location = new Point(x1 + 30, y1);
            y1 += 30;
            checkbox.Name = NextFile.Name;
           checkbox.Text = "无人机" + uav_nums.ToString();
           uav_nums += 1;
           this.listBox1.Controls.Add(checkbox);

遍历添加后的CheckBox的代码:

 foreach(var control in listBox1.Controls)
                {
                    CheckBox checkbox = (CheckBox)control;
                    checkbox.CheckState = CheckState.Checked;
                }
展开阅读全文

NSTableView添加CheckBox

10-16

如题 见下图效果 我在表中添加CheckBox之后 我选择CheckBox之后我要怎么知道选择的时哪一行 rnrn表格这样实现的rn[code=objc]rn- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowrnrn NSTableCellView *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];rn NSButton *cell1 = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];rn CompSameTimeDifSensorModel *comp = dataArray[row];rn if ([tableColumn.identifier isEqualToString:@"Selected"]) rn [cell1 setTag:row];rn [cell1 setTarget:self];rn [cell1 setAction:@selector(cellClick:)];rn rn [cell1 setState:0];rn return cell1;rn rn else if ([tableColumn.identifier isEqualToString:@"SensorID"]) rn cell.textField.stringValue = comp.SensorID;rn rn else if ([tableColumn.identifier isEqualToString:@"MinRecordTime"]) rn NSString *timeStr = [NSString stringWithFormat:@"%f",comp.MinRecordTime];rn NSDateFormatter *formatter = [[NSDateFormatter alloc] init];rn [formatter setTimeStyle:NSDateFormatterShortStyle];rn [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];rn NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeStr integerValue]/1000.0];rn cell.textField.stringValue = [formatter stringFromDate:confromTimesp];rn rn else if ([tableColumn.identifier isEqualToString:@"MaxRecordTime"]) rn NSString *timeStr = [NSString stringWithFormat:@"%f",comp.MaxRecordTime];rn NSDateFormatter *formatter = [[NSDateFormatter alloc] init];rn [formatter setTimeStyle:NSDateFormatterShortStyle];rn [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];rn NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeStr integerValue]/1000.0];rn cell.textField.stringValue = [formatter stringFromDate:confromTimesp];rn rn return cell;rnrn[/code]rnrn我给每个CheckBox添加了个方法,并赋值tag为行号,但是方法里面得不到CheckBox的tag且都为0,这是怎么回事rn[code=objc]rn- (void)cellClick:(NSButton *)senderrnrn if(sender != nil)rn NSButton* cell = (NSButton *)[self.tableView selectedCell];rn if([cell state] == 1)rn [cell setState:0];rn else rn [cell setState:1];rn rn NSLog(@"cellClick = %ld",[[self.tableView selectedCell] tag]);rn rnrn[/code]rnrn[code=objc]rn- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)rowrn[/code]rn这个代理方法是选择当前行才会调用 如果选择当前行里面的CheckBox则不会调用rnrn那该怎么实现呢,效果图及tableview结构如下rn[img=https://img-bbs.csdn.net/upload/201710/16/1508141008_734403.png][/img]rn[img=https://img-bbs.csdn.net/upload/201710/16/1508141021_933807.png][/img] 论坛

没有更多推荐了,返回首页