introduction

  • 更多控件用法,请参考 here 和 源码。
  • 本文的代码基于这里
  • 本文将演示listbox的添加,删除,删除选中项,添加到指定位置等常用功能。

xml文件添加代码

基于上一篇, 继续向basic.xml中添加下面关于ListBox的代码。 xml完整源码在文末。

<HBox>
<!-- List -->
<VListBox class="list" name="list" padding="5,3,5,3">
</VListBox>
<VBox> <!-- Buttons -->
<CheckBox class="checkbox_font12" name="list_checkbox_add_to_top" text="add to top" margin="0,5,0,10"/>
<Button class="btn_global_blue_80x30" name="list_btn_add" text="add" /> <CheckBox class="checkbox_font12" name="list_checkbox_remove_all" text="del all?" margin="0,5,0,10"/>
<Button class="btn_global_white_80x30" name="list_btn_remove" text="remove"/>
</VBox>
</HBox>

上面的代码创建了一个水平容器,容器从左往右分别是:listbox和一个垂直容器,垂直容器中从上到下是复选框和按钮。效果如下:

代码中关联

BasicForm.h

  • 打开BasicForm.h,类中添加下面的代码用于关联界面控件。
	// list
ui::ListBox *plist_;
// list的删除和添加按钮
ui::Button *plist_btn_arr_[2];
// list 的复选框,
ui::CheckBox *plist_cb_arr_[2];

同时,类中再额外添加2个函数,用于监听Addremove的点击。

	// list的删除和添加
bool OnListBoxAddItem(ui::EventArgs* msg);
bool OnListBoxRemoveItem(ui::EventArgs* msg);

BasicForm.cpp

InitWindow函数

  • 转到BasicForm.cpp,找到 InitWindow 函数,向其增加下面的代码
void BasicForm::InitWindow()
{
......
// 4.list控件
//----------------------------------------------------------------------------------------
plist_ = dynamic_cast<ui::ListBox*>(FindControl(L"list"));
if (plist_)
{
for (auto i = 0; i < 15; ++i)
{
ui::ListContainerElement* pelement = new(std::nothrow) ui::ListContainerElement;
if (pelement)
{
// 设置item显示的内容
pelement->SetText(nbase::StringPrintf(L"%d", i));
// 设置item的样式,可以在global.xml中找到
pelement->SetClass(L"listitem");
pelement->SetFixedHeight(20);
plist_->Add(pelement);
}
}
} // list关联的删除和添加按钮
plist_btn_arr_[0] = dynamic_cast<ui::Button*>(FindControl(L"list_btn_add"));
plist_btn_arr_[1] = dynamic_cast<ui::Button*>(FindControl(L"list_btn_remove")); if (plist_btn_arr_[0])
plist_btn_arr_[0]->AttachClick(nbase::Bind(&BasicForm::OnListBoxAddItem, this, std::placeholders::_1)); if (plist_btn_arr_[1])
plist_btn_arr_[1]->AttachClick(nbase::Bind(&BasicForm::OnListBoxRemoveItem, this, std::placeholders::_1)); // list 关联checkbox
plist_cb_arr_[0] = dynamic_cast<ui::CheckBox*>(FindControl(L"list_checkbox_add_to_top"));
plist_cb_arr_[1] = dynamic_cast<ui::CheckBox*>(FindControl(L"list_checkbox_remove_all"));
}

OnListBoxAddItem

函数体代码如下

bool BasicForm::OnListBoxAddItem(ui::EventArgs* msg)
{
if (plist_)
{
static int count_start_15 = 15;
ui::ListContainerElement* pelement = new(std::nothrow) ui::ListContainerElement;
if (pelement)
{
// 设置item显示的内容
pelement->SetText(nbase::StringPrintf(L"%d", count_start_15));
// 设置item的样式,可以在global.xml中找到
pelement->SetClass(L"listitem");
pelement->SetFixedHeight(20); // 添加到最前面
if (plist_cb_arr_[0]->IsSelected())
plist_->AddAt(pelement, 0);
else
plist_->Add(pelement); ++count_start_15;
}
} return false;
}

OnListBoxRemoveItem

OnListBoxRemoveItem函数体如下:

bool BasicForm::OnListBoxRemoveItem(ui::EventArgs* msg)
{
if (plist_)
{
if (plist_cb_arr_[1])
{
if (plist_cb_arr_[1]->IsSelected())
{
int list_count = plist_->GetCount();
if (0 < list_count)
plist_->RemoveAll();
else
;
}
else
{
int index = plist_->GetCurSel();
// 没有选中,将返回-1
if (-1 != index)
{
plist_->RemoveAt(index);
}
else
{
;
}
}
}
else
{
;
} }
else
{
;
} return false;
}

运行结果

xml完整源码

