Welcome to the fascinating world of C# programming! C# (pronounced “C-Sharp”) is a versatile and powerful programming language developed by Microsoft. It plays a pivotal role in the development of various applications, ranging from simple desktop programs to complex web and mobile applications. This section will introduce you to the basics of C#, its significance […]
Get the clicked button of a MessageBox using DialogResult
The MessageBox dialog can contain several buttons: OK, Cancel, Yes, No, Retry and so on. The question is how do you figure out which of the buttons was clicked? If OK was clicked, you want to take a different action than if Cancel was clicked. The solution is to use the DialogResult object: DialogResult dlgResult = MessageBox.Show(“Do you want to continue?”, “Continue?”, MessageBoxButtons.YesNo, MessageBoxIcon.Question);if […]
How to convert a variable from double to float
A variable of type double can easily be converted to float or any other similar data type, in C#:
Object cannot be cast from DBNull to other types
This is a common error that you get when trying to display a field with no value (null), from a table. If for example you have a field where you store the age of the employee, but you don’t know the age of all employees, and thus those fields are set to NULL. Trying to retrieve […]
Using EnsureVisible() to scroll down to the bottom of a ListView
You can easily programatically scroll to the bottom of a ListView using the EnsureVisible() method. This method takes 1 parameter – the index of the ListViewItem that you want to ensure visibility for. The trick to keep the ListView scrolled down is to use the EnsureVisible() method every time a new item is added to the list, and pass that item’s index as a parameter. […]
Items collection cannot be modified when the DataSource property is set
You have a control that supports data binding (such as a DropDownList or a DataGrid) which is binded to a DataSource (such as a database table, an XML file or an array). If you try to add new items to it after you binded to a DataSource, you will get the error Items collection cannot be […]
XmlException: Root element is missing
The cause of the XmlException entitled Root element is missing means the XML document you’re trying to load is not formatted properly, more exactly it’s missing the root node.Each XML file must have a root element / node which encloses all the other elements. The following is an example of an XML file which is not properly formed: And […]
Remove selected items from a ListBox
Suppose you have a ListBox named listBox1. If you want to remove the selected item from it, use the Items.Remove() method, and pass as an argument the SelectedItem property: listBox1.Items.Remove(listBox1.SelectedItem); However if the ListBox has the SelectionMode set to MultiExtended and you want to remove all the selected items, use the following code: while(lstKeys.SelectedItems.Count > 0){lstKeys.Items.Remove(lstKeys.SelectedItem);}
What is the difference between the int and Int32 datatype, or String and string (lowercase)?
Int32 is the System.Int32 class, while int is an alias for System.Int32.The same applies for String (uppercase S) which is System.String, while string (lowercase S) is an alias for System.String.So basically int is the same thing as Int32, and string is the same thing as Int32. It’s down to user’s preference which one to use but most prefer to use int and string as they are easier to type and more familiar among C++ programmers.
How do I generate a random number within a range?
Using .NET you can easily generate a random number withing a range, by using the Random class. In the following example a random number between 1 and 69 will be generated: System.Random RandNum = new System.Random();int MyRandomNumber = RandNum.Next(69); To generate a random number between 1986 and 2005 you would use: System.Random RandNum = new System.Random();int MyRandomNumber = RandNum.Next(1986, […]