C#如何在选择下拉框选项的同时显示对应信息在窗体上

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

我也明白这个标题很绕嘴,但是我就是说不明白

解释标题:

 

 

功能实现 

我们先来分析一下,既然能选择查询就说明卡号和姓名是绑定的,我这里的卡号和姓名是在一个表里的两个字段。(多表可以用视图联起来查)那既然是在一个表里,那我们应用的无非就是单表查询,问题在于如何显示。请参考笔者代码,希望能帮你获取答案。

实体层

namespace Entity
{
    /// <summary>
    /// 用户实体类
    /// </summary>
    public class UserEntity
    {
        //用户ID
        private int userID;
        public int UserID
        {
            get { return userID; }
            set { userID = value; }
        }

        //密码
        private string password;
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        //用户名
        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }

        //用户级别
        private string level;
        public string Level
        {
            get { return level; }
            set { level = value; }
        }
    }
}

 

 接口层

   public  interface ICheck
    {
        //从User表里查询用户ID
        List<Entity.UserEntity> SelectAllID(Entity.UserEntity user);
    }

 

数据传输层

   public  class CheckDAL :IDAL.ICheck
    {
        SQLHelper sqlhelper = new SQLHelper();

        /// <summary>
        /// 根据level查所有UserID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Entity.UserEntity> SelectAllID(Entity.UserEntity user)
        {
            SqlParameter[] sqlparams = { new SqlParameter("@level", "操作员") };
            string sql = @"select * from T_User where level=@level";
            DataTable table = sqlhelper.ExecuteQuery(sql, sqlparams, CommandType.Text);

            //将Datatable类型转换为list泛型
            ConvertHelper ct = new ConvertHelper();
            List<Entity.UserEntity> List = new List<Entity.UserEntity>();
            List = ct.ConvertTomodel<Entity.UserEntity>(table);
            return List;
        }

 

工厂层

        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];   //接收来自配置文件的数据

        /// <summary>
        /// 结账
        /// </summary>
        /// <returns></returns>
        public IDAL.ICheck Check()
        {
            string ClassName = StrDB + "." + "CheckDAL";  //DAL层的类名
            return (IDAL.ICheck)Assembly.Load(StrDB).CreateInstance(ClassName);   //反射+工厂的应用
        }

 

