我是 C# 新手。我正在大学的一个模块中学习它。我们被分配了一项任务,其中涉及我们必须使用 Visual Studio 工具箱中包含的各种组件创建一个简单的预订应用程序。
UI 具有一个ListBox
使用户能够选择将参加该活动的人员的多个姓名。当用户确认选择时,所选项目将连接到 aString
并在 a 中输出。Label
这是我从中获取值的代码ListBox
protected void btnRequest_Click(object sender, EventArgs e)
{
//Update the summary label with the details of the booking.
n = name.Text;
en = eventName.Text;
r = room.SelectedItem.ToString();
d = cal.SelectedDate.ToShortDateString();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
people += li.Text + " ";
}
}
confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
}
以下是我的完整代码:
public partial class _Default : System.Web.UI.Page
{
//Variables
public TextBox name;
public TextBox eventName;
public Label confirmation;
public DropDownList room;
public Calendar cal;
public Button btn;
public ListBox attendees;
//Booking variables - store all information relating to booking in these variables
public String n; //name of person placing booking
public String en; //name of event
public String r; //room it will take place
public List<String> att; //list of people attending
public String d; //date it will be held on
public String people;
protected void Page_Load(object sender, EventArgs e)
{
//Get references to components
name = txtName;
eventName = txtEvent;
room = droplistRooms;
attendees = attendeelist;
cal = Calendar1;
btn = btnRequest;
confirmation = lblSummary;
}
protected void btnRequest_Click(object sender, EventArgs e)
{
//Update the summary label with the details of the booking.
n = name.Text;
en = eventName.Text;
r = room.SelectedItem.ToString();
d = cal.SelectedDate.ToShortDateString();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
people += li.Text + " ";
}
}
confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + people + " will be attending.";
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
d = cal.SelectedDate.ToShortDateString();
}
输出是这样的:
Jason Manford 已于 2013 年 8 月 10 日为喜剧演出预订了房间 2。Jack Coldrick Bill Gates Larry Page Jimmy Wales 将出席。
但是,我想在参加活动的人的姓氏中添加一个和。我将如何去做这件事。我必须使用一个List
吗?
非常感谢...