VB combobox怎么实现可多选下拉列表中的内容?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB combobox怎么实现可多选下拉列表中的内容?相关的知识,希望对你有一定的参考价值。

想要实现在combobox下拉框可多选择几个条件,然后点查询时能查询出满足选择的几个条件,相当于OR的关系,只要满足几个条件中的一个就显示出来?

参考技术A 你用combobox,没这个属性的。可以考虑用listbox,这样才可以选择多个,将multiselect属性设置为2就好了。
combobox每次只能选择一个!
combobox是listbox和text的结合。
参考技术B 确定“combo1.additem
adodc1.recordset.fields("用户名").value”中(“用户名”)是你表中的字段名吗?或许改为(0)试试,貌似在你的combo1_clik中没有连接数据源吧!在连接数据源后一定要刷新adodc1.refresh。还有你的combo1中最开始没有任何内容单击是无效的。没见过单击combo之后又改变自身的字段值!(呵呵这样写倒是是可以的)你还是把事件放到form_laod里试试吧。检查我提到的几点小问题,有时候是自己疏忽了而已……
参考技术C 你是数据库查询吧,SQL之类的,很容易啊:
这很容易,要做几个combobox出来好了,里面是条件,比如
A大于0
,A等于0,A小于0
然后用selece
Case
combo1.text
判断一下。如:
dim
A
as
string
A
="select
*
form
XXX
where
1=1
and
"
selece
Case
combo1.text
Case
"A大于0"
A
=
A
&
"a>0"
Case
"A等于0"
A
=
A
&
"a=0"
...
select
end
下面比如B列判断之类的,到最后执行SQL查询命令,把结果显示出来就OK了。
参考技术D '举个简单的例子,如果Text1文本框中存在
Combo1内容或存在
Combo2内容或存在
Combo3内容那么Text1文本框显示ok
If
InStr(1,
Text1.Text,
Combo1.Text)
>
0
Or
InStr(1,
Text1.Text,
Combo2.Text)
>
0
Or
InStr(1,
Text1.Text,
Combo3.Text)
>
0
Then
Text1.Text
=
"ok"
End
If
'请参考。

java swing的可输入可多选的下拉框

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.CellRendererPane;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;
import javax.swing.plaf.metal.MetalComboBoxUI;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

public class MyJComboBox 
{
    public static void main(String[] args) 
    {
        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);