逻辑层

    public class CheckBLL  //结账
    {

        /// <summary>
        /// 根据level查所有UserID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Entity.UserEntity> SelectAllID(Entity.UserEntity user)
        {
            Factory.AdministratorFactory fact = new Factory.AdministratorFactory();
            IDAL.ICheck idal = fact.Check();
            return idal.SelectAllID(user);
        }

 

 

外观层

    public class CheckFacade //结账
    {

        /// <summary>
        /// 根据level查所有UserID
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Entity.UserEntity> SelectAllID(Entity.UserEntity user)
        {
            return new BLL.CheckBLL().SelectAllID(user);
        }

 

 

显示层

        //将操作员姓名加入到下拉菜单
        private void FrmCheck_Load(object sender, EventArgs e)
        {
            Facade.CheckFacade facade = new Facade.CheckFacade();
            Entity.UserEntity user = new Entity.UserEntity();
            List<Entity.UserEntity> list = facade.SelectAllID(user);
            for (int i = 0; i < list.Count; i++)
            {
                cmbUserID.Items.Add(list[i].UserID);
            }

        }
        /// <summary>
        /// 获得用户ID后,给姓名赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbUserID_SelectedIndexChanged(object sender, EventArgs e)
        {
            Facade.CheckFacade facade = new Facade.CheckFacade();
            Entity.UserEntity user = new Entity.UserEntity();
            List<Entity.UserEntity> list = facade.SelectAllID(user);
            for (int i = 0; i < list.Count; i++)
            {
                if (Convert.ToInt32(cmbUserID.Text) == list[i].UserID)
                {
                    txtUserName.Text = Convert.ToString(list[i].UserName);
                }
            }

            this.cancelcard();//退卡信息写入表格
            this.recharge();//充值信息写入表格
            this.register();//注册信息写入表格
            this.show();//汇总显示
        }

 

本篇博客针对性较强,主要针对七层开发框架。如果读者乍看不太懂,不妨照着敲敲,未涉及到较复杂的问题,但愿你的问题可以迎刃而解~

展开阅读全文

vc显示下拉框选择选项

09-16

BOOL CProgramexialakuangDlg::OnInitDialog()rnrn CDialog::OnInitDialog();rnrn // Add "About..." menu item to system menu.rnrn // IDM_ABOUTBOX must be in the system command range.rn ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);rn ASSERT(IDM_ABOUTBOX < 0xF000);rnrn CMenu* pSysMenu = GetSystemMenu(FALSE);rn if (pSysMenu != NULL)rn rn CString strAboutMenu;rn strAboutMenu.LoadString(IDS_ABOUTBOX);rn if (!strAboutMenu.IsEmpty())rn rn pSysMenu->AppendMenu(MF_SEPARATOR);rn pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);rn rn rnrn // Set the icon for this dialog. The framework does this automaticallyrn // when the application's main window is not a dialogrn SetIcon(m_hIcon, TRUE); // Set big iconrn SetIcon(m_hIcon, FALSE); // Set small iconrn rn // TODO: Add extra initialization herern CComboBox* a= ( CComboBox*)GetDlgItem(IDC_COMBO1);//创建组合框及选项rnrn a->AddString("黑色");rnrn a->AddString("红色");rnrn a->AddString("兰色");rnrnrnrn return TRUE; // return TRUE unless you set the focus to a controlrnrnrnrnvoid CProgramexialakuangDlg::OnButton1() //点击按钮对话框显示所选的选项,为什么总显示红色?rnrn // TODO: Add your control notification handler code herern CComboBox* a=(CComboBox*)GetDlgItem(IDC_COMBO1);rn if( a->SetCurSel(0))rn rn AfxMessageBox(_T("黑色"));rn rn else if(a->SetCurSel(1))rn rn AfxMessageBox(_T("红色"));rn rn else if(a->SetCurSel(2))rn rn AfxMessageBox(_T("兰色"));rn rn 论坛

求JComboBox如何在下拉框选项上添加一个Button

07-24

[size=18px]本人很菜,想写一个登陆框,其中用户名的JComboBox下拉项目上想添加一个button来实现删除不需要的用户名。现在关键不知道用什么方法写,因为用户名的ComboBox是可编辑的,所以不能使用setRenderer()方法rn,最好哪位高人能发一下实际代码就最好了!rnrn下面的代码是使用 setRenderer实现的,但是当选择后,显示的是对象字符串,大家可以运行下看看效果,然后注释掉下面的标记再运行看看~[/size]rn[code=Java]class ComplexCellRenderer implements ListCellRenderer rn protected DefaultListCellRenderer defaultRenderer = new DefaultListCellRenderer();rnrn public Component getListCellRendererComponent(JList list, Object value,rn int index, boolean isSelected, boolean cellHasFocus) rn Font theFont = null;rn Color theForeground = null;rn Icon theIcon = null;rn String theText = null;rnrn JLabel renderer = (JLabel) defaultRendererrn .getListCellRendererComponent(list, value, index, isSelected,rn cellHasFocus);rnrn if (value instanceof Object[]) rn Object values[] = (Object[]) value;rn theFont = (Font) values[0];rn theForeground = (Color) values[1];rn theIcon = (Icon) values[2];rn theText = (String) values[3];rn else rn theFont = list.getFont();rn theForeground = list.getForeground();rn theText = "";rn rn if (!isSelected) rn renderer.setForeground(theForeground);rn rn if (theIcon != null) rn renderer.setIcon(theIcon);rn rn renderer.setText(theText);rn renderer.setFont(theFont);rn return renderer;rn rnrnrnpublic class Test2 rn public static void main(String args[]) rn Object elements[][] = rn new Font("Helvetica", Font.PLAIN, 20), Color.RED,rn new MyIcon(), "A" ,rn new Font("TimesRoman", Font.BOLD, 14), Color.BLUE,rn new MyIcon(), "A" ,rn new Font("Courier", Font.ITALIC, 18), Color.GREEN,rn new MyIcon(), "A" ,rn new Font("Helvetica", Font.BOLD | Font.ITALIC, 12),rn Color.GRAY, new MyIcon(), "A" ,rn new Font("TimesRoman", Font.PLAIN, 32), Color.PINK,rn new MyIcon(), "A" ,rn new Font("Courier", Font.BOLD, 16), Color.YELLOW,rn new MyIcon(), "A" ,rn new Font("Helvetica", Font.ITALIC, 8), Color.DARK_GRAY,rn new MyIcon(), "A" ;rnrn JFrame frame = new JFrame("Complex Renderer");rn frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rnrn ListCellRenderer renderer = new ComplexCellRenderer();rn JComboBox comboBox = new JComboBox(elements);rn comboBox.setEditable(true); //标记rn comboBox.setRenderer(renderer);rn frame.add(comboBox, BorderLayout.NORTH);rnrn frame.setSize(300, 200);rn frame.setVisible(true);rn rnrnrnclass MyIcon implements Icon rnrn public MyIcon() rn rnrn public int getIconHeight() rn return 20;rn rnrn public int getIconWidth() rn return 20;rn rnrn public void paintIcon(Component c, Graphics g, int x, int y) rn g.setColor(Color.RED);rn g.drawRect(0, 0, 25, 25);rn rn[/code] 论坛

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