How to save settings to an internal config in visual C# (.NET)

In this tutorial I teach you how to save settings to an internal config!

Saving settings to a config is really easy in C#, so let’s begin the tutorial!

1. Right-click on the project root in the Solution Explorer:

Capture

2. Select “Properties” from the list:

Capture

3.  You should see something similar to this:

Capture

4. Set the names, types, and default values to whatever you like. DO NOT change the scope to “Application” if you plan on being able to edit the settings from the code.

5. Getting and Setting the property:
    Getting:  [Variable Type] [Variable Name] = Properties.Settings.Default.[Name of Property];

    Setting:  Properties.Settings.Default.[Name of Property] = [String, Int, etc. (As long as it is the same type as the property)];

Capture

In this example, I am checking if the config property LastSaveDirectory is not empty. If it is not empty, set the SaveFileDialog‘s Initial Directory to the stored property.

 

*BONUS*

How to loop through all Properties and display them in a MessageBox:

Capture

NOTE: I would recommend a String Builder if you plan on having a lot of properties.

Going border-less full-screen in visual C# (.NET)

Going border-less full-screen in C#.NET is actually really, really easy.

1. Set the FormBorderStyle to None: FormBorderStyle = FormBorderStyle.None;

2. Set the WindowState to Maximized: WindowState = FormWindowState.Maximized;

You may or may not have to adjust the width/height and location of various controls on the form.

To return the form to normal:

1. Set the FormBorderStyle to Sizable: FormBorderStyle = FormBorderStyle.Sizable;

2. Set the WindowState to Normal: WindowState = FormWindowState.Normal;

Again, you may or may not have to adjust various properties of controls on the form after doing this.

 

NOTE: I only have to adjust a few properties when going full-screen, and the form returns said properties to their original state when returning to normal mode.

My first programming language

My first programming language was Python. I didn’t choose it because someone told me it was the easiest to start with or because of it’s vast collection of libraries and frameworks. I chose it because I thought the name was cool. That’s it. (Was in 8th grade at the time)

Looking back, I am so glad I chose Python. I used to be an instant gratification monkey, and Python was easy enough to pick up quickly. The vast collection of libraries and frameworks mentioned earlier prevented me from losing interest for quite some time.

I know I said Python was my first programming language, but this is only partial because of two distinct facts:

1. I started scripting in batch before Python. I knew the windows command line like the back of my hand as well as batch scripting before I touched Python.

2. I wasn’t super passionate about programming until I learned JavaScript. Sure, Python was cool and all, but I stagnated before learning OOP with it, unlike JavaScript. It’s funny because JS classes are really pseudo-classes, and I learned the basics of OOP through them. My sudden break-through understanding of OOP gave me a huge motivation boost to learn more and more and to try new things.

This is why I consider batch my first scripting language, Python my first programming language, and JavaScript my first true understanding and conquering of programming basics.

The best case ever

There are so many different types of casing when it comes to variable names, but which one is the best you ask? Why, SCREAMING_SNAKE_CASE, of course.

Let’s compare a few cases just to show why screaming snake case is the best.

1. Kebab-Case

2. Snake_Case

3. SCREAMING_SNAKE_CASE

4. CamelCase

Screaming snake case really puts emotion into the code. It let’s the next person that works on your code know that you really felt strongly about the variable or function name you chose.

The other cases are cool and all, but none of them portray raw human emotion, meaning, and readability as well as screaming snake case.

 

What’s your favorite case? Tell me in the comments below!

write twice, method once

My brother Genji always says, “Measure twice, cut once”. After hearing him say this enough, I tried to apply it to my life and hobbies, and realized that in programming it’s the opposite. You don’t want to reuse code if you don’t have to. Let me show the full title now. Never write twice, method once. I realize that looks weird now, but the point still stands. If you have to use a piece of code more than once, you should probably make a method/function for it.
Let’s look at an example

Say you wanted to print out the contents of an array at multiple points within your program. Instead of having multiple for loops, you could just create one function, like this.

public void printArray(string[] myArray)
{
  foreach (string s in myArray)
  {
    Console.WriteLine(s);
  }
}

Writing text to files in visual C# (.NET)

Ok, ok. Writing text to files with C# is pretty easy. You just throw in that good old System.IO.File.WriteAllLines([path], [string array]);

But, what if you don’t want to explicitly ask the user (or yourself) for the path every time?
That’s where the SaveFileDialog class comes in handy.
Instead of having to ask the user for the path each time, you can simply do this instead:
1. Instantiate the class: SaveFileDialog SFD = new SaveFileDialog();
2. Set a filter if you so desire: SFD.Filter = "Text File|*.txt|All Files|*.*"; (This causes that handy-dandy drop-down of different file types to appear like when you save files with notepad and other editors.)
3. Set the title of the save dialog: SFD.Title = "Save the File";
4. Show the save dialog: SFD.ShowDialog();
5. Check to make sure the file name is not empty: if (SFD.FileName != "") { System.IO.File.WriteAllLines(SFD.FileName, [string array]); }
Notice how you never have to prompt the user for the file path, you can easily retrieve it by referencing SFD.FileName, as the file name is actually the path to the file, including it’s name.


For opening files, you can use the OpenFileDialog which works almost exactly like SaveFileDialog.

Introduction

Disclaimer: I have absolutely no connection with Blizzard or the Overwatch team. I am simply a fan of Overwatch.

Hello, class. My name is Hanzo Shimada, and I am here to teach you about programming. But, before I do that, I would like to give you some back story.
After reuniting with my brother Genji, I decided to do something with the spare time I gained from nolonger praying at the alter for my brother. I pondered for many, many nanoseconds before deciding on following in my cousin’s friend’s older brother’s footsteps. I decided to become a programmer.

Next time on Hanzo Programs: Idk yet, structure is boring. Probably something to do with programming.