code_junkie

How do I make a ListBox refresh its item text?

Nov 10th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. Public Class Form1
  2.  
  3. Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
  4. MyBase.OnLoad(e)
  5.  
  6. For i As Integer = 1 To 3
  7. Dim tempInfo As New NumberInfo()
  8. tempInfo.Count = i
  9. tempInfo.Number = i * 100
  10. ListBox1.Items.Add(tempInfo)
  11. Next
  12. End Sub
  13.  
  14. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  15. For Each objItem As Object In ListBox1.Items
  16. Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
  17. info.Count += 1
  18. Next
  19. End Sub
  20. End Class
  21.  
  22. Public Class NumberInfo
  23.  
  24. Public Count As Integer
  25. Public Number As Integer
  26.  
  27. Public Overrides Function ToString() As String
  28. Return String.Format("{0}, {1}", Count, Number)
  29. End Function
  30. End Class
  31.  
  32. using System;
  33. using System.ComponentModel;
  34. using System.Windows.Forms;
  35.  
  36. namespace TestBindingList
  37. {
  38. public class Employee
  39. {
  40. public string Name { get; set; }
  41. public int Id { get; set; }
  42. }
  43.  
  44. public partial class Form1 : Form
  45. {
  46. private BindingList<Employee> _employees;
  47.  
  48. private ListBox lstEmployees;
  49. private TextBox txtId;
  50. private TextBox txtName;
  51. private Button btnRemove;
  52.  
  53. public Form1()
  54. {
  55. InitializeComponent();
  56.  
  57. FlowLayoutPanel layout = new FlowLayoutPanel();
  58. layout.Dock = DockStyle.Fill;
  59. Controls.Add(layout);
  60.  
  61. lstEmployees = new ListBox();
  62. layout.Controls.Add(lstEmployees);
  63.  
  64. txtId = new TextBox();
  65. layout.Controls.Add(txtId);
  66.  
  67. txtName = new TextBox();
  68. layout.Controls.Add(txtName);
  69.  
  70. btnRemove = new Button();
  71. btnRemove.Click += btnRemove_Click;
  72. btnRemove.Text = "Remove";
  73. layout.Controls.Add(btnRemove);
  74.  
  75. Load+=new EventHandler(Form1_Load);
  76. }
  77.  
  78. private void Form1_Load(object sender, EventArgs e)
  79. {
  80. _employees = new BindingList<Employee>();
  81. for (int i = 0; i < 10; i++)
  82. {
  83. _employees.Add(new Employee() { Id = i, Name = "Employee " + i.ToString() });
  84. }
  85.  
  86. lstEmployees.DisplayMember = "Name";
  87. lstEmployees.DataSource = _employees;
  88.  
  89. txtId.DataBindings.Add("Text", _employees, "Id");
  90. txtName.DataBindings.Add("Text", _employees, "Name");
  91. }
  92.  
  93. private void btnRemove_Click(object sender, EventArgs e)
  94. {
  95. Employee selectedEmployee = (Employee)lstEmployees.SelectedItem;
  96. if (selectedEmployee != null)
  97. {
  98. _employees.Remove(selectedEmployee);
  99. }
  100. }
  101. }
  102. }
  103.  
  104. public class RefreshingListBox : ListBox
  105. {
  106. public new void RefreshItem(int index)
  107. {
  108. base.RefreshItem(index);
  109. }
  110.  
  111. public new void RefreshItems()
  112. {
  113. base.RefreshItems();
  114. }
  115. }
  116.  
  117. Public Class Form1
  118.  
  119. Private datasource As New List(Of NumberInfo)
  120. Private bindingSource As New BindingSource
  121.  
  122. Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
  123. MyBase.OnLoad(e)
  124.  
  125. For i As Integer = 1 To 3
  126. Dim tempInfo As New NumberInfo()
  127. tempInfo.Count = i
  128. tempInfo.Number = i * 100
  129. datasource.Add(tempInfo)
  130. Next
  131. bindingSource.DataSource = datasource
  132. ListBox1.DataSource = bindingSource
  133. End Sub
  134.  
  135. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  136. For Each objItem As Object In datasource
  137. Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
  138. info.Count += 1
  139. Next
  140. bindingSource.ResetBindings(False)
  141. End Sub
  142. End Class
  143.  
  144. Public Class NumberInfo
  145.  
  146. Public Count As Integer
  147. Public Number As Integer
  148.  
  149. Public Overrides Function ToString() As String
  150. Return String.Format("{0}, {1}", Count, Number)
  151. End Function
  152. End Class
  153.  
  154. public class ListBox2 : ListBox {
  155. public void RefreshItem2(int index) {
  156. RefreshItem(index);
  157. }
  158. }
  159.  
  160. if (objLstTypes.SelectedItem != null)
  161. {
  162. PublisherTypeDescriptor objType = (PublisherTypeDescriptor)objLstTypes.SelectedItem;
  163. objLstTypes.Items.Remove(objType);
  164. objLstTypes.Items.Add(objType);
  165. objLstTypes.SelectedItem = objType;
  166. }
Add Comment
Please, Sign In to add comment