在下面的代码中,
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace clone_test_01
{
public partial class MainForm : Form
{
public class Book
{
public string title = "";
public Book(string title)
{
this.title = title;
}
}
public MainForm()
{
InitializeComponent();
List<Book> books_1 = new List<Book>();
books_1.Add( new Book("One") );
books_1.Add( new Book("Two") );
books_1.Add( new Book("Three") );
books_1.Add( new Book("Four") );
List<Book> books_2 = new List<Book>(books_1);
books_2[0].title = "Five";
books_2[1].title = "Six";
textBox1.Text = books_1[0].title;
textBox2.Text = books_1[1].title;
} …Run Code Online (Sandbox Code Playgroud) 例如
List<string> name_list1 = new List<string>();
List<string> name_list2 = new List<string>();
Run Code Online (Sandbox Code Playgroud)
稍后在代码中:
name_list1.Add("McDonald");
name_list1.Add("Harveys");
name_list1.Add("Wendys");
name_list2 = name_list1; // I make a copy of namelist1 to namelist2
Run Code Online (Sandbox Code Playgroud)
所以,从这一点来说,我想继续添加元素或在name_list2中进行更改,而不会影响name_list1.我怎么做?
我通过构造函数传递了一个客户列表.然后它被数据绑定到ListBox.我还将数据框数据转换为允许更改客户名称,它会自动更新ListBox和客户列表,这非常好.
但是,如果用户单击"取消"按钮,我希望不保留更改.我告诉程序将客户列表设置为旧的,但它不起作用,当我再次打开窗口时,ListBox显示更新的客户名称而不是旧名称.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
namespace Bingding_Test
{
public partial class Form_Customers : Form
{
List<Customer> customers;
List<Customer> old_customers;
BindingSource bs = new BindingSource();
public Form_Actors(List<Customer> _customers)
{
InitializeComponent();
customers = _customers;
old_customers = new List<Customer>(_customers);
bs.DataSource = customers;
listBox1.DataSource = bs;
listBox1.DisplayMember = "Name";
txtb_name.DataBindings.Add("Text", bs, "Name");
}
void Btn_cancelClick(object sender, EventArgs e)
{
actors = old_customers;
this.Close();
}
void Btn_saveClick(object sender, EventArgs e)
{
this.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都知道我可以做什么来确保单击取消按钮时不保存所有更改?
单击一次按钮时,不希望发生事件,我希望仅在双击按钮时才会发生该事件.遗憾的是,双击事件不会出现在IDE中的事件列表中.
有谁知道这个问题的好方法?谢谢!
可能重复:
C#强制ListBox更新元素
让我们考虑一下这段小代码:
listbox.DataSource = base_items;
listbox.DisplayMember = "Name";
// ... a bit later in the program
base_items.Add( new Base_Item("Unnamed") );
Run Code Online (Sandbox Code Playgroud)
从这一点来说,如何使列表框更新其项目?我看到更新的唯一方法是关闭窗口并重新加载它.
首先,很难为我的问题找到合适的标题......对不起提前做好准备.
我正面临着C#.NET的问题.我将尝试用一个临时的例子来简化它.
我有三个班:
book movie_dvd game_dvd
它们中的每一个都具有相同的基本属性,例如:id和name.然而,我们可以假设它们具有其他不同的属性和不同的方法来成为同一个类.他们当然可以成为父母班级的孩子,以使其更好.
然后我创建每个类的列表来定义我的书籍,电影和游戏的集合.它们的id属性当然与List的键匹配,当我在List中插入每个项目时,我给它们一个合适的名字(它们的标题).
我有一个带有3个按钮的主winForm:"Show Books Collection","Show Movies Collection"和"Show Games Collection".用户可以单击其中一个,然后出现另一个窗口并在ListBox(或其他)中显示传递的集合.
现在这是我的问题.
现在我正在创建三个独立的(但几乎相同的)WinForms来显示每个集合(书籍,电影,游戏)的内容,我认为这些内容非常过分.我真正想做的是创建一个窗口来显示任何这些集合的内容.问题是,如果我在新winForm的参数内传递一个book对象/类,另一方面它应该是一个book对象.
如何在不重复WinForm代码的情况下使用单个Winform显示每个不同对象的标题?
这是来自使用2d数组的C#项目的一段代码.出于某种原因,我不明白我的程序编译完美,但在运行期间崩溃.
public class Tile_Info
{
public int id;
public Tile_Info(int _id)
{
id = _id;
}
}
class Program
{
public static void Main(string[] args)
{
int width = 20;
int height = 30;
Tile_Info[,] my_tile;
my_tile = new Tile_Info[width, height];
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
my_tile[x, y].id = 0;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
根据调试器,它是因为"对象引用未设置为对象的实例",但我很确定这就是我在这里所做的事情:my_tile = new Tile_Info[width, height];.
谁能告诉我哪里错了?感谢您的支持!
在 C# 中,我们可以在做其他事情之前确定 List 所持有的类型吗?例子:
List<int> listing = new List<int>();
if(listing is int)
{
// if List use <int> type, do this...
}
else if(listing is string)
{
// if List use <string> type, do this...
}
Run Code Online (Sandbox Code Playgroud) 我刚刚遇到一个奇怪的问题,这让我想知道为什么突然发生这种类型的错误,例如看看这段代码:
if(age == 0 || age == 47 || age = 99)
{
name = "Those are lucky numbers!";
}
else
{
name = "Try again...";
}
Run Code Online (Sandbox Code Playgroud)
错误是:错误CS0019:运算符'||' 不能应用于'bool'和'int'类型的操作数.
什么......?呵呵......我的意思是类型'bool'和'int'是我们想要用于这种操作的最常见的类型.他们为什么不工作?
此外,我确信我之前已经完成了数千次,我不明白为什么今天它会发生......或者我可能用Ruby语言做到了这一点.我仍然真的有C#以前的感受.
有人有合理的解释吗?我今天想念的东西?:d