<?xml version="1.0" encoding="UTF-8"?>
<Window size="800,400" caption="0,0,0,35">
<VBox bkcolor="bk_wnd_darkcolor">
<HBox width="stretch" height="35" bkcolor="bk_wnd_lightcolor">
<Control />
<Button class="btn_wnd_min" name="minbtn" margin="4,6,0,0" />
<Box width="21" margin="4,6,0,0">
<Button class="btn_wnd_max" name="maxbtn"/>
<Button class="btn_wnd_restore" name="restorebtn" visible="false"/>
</Box>
<Button class="btn_wnd_close" name="closebtn" margin="4,6,8,0"/>
</HBox> <!--下面是中间的控件-->
<VBox padding="30, 30, 30, 30" >
<HBox>
<VBox>
<!-- Buttons -->
<Button class="btn_global_blue_80x30" name="btn_blue" text="blue" />
<Button class="btn_global_white_80x30" name="btn_white" text="white"/>
<Button class="btn_global_red_80x30" name="btn_red" text="red"/>
</VBox> <!--checkbox-->
<VBox>
<CheckBox class="checkbox_font12" name="checkbox1" text="checkbox1" margin="0,5,0,10" selected="true"/>
<CheckBox class="checkbox_font12" name="checkbox2" text="checkbox2" margin="0,5,0,10"/>
<CheckBox class="checkbox_font12" name="checkbox3" text="checkbox3" margin="0,5,0,10"/>
</VBox> <!-- option-->
<VBox>
<Option class="circle_option_2" name="option1" group="option_group" text="option1" margin="0,3,0,10" selected="true"/>
<Option class="circle_option_2" name="option2" group="option_group" text="option2" margin="0,3,0,10"/>
<Option class="circle_option_2" name="option3" group="option_group" text="option3" margin="0,3,0,10"/>
</VBox> <HBox>
<!-- List -->
<VListBox class="list" name="list" padding="5,3,5,3">
</VListBox>
<VBox> <!-- Buttons -->
<CheckBox class="checkbox_font12" name="list_checkbox_add_to_top" text="add to top" margin="0,5,0,10"/>
<Button class="btn_global_blue_80x30" name="list_btn_add" text="add" /> <CheckBox class="checkbox_font12" name="list_checkbox_remove_all" text="del all?" margin="0,5,0,10"/>
<Button class="btn_global_white_80x30" name="list_btn_remove" text="remove"/>
</VBox>
</HBox> </HBox>
</VBox> <!--下面是中间的控件 结束-->
</VBox>
</Window>

nim_duilib(6)之listbox的更多相关文章

  1. nim_duilib(13)之添加fmt库

    introduction 习惯使用fmt库做字符串的格式化操作.尽管nim_duilib提供了类似的函数. 故项目demo_xml引入了外部库fmt framework.h中添加下面的以便使用fmt库 ...

  2. MVVM下listbox默认显示最后一行

    原文地址:http://stackoverflow.com/questions/16866309/listbox-scroll-into-view-with-mvvm public class Scr ...

  3. WPF 自定义列表筛选 自定义TreeView模板 自定义ListBox模板

    有很多项目,都有数据筛选的操作.下面提供一个案例,给大家做参考. 左侧是数据源,搜索框加TreeView控件,右侧是ListBox控件.在左侧数据列点击添加数据,然后点击确定,得到所筛选的数据. 下面 ...

  4. WPF制作的VS黑色风格的Listbox

    最近写的一个玩具,WPF写出来的东西还是挺好看的 style.xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/ ...

  5. jquery 双向select控件bootstrap Dual listbox

    http://www.cnblogs.com/hangwei/p/5040866.html       -->jquery 双向select控件bootstrap Dual listboxhtt ...

  6. C# 向listbox添加大量数据项的实践心得

    使用 ListBox.Items.Add 方法添加项时,可以使用 BeginUpdate 方法,以防止每次向列表添加项时控件都重新绘制 ListBox.完成向列表添加项的任务后,调用 EndUpdat ...

  7. 实现一个纵向排列的 ListBox ,并具有操作按钮

    需要实现的效果如下: 要想把 ListBox 的内容纵向显示很简单,只需把 ListBox 的内容控件为 WrapPanel 就可以了: <ListBox.ItemsPanel> < ...

  8. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  9. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

随机推荐

  1. 搜索工具Wox简单使用

    目录 下载安装 几个常用命令 自定义 Wox是快速搜索小工具,内置了everything(需要先安装),但比everything好用.不止是搜文件,网页.系统等都可以快速搜索,还可以自定义. 下载安装 ...

  2. FASTA/Q序列处理神器---seqkit

    该软件对于处理FASTA/Q十分方便,省去自己编写脚本 安装 1 conda install seqkit 使用 序列操作(seq) 1 ## 取方向序列 2 seqkit seq test.fa - ...

  3. Linux—ps -ef 命令输出信息的具体含义(显示所有正在运行的命令程序)

    linux 中使用 ps -ef 输出参数的具体含义 功能:显示所有正在运行的命令程序 UID: 说明该程序被谁拥有PID:就是指该程序的 IDPPID: 就是指该程序父级程序的 IDC: 指的是 C ...

  4. Linux-centos7设置静态IP地址

    参考:https://blog.csdn.net/sjhuangx/article/details/79618865

  5. Linux 中的五种 IO 模型

    Linux 中的五种 IO 模型 在正式开始讲Linux IO模型前,比如:同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一 ...

  6. MybatisPlus入门程序

    参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` ​ USE `mybatisplus` ​ CREATE TABLE `user ...

  7. Levenshtein莱文斯坦算法在项目中的应用

    简介 根据维基百科的描述,在信息理论.语言学和计算机科学中,莱文斯坦距离是一个测量两个序列之间差异的字符串度量.非正式地,两个单词之间的莱文斯坦距离是将一个单词改变为另一个单词所需的最小单字符编辑次数 ...

  8. 全网最详细的ReentrantReadWriteLock源码剖析(万字长文)

    碎碎念) 花了两天时间,终于把ReentrantReadWriteLock(读写锁)解析做完了.之前钻研过AQS(AbstractQueuedSynchronizer)的源码,弄懂读写锁也没有想象中那 ...

  9. day19 进程管理

    day19 进程管理 什么是进程,什么是线程 1.什么是程序 一般情况下,代码,安装包等全部都是应用程序 2.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进 ...

  10. Linux磁盘分区(三)之查看磁盘分区常用命令

    Linux磁盘分区(三)之查看磁盘分区常用命令转自https://blog.csdn.net/x356982611/article/details/77893264 1.df     df -T 总的 ...