Простая привязка данных к списку объектов. Ошибка "CreateBookInfoList" не существует в текущем контексте - C#

Узнай цену своей работы

Формулировка задачи:

Ошибка 2 Элемент "CreateBookInfoList" не существует в текущем контексте.

Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication6
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. bookInfoList = CreateBookInfoList();
  17. Bind();
  18. BindingManager.PositionChanged += new EventHandler(BindingManager_PositionChanged);
  19. RefreshData();
  20. }
  21. private BookInfo CreateBookInfo()
  22. {
  23. BookInfo bookInfo = new BookInfo();
  24. bookInfo.Author = "Том Клэнси";
  25. bookInfo.Title = "Игры патриотов";
  26. bookInfo.xxx = "5-699-18175-Х";
  27. bookInfo.PageCount = 706;
  28. bookInfo.Publisher = "ЭКСМО";
  29. return bookInfo;
  30. }
  31. private void Bind()
  32. {
  33. textBox1.DataBindings.Add("Text", bookInfoList, "Author", true);
  34. textBox2.DataBindings.Add("Text", bookInfoList, "Title");
  35. textBox3.DataBindings.Add("Text", bookInfoList, "xxx");
  36. textBox4.DataBindings.Add("Text", bookInfoList, "PageCount");
  37. textBox5.DataBindings.Add("Text", bookInfoList, "Publisher");
  38. }
  39. private BindingManagerBase BindingManager
  40. {
  41. get
  42. {
  43. return BindingContext[bookInfoList];
  44. }
  45. }
  46. private void BindingManager_PositionChanged(object sender, EventArgs e)
  47. {
  48. RefreshData();
  49. }
  50. private void button1_Click(object sender, EventArgs e)
  51. {
  52. BindingManager.Position = 0;
  53. RefreshData();
  54. }
  55. private void button2_Click(object sender, EventArgs e)
  56. {
  57. BindingManager.Position--;
  58. RefreshData();
  59. }
  60. private void button3_Click(object sender, EventArgs e)
  61. {
  62. BindingManager.Position++;
  63. RefreshData();
  64. }
  65. private void moveLastButton_Click(object sender, EventArgs e)
  66. {
  67. BindingManager.Position = BindingManager.Count - 1;
  68. RefreshData();
  69. }
  70. private void RefreshData()
  71. {
  72. int count = BindingManager.Count;
  73. int position = BindingManager.Position + 1;
  74. label2.Text = count.ToString();
  75. label1.Text = position.ToString();
  76. button1.Enabled = position > 1;
  77. button2.Enabled = position > 1;
  78. button3.Enabled = position < count;
  79. button4.Enabled = position < count;
  80. }
  81. private List<BookInfo> bookInfoList;
  82. }
  83. class BookInfo
  84. {
  85. public string Author { get; set; }
  86. public string Title { get; set; }
  87. public string xxx { get; set; }
  88. public int PageCount { get; set; }
  89. public string Publisher { get; set; }
  90. }
  91. }
WindowsFormsApplication6.rar

Решение задачи: «Простая привязка данных к списку объектов. Ошибка "CreateBookInfoList" не существует в текущем контексте»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         private BookInfo bookInfo;
  15.         private BookInfo bookInfo2;
  16.         private List<BookInfo> bookInfoList;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.             bookInfo = CreateBookInfo();
  22.             bookInfo2 = CreateBookInfo2();
  23.             bookInfoList = new List<BookInfo>();
  24.             bookInfoList.Add(bookInfo);
  25.             bookInfoList.Add(bookInfo2);
  26.             Bind();
  27.             BindingManager.PositionChanged += new EventHandler(BindingManager_PositionChanged);
  28.             button1.Click+=new EventHandler(button1_Click);
  29.             button2.Click+=new EventHandler(button2_Click);
  30.             button3.Click+=new EventHandler(button3_Click);
  31.             button4.Click+=new EventHandler(button4_Click);
  32.             RefreshData();
  33.         }
  34.  
  35.         private BookInfo CreateBookInfo()
  36.         {
  37.             BookInfo bookInfo = new BookInfo();
  38.             bookInfo.Author = "Том Клэнси";
  39.             bookInfo.Title = "Игры патриотов";
  40.             bookInfo.ISBN = "5-699-18175-Х";
  41.             bookInfo.PageCount = 706;
  42.             bookInfo.Publisher = "ЭКСМО";
  43.             return bookInfo;
  44.         }
  45.         private BookInfo CreateBookInfo2()
  46.         {
  47.             BookInfo bookInfo2 = new BookInfo();
  48.             bookInfo2.Author = "Том Клэнси";
  49.             bookInfo2.Title = "Red rabbit";
  50.             bookInfo2.ISBN = "5-699-18175-Х";
  51.             bookInfo2.PageCount = 756;
  52.             bookInfo2.Publisher = "ЭКСМО";
  53.             return bookInfo2;
  54.         }
  55.         private void Bind()
  56.         {
  57.             Binding authorBinding = new Binding("Text", bookInfoList, "Author", true);
  58.             textBox1.DataBindings.Add(authorBinding);
  59.             textBox2.DataBindings.Add("Text", bookInfoList, "Title");
  60.             textBox3.DataBindings.Add("Text", bookInfoList, "ISBN");
  61.             textBox4.DataBindings.Add("Text", bookInfoList, "PageCount");
  62.             textBox5.DataBindings.Add("Text", bookInfoList, "Publisher");
  63.         }
  64.  
  65.         private BindingManagerBase BindingManager
  66.            {
  67.              get
  68.                {
  69.                 return BindingContext[bookInfoList];
  70.                }
  71.            }
  72.  
  73.         private void BindingManager_PositionChanged(object sender, EventArgs e)
  74.            {
  75.             RefreshData();
  76.            }
  77.  
  78.         private void button1_Click(object sender, EventArgs e)
  79.            {
  80.             BindingManager.Position = 0;
  81.             RefreshData();
  82.            }
  83.  
  84.         private void button2_Click(object sender, EventArgs e)
  85.             {
  86.              BindingManager.Position--;
  87.              RefreshData();
  88.             }
  89.  
  90.         private void button3_Click(object sender, EventArgs e)
  91.             {
  92.              BindingManager.Position++;
  93.              RefreshData();
  94.             }
  95.  
  96.         private void button4_Click(object sender, EventArgs e)
  97.             {
  98.              BindingManager.Position = BindingManager.Count - 1;
  99.              RefreshData();
  100.             }
  101.  
  102.         private void RefreshData()
  103.             {
  104.              int count = BindingManager.Count;
  105.              int position = BindingManager.Position + 1;
  106.  
  107.              label2.Text = count.ToString();
  108.              label1.Text = position.ToString();
  109.              button1.Enabled = position > 1;
  110.              button2.Enabled = position > 1;
  111.              button3.Enabled = position < count;
  112.              button4.Enabled = position < count;
  113.             }
  114.  
  115.     }
  116.  
  117.     class BookInfo
  118.     {
  119.         public string Author { get; set; }
  120.         public string Title { get; set; }
  121.         public string ISBN { get; set; }
  122.         public int PageCount { get; set; }
  123.         public string Publisher { get; set; }
  124.     }
  125. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

13   голосов , оценка 4.231 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы