110,579
社区成员
发帖
与我相关
我的任务
分享
//定义调查类
public class Survey
{
public List<Item> Items =new List<Item>;
}
//定义每个调查项目
public class Item
{
public Control QuestionControl;
public Control[] AnswerControls = new Control[]();
public AnswerType AnswerType;
}
//每个调查项的回复方式枚举(作用后述)
public enum AnswerType
{
//反馈内容需输入文本
Text,
//复选
CheckBox,
//单选
RadioButton,
//下拉
DropDown,
}
/*
调查项目:
性别:男,女
年龄:10,20,30,40,50,60以上
口味:好吃,一般,不好吃
价格:便宜,适合,贵
购买原因:外观好看,货好,便宜
*/
void Main(string [] arges)
{
//这里假设每一个控件的构造方法都是设置Text为传入的string.
Survey s = new Survey();
Item item = new Item();
item.QuestionControl = new Label("性别");
item.AnswerControls.Add(new RadioButton("男"));
item.AnswerControls.Add(new RadioButton("女"));
item.AnswerType = Answer.RadioButton;
s.Items.Add(item);
item.QuestionControl = new Label("年龄");
item.AnswerControls.Add(new RadioButton("10"));
item.AnswerControls.Add(new RadioButton("20"));
item.AnswerControls.Add(new RadioButton("30"));
item.AnswerControls.Add(new RadioButton("40"));
item.AnswerControls.Add(new RadioButton("50"));
item.AnswerControls.Add(new RadioButton("60"));
item.AnswerControls.Add(new RadioButton("60以上"));
item.AnswerType = Answer.RadioButton;
s.Items.Add(item);
//其他的照加,这里主要演示多选的情况
item.QuestionControl = new Label("购买原因");
item.AnswerControls.Add(new CheckBox("外观好看"));
item.AnswerControls.Add(new CheckBox("货好"));
item.AnswerControls.Add(new CheckBox("便宜"));
item.AnswerType = AnswerType.CheckBox;
s.Items.Add(item);
//显示.
foreach (Item it in s.Items)
{
//显示选项文本控件
//自己实现布局.本处未实现
it.QuestionControl.Show();
for (int i = 0;i<it.AnswerControls.Count;i++)
{
//显视备选内容控件
//自己实现布局.本处未实现
it.AnswerControls[i].Show();
}
}
//将调查结果读到Hashtable,也可以是DataTable
Hashtable hash = new Hashtable();
foreach (Item it in s.Items)
{
string answerStr = "";
for (int i = 0;i<it.AnswerControls.Count;i++)
{
if (it.AnswerType == AnswerType.RidioButton && ((RadioButton)it.AnswerControls[i]).Checkd)
{
answerStr = answerStr + AnswerControls[i].Text;
break;
}
else if (it.AnswerType == AnswerType.CheckBox && ((CheckBox)it.AnswerControls[i]).Checkd)
{
answerStr = answerStr + AnswerControls[i].Text;
}
//可以加入其他判断
}
hash.Add(it.QuestionControl.Text,answerStr );
}
}
foreach(string[] strs in DiaoCha)
{
for(int i = 0;i<strs.Length;i++)
{
if(i == 0)//等于名字
}
}