Category: C#

Mastering C#: A Beginner’s Journey to Programming Excellence

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 […]

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. […]

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, […]

Back To Top