I’m following the example from “Programming C# 4.0, 6th edition” for working with Windows Forms, but I get to a point where I can’t understand what actually happens. I have 3 files
One with Main() :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ToDoList
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
one to work with the fields of the form:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class ToDoEntry
{
public string Title { get; set; }
public string Description { get; set; }
public DateTime DueDate { get; set; }
}
And the form itself :
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 ToDoList
{
public partial class Form1 : Form
{
private BindingList<ToDoEntry> entries = new BindingList<ToDoEntry>();
public Form1()
{
InitializeComponent();
entriesSource.DataSource = entries;
CreateNewItem();
}
private void bindingSource1_CurrentChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void CreateNewItem()
{
ToDoEntry newEntry = (ToDoEntry)entriesSource.AddNew();
newEntry.Title = "New entry";
newEntry.DueDate = DateTime.Now;
entriesSource.ResetCurrentItem();
}
private void entriesSource_ListChanged(object sender, ListChangedEventArgs e)
{
switch (e.ListChangedType)
{
case ListChangedType.ItemAdded:
MakeListViewItemForNewEntry(e.NewIndex);
break;
case ListChangedType.ItemDeleted:
RemoveListViewItem(e.NewIndex);
break;
case ListChangedType.ItemChanged:
UpdateListViewItem(e.NewIndex);
break;
}
}
private void MakeListViewItemForNewEntry(int newItemIndex)
{
ListViewItem item = new ListViewItem();
item.SubItems.Add("");
entriesListView.Items.Insert(newItemIndex, item);
}
private void UpdateListViewItem(int itemIndex)
{
ListViewItem item = entriesListView.Items[itemIndex];
ToDoEntry entry = entries[itemIndex];
item.SubItems[0].Text = entry.Title;
item.SubItems[1].Text = entry.DueDate.ToShortDateString();
}
private void RemoveListViewItem(int deletedItemIndex)
{
entriesListView.Items.RemoveAt(deletedItemIndex);
}
}
}
My problem is with saying that The name entriesListView does not exist in the current context. Which is true, but for example entriesSource.DataSource = entries;
I don’t have entriesSource either but for some reason I can use it. Now I’m not sure if the missing class(at least I think it should be a class) is something that VS2010 (Yes I’m using Visual Studio 2010) should generate but then – why it hasn’t.
Is something that I should write manually but then, there’s nothing about this in the example and I too have no idea how to define entriesListView.
These variables are being created by Visual Studio in a designer file. This is done through the use of the
partialkeyword, which is a feature in C# that allows you to split a class definition into two or more files.The designer file will be called
Form1.designer.csin this instance. You can see which controls have been created by opening this file, or, if using Visual Studio, by opening the form in the Visual Studio Designer. To open the designer file in Visual Studio, expand theForm1.csentry in the Solution Explorer TreeView, and double-click the designer file.In your code above, the problem appears to be that the
entriesListViewvariable does not exist. It is possible that it was created at one point, and then deleted (this can even happen inadvertently due to Visual Studio bugs). Adding a new ListView to your form and setting theNameproperty toentriesListViewshould correct the problem.