我是C#的新手。我正在大学的一个模块中学习它。我们获得了一项任务,涉及我们必须使用Visual Studio中工具箱中包含的各种组件创建一个简单的预订应用程序。
用户界面有一个ListBox
,用户可以选择参加活动的多个人名。当用户确认选择时,所选项目将连接到String
并在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();
}
输出是这样的:
房间2已于08/10/20由杰森曼福德预订Comedy Gig。 Jack Coldrick Bill Gates Larry Page Jimmy Wales将出席。
但是我想添加一个和参加活动的人的姓氏。我该怎么做呢我是否必须使用List
?
非常感谢...
答案 0 :(得分:3)
尝试使用简单的计数器将所选项目复制到另一个集合中:
int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)
List<ListItem> selectedItems = new List<ListItem>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
selectedItems.Add(li);
}
}
foreach (ListItem li in selectedItems)
{
counter++;
if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
{
people += " and";
}
people += li.Text + " ";
}
正如少数人所指出的,你也应该考虑使用StringBuilder
,因为字符串在.Net中是不可变的,这意味着它们不能被修改。每次将文本追加到字符串时,在幕后都会使用新内容创建一个新字符串,旧字符串将被丢弃。可以想象,如果列表中有很多名称,最终可能会影响性能。示例如下:
List<ListItem> selectedItems = new List<ListItem>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
selectedItems.Add(li);
}
}
StringBuilder sbPeople = new StringBuilder();
int counter = 1; // start at 1 so that the counter is in line with the number of items that the loop has iterated over (instead of 0 which would be better for indexing into the collection)
foreach (ListItem li in attendees.SelectedItems)
{
counter++;
if (selectedItems.Count > 1 && i == selectedItems.Count) // check after the counter has been incremented so that only the last item triggers it
{
sbPeople.Append(" and");
}
sbPeople.Append(li.Text);
sbPeople.Append(" ");
}
参考StringBuilder
文档:http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx
答案 1 :(得分:3)
var p = new List<string>() {"person1", "person2", "person3"};
string people;
if(p.Count == 1)
people = p[0];
else
people = string.Join(", ", p.Take(p.Count - 1)) + " and " + p[p.Count - 1]
为了更好地适应你的代码,我会写这样的东西(从评论中间接建议):
var p = attendees.Items.OfType<ListItem>.Where(y => y.Selected).Select(y => y.Text).ToList();
var people = "";
if(p.Count == 1)
people = p[0];
if(p.Count > 1)
people = string.Join(", ", p.Take(p.Count - 1)) + " and " + p[p.Count - 1]
Confirmation.Text = string.Format("{0} has been booked on {1} by {2} for {3}. {4} will be attending", r, d, n, en, people);
答案 2 :(得分:2)
你可以用这个替换你的foreach
var peopleList = new List<string>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
peopleList.Add(li.Text);
}
var people = string.Join(",", list.Take(list.Count - 1));
people += list.Count > 1 ? " and " + list.Last() : list.Last();
答案 3 :(得分:0)
为了使答案与你的问题保持一致,我会很容易理解 - 尽管我使用StringBuilder而不是附加字符串 - 我已经看到这种方法可以更快地处理更大的字符串。
var selectedPeople = new List<string>();
foreach (ListItem li in attendees.Items)
{
if (li.Selected)
{
people.Add(li.Text);
}
}
var sb = new StringBuilder();
if (selectedPeople.Count == 0)
{
sb.Append("");
}
else
{
for (var i = 0; i < selectedPeople.Count; i++)
{
if (i > 0)
{
sb.Append(" ");
if (i == selectedPeople.Count)
{
sb.Append("and ");
}
}
sb.Append(selectedPeople[i]);
}
}
...
confirmation.Text = r + " has been booked on " + d + " by " + n + " for " + en + ". " + sb.ToString() + " will be attending.";
我建议你研究另一个建议使用SelectedItems的答案,以防止必须进行第一次循环。
答案 4 :(得分:0)
var selectedPeople = list.Items.OfType<ListItem>().Where(c => c.Selected).Select(c => c.Text).ToList();
string people;
if (selectedPeople.Count == 1)
people = selectedPeople[0];
else
people = string.Join(", ", selectedPeople.Take(selectedPeople.Count - 1))
+ " and " + selectedPeople[selectedPeople.Count - 1];