matlab 中listbox 增加或删除 选中的项目!

matlab 中listbox 增加或删除 选中的项目!,第1张

这是删除:
n=get(handleslistbox,'value'); %获取选中的行号
str=get(handleslistbox,'string'); %获取列表中的所有数据
m=size(str1,1);
k=0;
for i=1:m
for j=1:size(n,2)
if(i~=n(j))
k=k+1;
str1(k,:)=str(i,:);
end
end
end
set(handleslistbox,'string',str1);
添加的话稍微发杂,需要d出一个类似的listbox作为总表,从总表里选,一种做法如下:
再建一个子fig,通过主fig上的add按钮来激发它,在子fig上设置一个listbox和一个pushbutton。将选中的选项添加到主fig的listbox中就行了。两个fig间的互相调用通过句柄的获取和定义来实现,不清楚的话再说。

1)新建一个“Windows 窗体应用程序”

2)在Form1上布置一个ListBox,一个TextBox,一个Button

3)对button1的Click事件编程

using System;
using SystemDrawing;
using SystemWindowsForms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Size size;
        public Form1()
        {
            InitializeComponent();
      
         }
        private void button1_Click(object sender, EventArgs e)
        {
            // 如果textBox1为空,则退出
            if (textBox1TextTrim() == "") return;
            // 否则,在listBox1中添加一个项目
            listBox1ItemsAdd(textBox1TextTrim());
            // 选中listBox1中新添加的项目
            listBox1SelectedIndex = listBox1ItemsCount - 1;
        }
    }
}

简单的来说就是监控,定位+保持比例。

例如界面分为左右下三部分,其中下部分最好解决。使用Dock属性直接定位到Bottom。难点就是上面的左右两块。

我的做法是:外面套一层,然后分割为左右两部分。这里采用TableLayoutPanel为例。将TableLayoutPanel调整为一行两列左右各占50%的单元格形式。

并将TableLayoutPanel的Dock属性调整为Fill。这样两个控件就会各占上下两部分。

这时候可以运行,会发现无论窗体大小怎么变,控件都是会自动调整。不会死板板的。

下面是重点:

放两个需要测试的控件分别到TableLayoutPanel的两个单元格中。我这里放的是ListBox。然后将两个ListBox的Anchor属性都设为Top,Bottom,Left,Right。既上下左右。最后调整两个ListBox到合适大小。好了,完事。。。

下面贴出的是Designer部分的代码。直接粘贴进去看效果吧

