Sử dụng ListBox trong C#

X

Privacy & Cookies

This site uses cookies. By continuing, you agree to their use. Learn more, including how to control cookies.

Got It!
Advertisements

Listbox

Yêu cầu: các button lệnh thực hiện các công việc sau:

  • Tính tổng các phẩn tử trong Listbox, hiển thị lên Messagebox.
  • Xóa phần tử đầu và cuối của listbox.
  • Xóa các phần tử đang chọn trong listbox.
  • Tăng giá trị mỗi phần tử lên 2
  • Thay mỗi giá trị của mỗi phần tử bằng bình pương của chính nó
  • Thực hiện chọn các phần tử trong listbox là số chẵn.
  • Thực hiện chọn các phần tử trong listbox là số lẻ.

Ảnh code


//imgur.com/gallery/zVAjq

Full code:
public partial class Frm_ListBoxBTVn : Form
{
public Frm_ListBoxBTVn[]
{
InitializeComponent[];
}

private void btn_Nhap_Click[object sender, EventArgs e]
{
if [!String.IsNullOrEmpty[txt_Phantu.Text]]
lsb_Phantu.Items.Add[txt_Phantu.Text];
}

private void btn_SumPTList_Click[object sender, EventArgs e]
{
double tong = 0;
foreach [string s in lsb_Phantu.Items]
{
tong += double.Parse[s];
}
MessageBox.Show[tổng các phần tử trong listbox + tong.ToString[]];
}

private void btn_XoaPTdaucuoi_Click[object sender, EventArgs e]
{
if [lsb_Phantu.Items.Count < 2] lsb_Phantu.Items.RemoveAt[0]; else { lsb_Phantu.Items.RemoveAt[0]; lsb_Phantu.Items.RemoveAt[lsb_Phantu.Items.Count 1]; } } private void btn_xoaphantudangchon_Click[object sender, EventArgs e] { while [lsb_Phantu.SelectedItems.Count != 0] { lsb_Phantu.Items.Remove[lsb_Phantu.SelectedItems[0]]; } } private void btn_TangPTlen2_Click[object sender, EventArgs e] { for [int i = lsb_Phantu.Items.Count 1; i >= 0; i]
{
if [!String.IsNullOrEmpty[lsb_Phantu.Items[i] as String]]
{
double tmp = double.Parse[[lsb_Phantu.Items[i].ToString[]]];
tmp = tmp + 2;
lsb_Phantu.Items[i] = tmp.ToString[];
}
}
}

private void btn_Thaybangbinhphuong_Click[object sender, EventArgs e]
{
for [int i = lsb_Phantu.Items.Count 1; i >= 0; i]
{
if [! String.IsNullOrEmpty[lsb_Phantu.Items[i] as String]]
{
double tmp = double.Parse[[lsb_Phantu.Items[i].ToString[]]];
tmp = Math.Pow[tmp,2];
lsb_Phantu.Items[i] = tmp.ToString[];
}
}
}

private void btn_chonsochan_Click[object sender, EventArgs e]
{
for [int i = lsb_Phantu.Items.Count 1; i >= 0; i]
{
if [!String.IsNullOrEmpty[lsb_Phantu.Items[i] as String]]
{
int tmp = int.Parse[[lsb_Phantu.Items[i].ToString[]]];
if [tmp % 2 == 0]
lsb_Phantu.SetSelected[i, true];
}
}
}

private void btn_Chonsole_Click[object sender, EventArgs e]
{
for [int i = lsb_Phantu.Items.Count 1; i >= 0; i]
{
if [!String.IsNullOrEmpty[lsb_Phantu.Items[i] as String]]
{
int tmp = int.Parse[[lsb_Phantu.Items[i].ToString[]]];
if [tmp % 2 == 1]
lsb_Phantu.SetSelected[i, true];
}
}
}

private void Frm_ListBoxBTVn_FormClosing[object sender, FormClosingEventArgs e]
{
DialogResult r = MessageBox.Show[Bạn thật sự muốn đóng ứng dụng, Đóng ứng dụng, MessageBoxButtons.OKCancel];
if [r==DialogResult.Cancel]
{
e.Cancel = true;
}
}

private void btn_ketthuc_Click[object sender, EventArgs e]
{
this.Dispose[];
}

Comment sẽ giải thích
Dispose[],duyệt listbox: 3 cách, setSelected[index,bool], DialogResult,RemoveAt[],

Full: //www.tenlua.vn/weblog/1737e628e9036a051531/a.rar

Advertisements

Share this:

Related

  • Load dữ liệu combobox C#
  • October 26, 2017
  • In "Chuyện Cafe"
  • Chuyển listbox này sang listbox kia [BTTL]
  • October 26, 2017
  • In "Chuyện Cafe"
  • Tiếng anh IT
  • January 19, 2018

Video liên quan

Chủ Đề