c#-2.0

C# winform how textbox became null on button click second time?

眉间皱痕 提交于 2019-12-23 15:35:26
问题 In a form I have group Box which contains tab control with 4 tabs. In the second tab I have some textboxes and before saving data I need to validate input entered in these textbxes. Please note my save button is in last tab. The following test scenario works: Invalid input in first textbox Invalid input in second textbox Click button However, in the following test scenario an "Object refrence not set to an instance of a object" exception is thrown: Invalid input in first textbox Click button

Should empty “if” statement in C# lead to an error or warning?

这一生的挚爱 提交于 2019-12-23 12:27:14
问题 Let me start from a real life example: Customer: Alex, just noticed something strange in the RemovalProcessor at line 138: if (Session.Handler.ExecutePrefetchTasks()==null); Session.ExecuteDelayedQueries(); Should the semicolumn behind the 'if' be there? Me: Oops... I'll send this to our guys to check, but most likely, you're right. Although the case is rare, I admit nearly any big project has similar issue. I understand that semicolon (and statement block) usage rules in C# can't be changed

(DataGridView + Binding)How to color line depending of the object binded?

ε祈祈猫儿з 提交于 2019-12-23 12:22:28
问题 I would like to add a backcolor for specific line depending of a Property of the object binded. The solution I have (and it works) is to use the Event DataBindingComplete but I do not think it's the best solution. Here is the event: private void myGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { for (int i = 0; i < this.myGrid.Rows.Count; i++) { if((this.myGrid.Rows[i].DataBoundItem as MyObject).Special) { this.myGrid.Rows[i].DefaultCellStyle.BackColor = Color

Hot to change specific column header color only in datagridview?

馋奶兔 提交于 2019-12-23 09:38:32
问题 Uses: VS 2005, C#, DataGridView, WinForms; I need to color the font/background of a particular column's Header portion. I see that it can only be done to the entire column list's header instead of a single column. Any help greatly appreciated. 回答1: First in your DataGridView you need to set EnableHeadersVisualStyles to false. After you've done that you can set the individual header style on each column. DataGridViewColumn dataGridViewColumn = dataGridView1.Columns[0]; dataGridViewColumn

How does the AddExtension property work in C# 2.0?

江枫思渺然 提交于 2019-12-23 07:57:49
问题 I want to open a save file dialog, have the user enter a filename, and if they forget the .csv extension, have it tacked on. It would seem that the SaveFileDialog AddExtension property would work, but its doesn't. I've even set the DefaultExt property to .csv, and still nothing gets tacked on. My file gets saved just fine, but sans extension, so the user can't just double click on the file and have it open in Excel. I have to be missing something obvious. Here's what I've got SaveFileDialog

How to reference a field by reflection

时光总嘲笑我的痴心妄想 提交于 2019-12-22 17:46:10
问题 Sorry for the title, it's not explicit. Further to my precedent question, I want to subscribe a method to an event object retrieved dynamically (via reflection). The object in question is a field of a Control : public void SubscribeEvents(Control control) { Type controlType = control.GetType(); FieldInfo[] fields = controlType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); MethodInfo method = typeof(Trace).GetMethod("WriteTrace"); // "button1" hardcoded for

Fastest way to find out whether two ICollection<T> collections contain the same objects

拈花ヽ惹草 提交于 2019-12-22 08:02:23
问题 What is the fastest way to find out whether two ICollection<T> collections contain precisely the same entries? Brute force is clear, I was wondering if there is a more elegant method. We are using C# 2.0, so no extension methods if possible, please! Edit: the answer would be interesting both for ordered and unordered collections, and would hopefully be different for each. 回答1: use C5 http://www.itu.dk/research/c5/ ContainsAll " Check if all items in a supplied collection is in this bag

set Enums using reflection

非 Y 不嫁゛ 提交于 2019-12-22 07:03:17
问题 How to set Enums using reflection, my class have enum : public enum LevelEnum { NONE, CRF, SRS, HLD, CDD, CRS }; and in runtime I want to set that enum to CDD for ex. How can I do it ? 回答1: public class MyObject { public LevelEnum MyValue {get;set,}; } var obj = new MyObject(); obj.GetType().GetProperty("MyValue").SetValue(LevelEnum.CDD, null); 回答2: Try use of class Enum LevelEnum s = (LevelEnum)Enum.Parse(typeof(LevelEnum), "CDD"); 回答3: value = (LevelEnum)Enum.Parse(typeof(LevelEnum),"CDD");

Capturing KeyDown events in a UserControl

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 05:14:12
问题 I have a user control with several child controls. I need the user interface to react to keypresses, so I decided to put the handling code in a MainControl_KeyDown event. However, when I press a key in my application, this event does not fire. I have found a solution through a search engine which relies upon use of the Windows API, which I would like to avoid, as it seems like overkill for what should be a function that is properly supported by the .NET framework. 回答1: You could add a KeyDown

Capturing KeyDown events in a UserControl

早过忘川 提交于 2019-12-22 05:14:11
问题 I have a user control with several child controls. I need the user interface to react to keypresses, so I decided to put the handling code in a MainControl_KeyDown event. However, when I press a key in my application, this event does not fire. I have found a solution through a search engine which relies upon use of the Windows API, which I would like to avoid, as it seems like overkill for what should be a function that is properly supported by the .NET framework. 回答1: You could add a KeyDown