用组合框填充组合框 [英] fill Combo Box with Combo Box

查看:69
本文介绍了用组合框填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,印尼语的iam adnan
到重点

我想让组合框填充组合框,
案例:我有2个组合框,组合框1和组合框2,当我单击组合框1(例如:数学课)中的一项时,组合框2会自动填写数学课的学生姓名,

我的假设:我从Microsoft Office Access数据库中创建一些数据库,还是从源代码中获取它?
你能帮我做什么?

非常感谢您的帮助,谢谢.

adnan

hello, iam adnan from indonesian
to the point

i want to make Combo Box fill Combo Box,
case : i have 2 Combo Box, Combo Box 1 and Combo Box 2, when i click one of item in Combo Box 1 (ex : Math Class) , Combo Box 2 will automatically fill the name of student in Math class,

My assumption : i make some database form microsoft office access database, or i make it from source?
can you help me what must i do?

i really appreciate if you help me, thx

adnan

推荐答案

Selamat疮,

到目前为止,您做了什么并且尝试过?

您是否创建了两个ComboBox,并用Items填充了它们,并确保您了解如何通过使用为以下事件之一的第一个ComboBox创建的EventHandler来更改第二个ComboBox中的内容?或SelectionChangeCommitted吗?

也许,考虑解析简单的Text文件以保存第二个ComboBox项目集的内容.每个不同的组都有一个单独的文件?



Visual Studio及其生成的Designer.cs文件(C#,WinForms)在这里可能对您来说是一个宝贵的资产:将第一个ComboBox拖放到Form上后,在上下文上单击鼠标,然后选择编辑项目:",然后输入一堆条目,并用回车符分隔.关闭该项目编辑器.

然后,打开Designer.cs文件:单击带有"+"号的小矩形以展开标题为"Windows Form Designer生成的代码"的区域.

您会看到将项目添加到组合框的方式如下所示:
Selamat Sore,

What have you done, and tried so far ?

Have you created two ComboBoxes, filled them with Items, and made sure you understand how to change what''s in the second ComboBox by using an EventHandler you''ve created for the first ComboBox for one of these events: SelectedIndexChanged, SelectedValueChanged, or SelectionChangeCommitted ?

Perhaps, consider parsing simple Text files to hold the contents of the second ComboBox item-sets. One separate file for each of the different groups ?

~

Visual Studio and the Designer.cs file it produces (C#, WinForms) can be a great asset to you here: after you drag-drop a first ComboBox onto a Form, context-click on it, and select "Edit Items:" then type in a bunch of entries, separated by carriage returns. Close that item-editor.

Then,open the Designer.cs file: click on the little rectangle with the "+" sign to expand the region titled "Windows Form Designer generated code."

You''ll see that the way that Items are added to a ComboBox looks something like this:
this.cmbPrimary.Items.AddRange
(
    new object[] 
    {
        "Category1",
        "Category2",
        "Category3"
    }
);

这清楚地向您显示了表单 ,您将必须放入第二个ComboBox的不同项集,并且您将必须将您的数据(无论其来源如何)转化为该形式.有很多方法可以做到这一点.



就个人而言,我可能会将所有项目集数据存储在一个Text文件或一个XML格式文件中,然后对其进行解析:因此,我将构建一个像这样的对象:

That shows you clearly the form your different item-sets for the second ComboBox are going to have to be in, and you are going to have to get your data, whatever its source, into that form. There are many ways to do that.

~

Personally, I would probably store all the Item-set data in either one Text file, or, perhaps one XML format file, and then parse it: so, I''d build an object like this:

List<Object[]> SecondaryComboBoxItems = new List<object[]>()
{
    new object[] { "Item 1", "Item 2", "Item 3", "Item 4" },
    new object[] { "Item 1a", "Item 2a", "Item 3a", "Item 4a"},
    new object[] { "Item 1b", "Item 2b", "Item 3b", "Item 4b"}
};

这给了我一个列表,每个列表都是一个对象数组,其中给定数组中的每个对象都是一个字符串.

因此,我可以只使用一个整数索引(在这种情况下为#0〜2)将正确的项放入辅助ComboBox中.



数据库问题:如果您使用数据库,您可能不想做的一件事是:每次第一个ComboBox中的选择更改时,您都必须再次下拉数据:在这种情况下,您可能并没有在谈论可能需要这样做的大量数据".

因此,如果您使用数据库:如何获取将要放入第二个ComboBox的项目集中的所有不同组,并存储(缓存")它们:因此在第二个ComboBox的项目集之间进行切换ComboBox,因为第一个ComboBox中的选定项更改:快速?

这并不困难,并且是您要解决的一个好问题.



如果您将此挑战分解为步骤,正如我在此处试图说明的那样(某些编程老师将其称为分而治之"策略),请解决每个步骤:

您将学习一套技巧,然后将其应用到许多未来的问题中,而我在此响应中的目标是让您继续努力,而不仅仅是告诉您如何编写代码.

Baik baik saja吗?

祝您好运!Bill

This gives me a List, each Item of which is an Array of Objects where each object in a given array is a string.

So, I could just use an integer index (in this case #0~2) to get the right Items into the secondary ComboBox.

~

The Database issue: if you use a DataBase, one thing you probably don''t want to do is: every time the selection in the first ComboBox changes you must pull-down the data again: in your scenario here, you are probably not talking about such "massive data" that you would need to, possibly, do this.

So, if you use a DataBase: how can you get all the different groups of whatever that will go into the second ComboBox''s Item-sets, and store ("cache") them: so switching between Item-sets in the second ComboBox, as the selected Item in the first ComboBox changes: is fast ?

This is not difficult, and is a good problem for you to solve.

~

If you break down this challenge into steps, as I''ve tried to illustrate here (this is called the "divide and conquer" strategy" by some teachers of programming), and solve each step:

You will learn a set of skills that you can then apply to many different future problems, and my goal in this response is to get you to work on that, not just telling you how to write the code.

Baik baik saja ?

good luck, Bill


从数据库中提取信息,并使用selectedindexchanged事件.填充1个组合框后,第二个组合框将填充与先前选择相关的相应信息.
Pull info from database and use the selectedindexchanged event. When 1 combobox is filled, the 2nd one will be filled with corresponding information relating to the prior selection.


这篇关于用组合框填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