        ReJComboBox reJComboBox = new ReJComboBox(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "4", "5", "6", "1", "2", "3", "4", "5", "6" });

        reJComboBox.setPreferredSize(new Dimension(150, 26));
        reJComboBox.setBorder(BorderFactory.createLineBorder(Color.GREEN));
        reJComboBox.setEditable(true);
        jTextFieldall =(JTextField) reJComboBox.getEditor().getEditorComponent();

        frame.add(reJComboBox);
        frame.setVisible(true);
    }
    //************************重写JComboBox组件以便于直接使用
    protected static JTextField jTextFieldall; 
    public static class ReJComboBox extends JComboBox
    {
        protected Set set = new HashSet();
        private ReJComboBox(String[] string) 
        {
            super(string);
            setUI(new ReMetalComboBoxUI());
            setRenderer(new ReJCheckBox());          
        }
        public String Stringmanipulation() 
        {
            StringBuilder sb;
            String spliceRegex = ",";
            List list = new ArrayList(set);
            Collections.sort(list);
            sb = new StringBuilder();
            for (int i=0;i<list.size();i++)  
            {
                Object obj = getModel().getElementAt( Integer.parseInt(list.get(i).toString()));
                sb.append(obj == null ? "" : obj).append(spliceRegex);
            }
            if(sb.length()>1)
                sb.setLength(sb.length()-1);
            return sb.toString();
        }
    }
    public static class ReBasicComboPopup extends BasicComboPopup 
    {
        public ReBasicComboPopup(JComboBox combo) 
        {
            super(combo);
        }
        protected void configureList() 
        {
            super.configureList();
            list.setSelectionModel(new DefaultListSelectionModel() {public boolean isSelectedIndex(int index) 
            {
                return ((ReJComboBox) comboBox).set.contains(Integer.valueOf(index));
            }});
        }
        protected MouseListener createListMouseListener() 
        {
                      return new ReMouseAdapter(list,comboBox);
        }   
    }
    //************************重写鼠标监听事件
    private static class ReMouseAdapter extends MouseAdapter 
    {
        protected  JList jList; 
        protected JComboBox jComboBox;
        private ReMouseAdapter(JList list, JComboBox comboBox) 
        {
            this.jList = list;
            this.jComboBox = comboBox;
        }
        public void mousePressed(MouseEvent anEvent)
        {
            int index = jList.getSelectedIndex();
            ReJComboBox reJComboBox = (ReJComboBox) jComboBox;
            if (jList.getSelectionMode() == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION)
                if (reJComboBox.set.contains(Integer.valueOf(index)))
                    reJComboBox.set.remove(Integer.valueOf(index));
                else
                    reJComboBox.set.add(Integer.valueOf(index));
            else
                if (!reJComboBox.set.contains(Integer.valueOf(index)))
                {
                    reJComboBox.set.clear();
                    reJComboBox.set.add(Integer.valueOf(index));
                }
                jComboBox.repaint();
                jList.repaint();
        }
    }
    //************************重写MetalComboBoxUI弹出框
    public static class ReMetalComboBoxUI extends MetalComboBoxUI
    {
        public void paint( Graphics g, JComponent c ) 
        {
            Rectangle r = rectangleForCurrentValue();
            paintCurrentValueBackground(g,r,hasFocus);
            paintCurrentValue(g,r,hasFocus);
        }
        public void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus) 
        {
            JTextField jTextField = new JTextField();
            jTextField.setText(((ReJComboBox) comboBox).Stringmanipulation());
            jTextField.setFont(comboBox.getFont());
            jTextField.setOpaque(comboBox.isOpaque());
            if (comboBox.getRenderer() instanceof JComponent) 
            {
                JComponent r = (JComponent) comboBox.getRenderer();
                jTextField.setBorder(r.getBorder());
            }
            int x = bounds.x, y = bounds.y, w = bounds.width, h = bounds.height;
            if (padding != null) 
            {
                x = bounds.x + padding.left;
                y = bounds.y + padding.top;
                w = bounds.width - (padding.left + padding.right);
                h = bounds.height - (padding.top + padding.bottom);
            }
            new ReCellRendererPane(currentValuePane).paintComponent(g, jTextField, comboBox, x, y, w, h, true);           
        }
        protected ComboPopup createPopup() 
        {
            return new ReBasicComboPopup(comboBox);
        }
    }
    //************************重写录入框
    public static class ReCellRendererPane extends CellRendererPane 
    {
        protected CellRendererPane cellRendererPane;
        private ReCellRendererPane(CellRendererPane cellRendererPane) 
        {
            this.cellRendererPane = cellRendererPane;
        }
        public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) 
        {
            if (c == null)                   
            {
                if (p != null) 
                {
                    Color oldColor = g.getColor();
                    g.setColor(p.getBackground());
                    g.fillRect(x, y, w, h);
                    g.setColor(oldColor);
                }
                return;
            }
            Document doc = ((JTextComponent)c).getDocument();
            if (c.getParent() != this) 
            {
                this.add(c);
            }
            c.setBounds(x, y, w, h);
                    
            if(shouldValidate) 
            {
                c.validate();
            }

            boolean wasDoubleBuffered = false;
            if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) 
            {
                wasDoubleBuffered = true;
                ((JComponent)c).setDoubleBuffered(false);
            }
            jTextFieldall.setDocument(doc);
            Graphics cg = g.create(x, y, w, h);                 
            try {
                c.paint(cg);
            }
            finally {
                cg.dispose();
            }
            if (wasDoubleBuffered && (c instanceof JComponent)) {
                ((JComponent)c).setDoubleBuffered(true);
            }
            c.setBounds(-w, -h, 0, 0);
        }          
    }    
    
    //************************修改弹出框的选单样式
    public static class ReJCheckBox extends JCheckBox implements ListCellRenderer
    {
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) 
        {
            setComponentOrientation(list.getComponentOrientation());
            if (isSelected) 
            {
                setBackground(list.getSelectionBackground());
                setForeground(list.getSelectionForeground());
            } else 
            {
                setBackground(list.getBackground());
                setForeground(list.getForeground());
            }
            setEnabled(list.isEnabled());
            setSelected(isSelected);
            setText(value == null ? "" : value.toString());
            setFont(list.getFont());
            return this;
        }
    }
}


以上是关于VB combobox怎么实现可多选下拉列表中的内容?的主要内容,如果未能解决你的问题,请参考以下文章

WPF 实现可以多选的 Combo box 有啥好的思路或解决方案

html如何做下拉的可多选的复选框列表

html如何做下拉的可多选的复选框列表

一个类别和带分页的可多选表格的实现方案

如何用VB6中的COMBOBOX实现模糊输入后自动匹配下拉列表

VB大神赐教!怎么让下拉列表combobox只能选择不能写入其他值?