Простая привязка данных к списку объектов. Ошибка "CreateBookInfoList" не существует в текущем контексте - C#
Формулировка задачи:
Ошибка 2 Элемент "CreateBookInfoList" не существует в текущем контексте.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication6
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- bookInfoList = CreateBookInfoList();
- Bind();
- BindingManager.PositionChanged += new EventHandler(BindingManager_PositionChanged);
- RefreshData();
- }
- private BookInfo CreateBookInfo()
- {
- BookInfo bookInfo = new BookInfo();
- bookInfo.Author = "Том Клэнси";
- bookInfo.Title = "Игры патриотов";
- bookInfo.xxx = "5-699-18175-Х";
- bookInfo.PageCount = 706;
- bookInfo.Publisher = "ЭКСМО";
- return bookInfo;
- }
- private void Bind()
- {
- textBox1.DataBindings.Add("Text", bookInfoList, "Author", true);
- textBox2.DataBindings.Add("Text", bookInfoList, "Title");
- textBox3.DataBindings.Add("Text", bookInfoList, "xxx");
- textBox4.DataBindings.Add("Text", bookInfoList, "PageCount");
- textBox5.DataBindings.Add("Text", bookInfoList, "Publisher");
- }
- private BindingManagerBase BindingManager
- {
- get
- {
- return BindingContext[bookInfoList];
- }
- }
- private void BindingManager_PositionChanged(object sender, EventArgs e)
- {
- RefreshData();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- BindingManager.Position = 0;
- RefreshData();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- BindingManager.Position--;
- RefreshData();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- BindingManager.Position++;
- RefreshData();
- }
- private void moveLastButton_Click(object sender, EventArgs e)
- {
- BindingManager.Position = BindingManager.Count - 1;
- RefreshData();
- }
- private void RefreshData()
- {
- int count = BindingManager.Count;
- int position = BindingManager.Position + 1;
- label2.Text = count.ToString();
- label1.Text = position.ToString();
- button1.Enabled = position > 1;
- button2.Enabled = position > 1;
- button3.Enabled = position < count;
- button4.Enabled = position < count;
- }
- private List<BookInfo> bookInfoList;
- }
- class BookInfo
- {
- public string Author { get; set; }
- public string Title { get; set; }
- public string xxx { get; set; }
- public int PageCount { get; set; }
- public string Publisher { get; set; }
- }
- }
Решение задачи: «Простая привязка данных к списку объектов. Ошибка "CreateBookInfoList" не существует в текущем контексте»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- private BookInfo bookInfo;
- private BookInfo bookInfo2;
- private List<BookInfo> bookInfoList;
- public Form1()
- {
- InitializeComponent();
- bookInfo = CreateBookInfo();
- bookInfo2 = CreateBookInfo2();
- bookInfoList = new List<BookInfo>();
- bookInfoList.Add(bookInfo);
- bookInfoList.Add(bookInfo2);
- Bind();
- BindingManager.PositionChanged += new EventHandler(BindingManager_PositionChanged);
- button1.Click+=new EventHandler(button1_Click);
- button2.Click+=new EventHandler(button2_Click);
- button3.Click+=new EventHandler(button3_Click);
- button4.Click+=new EventHandler(button4_Click);
- RefreshData();
- }
- private BookInfo CreateBookInfo()
- {
- BookInfo bookInfo = new BookInfo();
- bookInfo.Author = "Том Клэнси";
- bookInfo.Title = "Игры патриотов";
- bookInfo.ISBN = "5-699-18175-Х";
- bookInfo.PageCount = 706;
- bookInfo.Publisher = "ЭКСМО";
- return bookInfo;
- }
- private BookInfo CreateBookInfo2()
- {
- BookInfo bookInfo2 = new BookInfo();
- bookInfo2.Author = "Том Клэнси";
- bookInfo2.Title = "Red rabbit";
- bookInfo2.ISBN = "5-699-18175-Х";
- bookInfo2.PageCount = 756;
- bookInfo2.Publisher = "ЭКСМО";
- return bookInfo2;
- }
- private void Bind()
- {
- Binding authorBinding = new Binding("Text", bookInfoList, "Author", true);
- textBox1.DataBindings.Add(authorBinding);
- textBox2.DataBindings.Add("Text", bookInfoList, "Title");
- textBox3.DataBindings.Add("Text", bookInfoList, "ISBN");
- textBox4.DataBindings.Add("Text", bookInfoList, "PageCount");
- textBox5.DataBindings.Add("Text", bookInfoList, "Publisher");
- }
- private BindingManagerBase BindingManager
- {
- get
- {
- return BindingContext[bookInfoList];
- }
- }
- private void BindingManager_PositionChanged(object sender, EventArgs e)
- {
- RefreshData();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- BindingManager.Position = 0;
- RefreshData();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- BindingManager.Position--;
- RefreshData();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- BindingManager.Position++;
- RefreshData();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- BindingManager.Position = BindingManager.Count - 1;
- RefreshData();
- }
- private void RefreshData()
- {
- int count = BindingManager.Count;
- int position = BindingManager.Position + 1;
- label2.Text = count.ToString();
- label1.Text = position.ToString();
- button1.Enabled = position > 1;
- button2.Enabled = position > 1;
- button3.Enabled = position < count;
- button4.Enabled = position < count;
- }
- }
- class BookInfo
- {
- public string Author { get; set; }
- public string Title { get; set; }
- public string ISBN { get; set; }
- public int PageCount { get; set; }
- public string Publisher { get; set; }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д