#region Windows 窗体设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            thislstBottom = new SystemWindowsFormsListBox();
            thistlpSpilt = new SystemWindowsFormsTableLayoutPanel();
            thislstRight = new SystemWindowsFormsListBox();
            thislstLeft = new SystemWindowsFormsListBox();
            thistlpSpiltSuspendLayout();
            thisSuspendLayout();
            // 
            // lstBottom
            // 
            thislstBottomDock = SystemWindowsFormsDockStyleBottom;
            thislstBottomFont = new SystemDrawingFont("微软雅黑", 10F, SystemDrawingFontStyleRegular, SystemDrawingGraphicsUnitPoint, ((byte)(134)));
            thislstBottomFormattingEnabled = true;
            thislstBottomHorizontalExtent = 2000;
            thislstBottomHorizontalScrollbar = true;
            thislstBottomItemHeight = 19;
            thislstBottomItemsAddRange(new object[] {
            "底"});
            thislstBottomLocation = new SystemDrawingPoint(0, 296);
            thislstBottomName = "lstBottom";
            thislstBottomSize = new SystemDrawingSize(849, 156);
            thislstBottomTabIndex = 12;
            // 
            // tlpSpilt
            // 
            thistlpSpiltColumnCount = 2;
            thistlpSpiltColumnStylesAdd(new SystemWindowsFormsColumnStyle(SystemWindowsFormsSizeTypePercent, 50F));
            thistlpSpiltColumnStylesAdd(new SystemWindowsFormsColumnStyle(SystemWindowsFormsSizeTypePercent, 50F));
            thistlpSpiltControlsAdd(thislstRight, 0, 0);
            thistlpSpiltControlsAdd(thislstLeft, 0, 0);
            thistlpSpiltDock = SystemWindowsFormsDockStyleFill;
            thistlpSpiltLocation = new SystemDrawingPoint(0, 0);
            thistlpSpiltName = "tlpSpilt";
            thistlpSpiltRowCount = 1;
            thistlpSpiltRowStylesAdd(new SystemWindowsFormsRowStyle(SystemWindowsFormsSizeTypePercent, 50F));
            thistlpSpiltSize = new SystemDrawingSize(849, 296);
            thistlpSpiltTabIndex = 13;
            // 
            // lstRight
            // 
            thislstRightAnchor = ((SystemWindowsFormsAnchorStyles)((((SystemWindowsFormsAnchorStylesTop | SystemWindowsFormsAnchorStylesBottom) 
            | SystemWindowsFormsAnchorStylesLeft) 
            | SystemWindowsFormsAnchorStylesRight)));
            thislstRightFont = new SystemDrawingFont("微软雅黑", 10F, SystemDrawingFontStyleRegular, SystemDrawingGraphicsUnitPoint, ((byte)(134)));
            thislstRightFormattingEnabled = true;
            thislstRightItemHeight = 19;
            thislstRightItemsAddRange(new object[] {
            "右"});
            thislstRightLocation = new SystemDrawingPoint(427, 3);
            thislstRightName = "lstRight";
            thislstRightSize = new SystemDrawingSize(419, 289);
            thislstRightTabIndex = 6;
            // 
            // lstLeft
            // 
            thislstLeftAnchor = ((SystemWindowsFormsAnchorStyles)((((SystemWindowsFormsAnchorStylesTop | SystemWindowsFormsAnchorStylesBottom) 
            | SystemWindowsFormsAnchorStylesLeft) 
            | SystemWindowsFormsAnchorStylesRight)));
            thislstLeftFont = new SystemDrawingFont("微软雅黑", 10F, SystemDrawingFontStyleRegular, SystemDrawingGraphicsUnitPoint, ((byte)(134)));
            thislstLeftFormattingEnabled = true;
            thislstLeftItemHeight = 19;
            thislstLeftItemsAddRange(new object[] {
            "左"});
            thislstLeftLocation = new SystemDrawingPoint(3, 3);
            thislstLeftName = "lstLeft";
            thislstLeftSize = new SystemDrawingSize(418, 289);
            thislstLeftTabIndex = 5;
            // 
            // Form2
            // 
            thisAutoScaleDimensions = new SystemDrawingSizeF(6F, 12F);
            thisAutoScaleMode = SystemWindowsFormsAutoScaleModeFont;
            thisClientSize = new SystemDrawingSize(849, 452);
            thisControlsAdd(thistlpSpilt);
            thisControlsAdd(thislstBottom);
            thisName = "Form2";
            thisStartPosition = SystemWindowsFormsFormStartPositionCenterScreen;
            thisText = "Demo";
            thistlpSpiltResumeLayout(false);
            thisResumeLayout(false);
        }
        #endregion
        private SystemWindowsFormsListBox lstBottom;
        private SystemWindowsFormsTableLayoutPanel tlpSpilt;
        private SystemWindowsFormsListBox lstRight;
        private SystemWindowsFormsListBox lstLeft;

import tkinter

win=tkinterTk()

wintitle('第一个窗口')

wingeometry()

'''

Listbox 列表框控件:可以包含一个或者多个文本框

作业:在listbox控件的小窗口显示一个字符串

'''

#一:支持鼠标移动选中位置使用selectmode=tkinterBROWSE,创建一个listbox,添加几个元素

'''

lb=tkinterListbox(win,selectmode=tkinterBROWSE)

lbpack()

for item in ['good','nice','beautiful']:

#1:添加

#按顺序添加(向后添加)

    lbinsert(tkinterEND,item) 

#在开始添加(往前添加)

lbinsert(tkinterACTIVE,'cool') 

#将列表当成一个元素添加

#lbinsert(tkinterEND,['cool','sunk']) 

'''

#2:删除索引从0开始数(删除开始索引到结束索引的所有元素),参数1为开始索引,参数2为结束索引,如果不指定参数2,只删除第一个索引的内容

#lbdelete(1)

#3:选中索引从0开始数(选中开始索引到结束索引的所有元素),参数1为开始索引,参数2为结束索引,如果不指定参数2,只选中第一个索引的内容

#lbselect_set(2)

#lbselect_set(2,4)

#4:取消选中索引从0开始数(取消选中开始索引到结束索引的所有元素),参数1为开始索引,参数2为结束索引,如果不指定参数2,只取消选中第一个索引的内容

#lbselect_clear(2)

#5:获取到列表中的元素个数

#print(lbsize())

#6:从列表中取值索引从0开始数(获取选中开始索引到结束索引的所有元素的值(tuple)),参数1为开始索引,参数2为结束索引,如果不指定参数2,只获取第一个索引的内容

#print(lbget(1,2))

#7:返回当前选中的索引项(得到索引),不是的到内容

#print(lbcurselection())

#8:判断 一个选项是否被选中,返回布尔类型 用索引表示 

#print(lbselect_includes(1))

#9:绑定变量

#lbv=tkinterStringVar()

#   91 打印当前列表中的所有选项(tuple)

#print(lbvget())

#   92 设置选项(tuple)

#lbvset(('1','2'))

#10:绑定事件(需要一个参数,但是不要传)

'''

def myPrint(event):

    print(lbget(lbcurselection()))

lbbind('<Double-Button-1>',myPrint)

#11:滚动条

sc=tkinterScrollbar(win)

scpack(side=tkinterRIGHT,fill=tkinterY)

lbpack(side=tkinterLEFT,fill=tkinterBOTH)

#关联

lbconfigure(yscrollcommand=scset)  (config方法也可以)

sc['command']=lbyview #额外给属性赋值,相当于 scconfigure(command=textyview)

'''

'''#二:不支持鼠标移动选中位置,支持鼠标按下后选中位置使用selectmode=tkinterSINGLE,创建一个listbox,添加几个元素,与(一)相似

lb=tkinterListbox(win,selectmode=tkinterSINGLE,listvariable=lbv)

lbpack()

for item in ['good','nice','beautiful']:

    lbinsert(tkinterEND,item)

'''

#三:在一的功能上,可以使listbox支持shift和control(实现连选与多选)使用selectmode=tkinterEXTENDED

'''

lb=tkinterListbox(win,selectmode=tkinterEXTENDED)

for item in ['good','nice','beautiful','good1','nice1','beautiful1','good2','nice2','beautiful2','good3','nice3','beautiful3']:

    lbinsert(tkinterEND,item)

sc=tkinterScrollbar(win)

scpack(side=tkinterRIGHT,fill=tkinterY)

lbconfigure(yscrollcommand=scset)

lbpack(side=tkinterLEFT,fill=tkinterBOTH)

sc['command']=lbyview

'''

#四:在二的功能上,支持多选使用selectmode=tkinterMULTIPLE

lb=tkinterListbox(win,selectmode=tkinterMULTIPLE)

for item in ['good','nice','beautiful','good1','nice1','beautiful1','good2','nice2','beautiful2','good3','nice3','beautiful3']:

    lbinsert(tkinterEND,item)

lbpack()

winmainloop()

style="height:80px;"加入到HTML里面,设置下吧。
把你要显示的东西先隐藏啊。visible=false;点击按钮再变成visible=true,不就成了。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/13382061.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-31
下一篇2025-08-31

发表评论

登录后才能评论

评论列表(0条)

    保存