Simplifying string comparisons

and other program areas

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 1149 Views - Last Post: 01 August 2009 - 05:19 PM Rate Topic: -----

#1 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Simplifying string comparisons

Post icon  Posted 22 July 2009 - 03:45 PM

I am writing a small program to teach myself the language the help me control some values in a text file, but I also have my program writing/reading a settings file which I got it to make. So far things work well but I want to simplify it.

For instance I have a menu with two choices: auto or manual. Right now my code look slike this:

				//convert second position in arlSettingsINIContent to a string and check if it contains the string "Auto"
				//if it does set application mode to auto otherwise set application mode to manual
				if (arlSettingsINIContent[1].ToString().Contains("Auto"))
				{
					menuModeAuto.Checked = true;

					tabmenuManualOptions.Enabled = false;
					tabmenuManualOptions.Visible = false;

					//set form size to match default form size
					this.Size = new System.Drawing.Size(240, 150);
				}
				else
				{
					menuModeManual.Checked = true;

					tabmenuManualOptions.Enabled = true;
					tabmenuManualOptions.Visible = true;

					//if manual mode is selected resize the form to fit manual controls
					this.Size = new System.Drawing.Size(240, 360);
				}


Is there a faster way to determine which value was part of the string that get read into my program? It can only be auto or manual. I thought I could use a switch but it won't let me switch with a blank Contains or EndsWith method. How else can I simplify this?

Also, for my classes I all have the beginning like this:

namespace programname
{
	public partial class formname
	{


And I was wondering if that is correct or if they can be used with their own class names. It seems if I don't crate them like I have them they do not work, unless I am missing something of course.

I took one intro to c# course but that was only console apps, this is a Windows form app that I have been able to work out so far minus those two things, for now.

Thanks for any help/advice.

This post has been edited by blank_program: 23 July 2009 - 10:22 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Simplifying string comparisons

#2 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 22 July 2009 - 06:32 PM

Here's another way to accomplish your goal:

   // flip between auto/manual mode
   bool isAutoMode = arlSettingsINIContent[1].ToString().Contains("Auto");
   menuModeAuto.Checked = isAutoMode;
   menuModeManual.Checked = !isAutoMode;
   tabmenuManualOptions.Enabled = !isAutoMode;
   tabmenuManualOptions.Visible = !isAutoMode;
   
   // adjust form size to accommodate manual mode;
   if (isAutoMode)
      this.Height = 150; // default size
   else
      this.Height = 360; // adjusted to fit the manual controls



You can even shorten the width logic into a ternary operator if you like that style:
this.Height = (isAutoMode) ? 150 : 360;

You can get away with just changing only the Width property as opposed to changing the Size property of the form because if you're using Visual Studio, the size of the form will already be defined and changeable. However, if you're hardcore and you're creating the form in a text editor like notepad, then you might need to use the System.Drawing.Size statement like you have been.

In terms of your namespace/class name dilemma, you need to elaborate a little more on what issues you're running into. If you mean that after you create a project you try to rename your namespace or class names and it doesn't compile, then you're probably not renaming every reference to these namespaces/class names. I.E. if your form's name property is "formname", just change it to something like "MyFirstForm" in the form's Properties window and it should automatically rename every reference I think.

If you're wondering about the "partial" keyword in public partial class formname statement, all that does is allow you to spread out the method definitions across different files, which can be helpful if multiple developers need to work on this same class.

I personally prefer camel casing the namespaces and class names, i.e.:

namespace ProgramName
{
    public partial class FormName
    {
    }
}



Edit: if your string can only be "Auto" or "Manual", then you can do this:
bool isAutoMode = arlSettingsINIContent[1].ToString() == "Auto";
or
bool isAutoMode = arlSettingsINIContent[1].ToString().Equals("Auto");

This post has been edited by masteryee: 22 July 2009 - 07:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 10:07 AM

Would I be able to use the same logic for this part of my code as well?
 //determine which mode setting is checked and write the setting AppMode to match selection
		   if (menuModeAuto.Checked == true)
				{
					swSaveSettingsINI.WriteLine(strAppMode + "Auto");
				}
				else
				{
					swSaveSettingsINI.WriteLine(strAppMode + "Manual");
				}


So when I do Windows forms all my classes should use partial public class and the form name? Or can they be different and just referenc the form like so:
 public partial class classname : formname


I noticed when I just use the form name or even that way in the solution explorer the classes get the icon representing a form and if double clicked actually have one assigned to them even if not used. Is this normal for that to happen?

When I had a VB course the instructor always said to abbriviate whatever the variable or control so its easier to tell what it is and allow you to use similar names for related controls, so I have stuck with that and keep that lowercase, but each other word in the name gets capitalized. Is this a good practice for naming conventions as well?

I am still learning so I do have room to change but it may not happen over night.

This post has been edited by blank_program: 23 July 2009 - 10:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 12:40 PM

Sorry, I may have gotten ahead of myself since I you've only been through one programming class. I'll try and explain things a little more, but your best bet is to look around for tutorials that can teach you the basics on winforms and programming in general since your head is probably buzzing with questions.

Quote

Would I be able to use the same logic for this part of my code as well?


You don't need to explicitly compare to true or false, so can use "if (menuModeAuto.Checked)" or you can use my variable: "if (isAutoMode)"

Quote

So when I do Windows forms all my classes should use partial public class and the form name? Or can they be different and just referenc the form like so


No. When you create a form in Visual Studio 2005 and up, it will automatically separate your form class into 2 partial classes, one of which will contain initialization code that is automatically generated for you when you add buttons and labels and other components and events to your form in design mode. The other partial class is what you (the developer) work with to define your custom methods. Leave the partial keyword alone for now until you're more comfortable with the concept.

And no, when you use the format "public partial class classname : formname", you're using an object-oriented principle known as inheritance. That's what the colon does in this statement: it causes classname to derive from formname, so you're essentially creating another form that inherits certain properties and functionality of the parent form. Look into "inheritance" and it will describe this concept more in detail.

You need to look into how to use namespaces when creating different classes. The way to link up other files and classes is through namespaces. Here's file 1 in a project:

namespace One
{
   public class MyOwnMathClass
   {
      public MyOwnMathClass()...
   }
}



Here's file 2:
using One;

namespace FormNamespace
{
   public partial class Form1
   {
      public Form1()
      {
         MyOwnMathClass mathClass = new MyOwnMathClass();
      }
   }
}




Quote

When I had a VB course the instructor always said to abbriviate whatever the variable or control so its easier to tell what it is and allow you to use similar names for related controls, so I have stuck with that and keep that lowercase, but each other word in the name gets capitalized. Is this a good practice for naming conventions as well?


To each their own. In terms of prefixing an abbreviation, my workplace does that for our Delphi code because of our coding standards (in Delphi, I would have used bIsAutoMode), but for C#, I strayed away. Typically, I might only do this for components (btnAdd, lblUsername, tbDisplayName), but for normal variables I just try to give them meaningful names, hence isAutoMode, which represents a boolean flag if we're in auto mode or not. I lower case the first word, then uppercase the following words, and this is common practice for C# fields (aka variables) and parameters. However, I start class properties with an uppercase letter (if you don't know the different between a field and property, look it up).

Prefixing a variable name with "is", "can", "has" is common to represent booleans. Look into coding standards and practices, and this will fall under the "readability" section.
Was This Post Helpful? 0
  • +
  • -

#5 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 01:11 PM

When I change it to just be puclib class classname then I have no access to any ontrols on my form which is where I run into the problem. I have extra classes to more organize my work. Each class deals with a specific section of the form and code.
Was This Post Helpful? 0
  • +
  • -

#6 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 02:06 PM

I think I know what you're trying to do, and that's not how programming works. However, if I'm wrong and you're trying to access them through a variable of your formname type, then you can't see them because of a concept known as encapsulation, which prevents you from being able to see privately declared variables, and your controls are private by default.

Off the top of my head, the only way to do this is to have your other class methods accept the components/controls as parameters, i.e. public void WriteNameInTextBox(TextBox tbDisplay). If your form has a text box called tbName, pass it in to the method: myClassObj.WriteNameInTextBox(tbName). You will need to add "using System.Windows.Forms;" to the top of your class file if you do it this way.

Another way to do this is through the use of an intermediate-level concept known as delegates, but you're not quite at that stage yet. Baby steps...

Edit: sorry, I forgot to clarify that you need to declare objects of your other classname classes within the formname class. So in your formname class you have MyClass myClassObj = new MyClass(), and then afterward you can use myClassObj.WriteNameInTextBox(tbName); within the formname class. Clear as mud?

This post has been edited by masteryee: 23 July 2009 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 02:18 PM

View Postmasteryee, on 23 Jul, 2009 - 01:06 PM, said:

I think I know what you're trying to do, and that's not how programming works. However, if I'm wrong and you're trying to access them through a variable of your formname type, then you can't see them because of a concept known as encapsulation, which prevents you from being able to see privately declared variables, and your controls are private by default.

Off the top of my head, the only way to do this is to have your other class methods accept the components/controls as parameters, i.e. public void WriteNameInTextBox(TextBox tbDisplay). If your form has a text box called tbName, pass it in to the method: myClassObj.WriteNameInTextBox(tbName). You will need to add "using System.Windows.Forms;" to the top of your class file if you do it this way.

Another way to do this is through the use of an intermediate-level concept known as delegates, but you're not quite at that stage yet. Baby steps...

Edit: sorry, I forgot to clarify that you need to declare objects of your other classname classes within the formname class. So in your formname class you have MyClass myClassObj = new MyClass(), and then afterward you can use myClassObj.WriteNameInTextBox(tbName); within the formname class. Clear as mud?

So in my Main class where it initializes the form I should make one method to send the form object (button for instance) to the class that will use it?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5943
  • View blog
  • Posts: 12,871
  • Joined: 16-October 07

Re: Simplifying string comparisons

Posted 23 July 2009 - 03:03 PM

If it's bool, then this "if (menuModeAuto.Checked == true)" is redundant and possibly confusing. Prefer this "if (menuModeAuto.Checked)".

Also, look for repetion and see where you can kill it, e.g.
swSaveSettingsINI.WriteLine(strAppMode + (menuModeAuto.Checked ? "Auto" : "Manual"));




View Postblank_program, on 23 Jul, 2009 - 11:07 AM, said:

When I had a VB course the instructor always said to abbriviate whatever the variable or control.


Always err on the side of clarity. Quite simply, there are too many types to for some kind of Hungarian notation thing to work. However, whatever you do, be consistent. Lowercase names for classes and types is not part of the accepted standard for C#. Unlike VB, you can declare a class and an instance and be clear, e.g. "Person person;"
Was This Post Helpful? 0
  • +
  • -

#9 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 23 July 2009 - 03:07 PM

Here's an example. Let's say I have button1 and textbox1. If I click on button1, your external class will run a method to add a string to textbox1. It would be like something of this nature:
namespace ProgramName
{
    MyClass myClassObj;

    public partial class FormName
    {
        public FormName()
        {
            InitializeComponent();
            myClassObj = new MyClass();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            myClassObj.WriteNameInTextBox(textbox1); 
        }
    }
}



I hope this is clear enough. I'm leaving it up to you to write the WriteNameInTextBox method.
Was This Post Helpful? 0
  • +
  • -

#10 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 24 July 2009 - 08:42 PM

View Postmasteryee, on 23 Jul, 2009 - 02:07 PM, said:

Here's an example. Let's say I have button1 and textbox1. If I click on button1, your external class will run a method to add a string to textbox1. It would be like something of this nature:
namespace ProgramName
{
    MyClass myClassObj;

    public partial class FormName
    {
        public FormName()
        {
            InitializeComponent();
            myClassObj = new MyClass();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            myClassObj.WriteNameInTextBox(textbox1); 
        }
    }
}



I hope this is clear enough. I'm leaving it up to you to write the WriteNameInTextBox method.

I dont't quite understand. Here is the start of one of my classes that has an event from a menu option on the main form
	public class Controller_ModeMenu
	{
		//when menuModeAuto is clicked if the value is NOT true set the Checked property to True
		//to ensure only one option is selected set Checked property of menuModeManual to false
		private void menuModeAuto_Click(object sender, EventArgs e)
		{
			if (menuModeAuto.Checked != true)
			{
				menuModeAuto.Checked = true;
				menuModeManual.Checked = false;
			}


I am not sure why it still tells me menuModeAuto is wrong. I even tried adding
menuModeAuto = new Controller_ModeMenu()

to the main class after form initialization and it didn't work.

Am I missing something here that I probably should be getting?
Was This Post Helpful? 0
  • +
  • -

#11 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 25 July 2009 - 11:28 AM

If that's all you're trying to do though, just leave that click event code in your form class instead of putting it in an external class. You're making things harder than they should be. Keep it simple. It still looks like you're trying to access your form's controls outside of your form class illegally; not only that, but if your form already has a control called menuModeAuto, and it's a menu item, you can't just reassign it to a new object of a completely different data type (Controller_ModeMenu).

I applaud your efforts in trying to teach yourself how to program, but in order for us to help you better, you need to post your ENTIRE code.

I can't tell if I'm helping or just confusing you more. If you're still taking a programming course or will be taking another course soon, you should ask your instructor for help.
Was This Post Helpful? 0
  • +
  • -

#12 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 25 July 2009 - 08:45 PM

View Postmasteryee, on 25 Jul, 2009 - 10:28 AM, said:

If that's all you're trying to do though, just leave that click event code in your form class instead of putting it in an external class. You're making things harder than they should be. Keep it simple. It still looks like you're trying to access your form's controls outside of your form class illegally; not only that, but if your form already has a control called menuModeAuto, and it's a menu item, you can't just reassign it to a new object of a completely different data type (Controller_ModeMenu).

I applaud your efforts in trying to teach yourself how to program, but in order for us to help you better, you need to post your ENTIRE code.

I can't tell if I'm helping or just confusing you more. If you're still taking a programming course or will be taking another course soon, you should ask your instructor for help.

Should I post the whole VS2008 solution in a .zip or just use code boxes for each class I created?
Was This Post Helpful? 0
  • +
  • -

#13 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Simplifying string comparisons

Posted 25 July 2009 - 09:18 PM

Attaching files probably isn't the best idea. Placing the code into code blocks would be my suggestion.
Was This Post Helpful? 0
  • +
  • -

#14 blank_program  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 284
  • Joined: 22-July 09

Re: Simplifying string comparisons

Posted 26 July 2009 - 08:22 AM

Here is each class. I know I did them wrong. I know I need all the events in Main and then to just have methods in the other classes, but how do I give them back their names then run the methods from Main when events are triggered?

Main.cs:
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger : Form
	{
		public formResolutionchanger()
		{
			InitializeComponent();
		}
	}
}


Controller_OptionsMenu.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger
	{
		//when Options > Backup > zsnesw.cfg is clicked backup of zsnesw.cfg will be created
		//filename of backup: zsnesw.bak
		//backup file will be overwritten if it exists
		private void zsneswcfgBackupMenuItem_Click(object sender, EventArgs e)
		{
			File.Copy("zsnesw.cfg", "zsnesw.bak", true);
			zsneswcfgRestoreMenuItem.Enabled = true;
		}

		//when Options > Restore > zsnesw.cfg is clicked backup file will replace current zsnesw.cfg file
		//file will be completely overwritten resetting any user changes to those stored in the backup
		private void zsneswcfgRestoreMenuItem_Click(object sender, EventArgs e)
		{
			File.Copy("zsnesw.bak", "zsnesw.cfg", true);
		}

		//when Options > Backup > settings.ini is clicked a copy of settings.ini will be created
		//filename of backup: settings.bak
		//backup file will be overwritten if it exists
		private void settingsiniBackupMenuItem_Click(object sender, EventArgs e)
		{
			if (File.Exists("settings.ini"))
			{
				File.Copy("settings.ini", "settings.bak", true);
				settingsiniRestoreMenuItem.Enabled = true;
			}
		}

		//when Options > Restore > settings.ini is clicked backup file will replace current settings.ini file
		//file will be completely overwritten resetting any user changes to those stored in the backup
		private void settingsiniRestoreMenuItem_Click(object sender, EventArgs e)
		{
			File.Copy("settings.bak", "settings.ini", true);
			LoadSettings();
		}

		//if menuOptionsClose is clicked application will exit
		private void menuOptionsClose_Click(object sender, EventArgs e)
		{
			Application.Exit();
		}

		// this method is used to check if any backup files exist and if so enable the restore menu options for the respective files
		public void EnableRestoreMenuOptions()
		{
			//if zsnesw.bak exists enable zsneswcfgRestoreMenuItem otherwise leave disabled
			switch (File.Exists("zsnesw.bak"))
			{
				case true:
					zsneswcfgRestoreMenuItem.Enabled = true;
					break;

				case false:
					zsneswcfgRestoreMenuItem.Enabled = false;
					break;

				default:
					break;
			}

			//if settings.bak exists enable settingsiniRestoreMenuItem otherwise leave disabled
			switch (File.Exists("settings.bak") == true)
			{
				case true:
					settingsiniRestoreMenuItem.Enabled = true;
					break;

				case false:
					settingsiniRestoreMenuItem.Enabled = false;
					break;

				default:
					break;
			}
		}
	}
}


Controller_SettingsINI.cs:
using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger
	{
		//strings to hold beginning values for each line in the settings.ini file for later output
		//strings not complete and require the addition of the checked values from both chkRunAfterSet and menuOptionsSaveSettingsOnExit
		string strRunAfterSet = "RunAfterSet=";
		string strAppMode = "AppMode=";
		string strScreenMode = "ScreenMode=";

		private void LoadProgram(object sender, EventArgs e)
		{
			//since backup files may be moved or deleted between program use run EnableRestoreMenuOptions()
			//the method checkes for backup files and enables the feature for the respective file
			EnableRestoreMenuOptions();

			//run the LoadSettings function to load program settings when form is opened
			LoadSettings();
		}

		//method to load settings from settings.ini file
		//also creates settings.ini if it does not exist
		public void LoadSettings()
		{
			ArrayList arlSettingsINIContent = new ArrayList();
			int intLineCount = 0;
			string strLineContent;

			//check if file settings.ini exists
			if (File.Exists("settings.ini"))
			{
				StreamReader srReadSettingsINILoad = new StreamReader(File.OpenRead("settings.ini"));
				do
				{
					//if current line is not null program reads it and stores the line in strLineContent then increments the line counter
					strLineContent = srReadSettingsINILoad.ReadLine();
					if (strLineContent != null)
					{
						intLineCount++;
						//when line counter is not zero copy the last line read into the list array
						if (intLineCount > 0)
						{
							arlSettingsINIContent.Add(strLineContent);
						}
					}
				} while (strLineContent != null);

				//close the reader to allow file access
				srReadSettingsINILoad.Close();

				//check first position in arlSettingsINIContent to determined if chkRunAfterSet should be checked or not
				//string conversion takes place here as opposed to using extra string variables
				if (arlSettingsINIContent[0].ToString().Contains("True"))
				{
					chkRunAfterSet.Checked = true;
				}

				//convert second position in arlSettingsINIContent to a bool and check if it contains the string "Auto"
				bool isAutoMode = arlSettingsINIContent[1].ToString().Contains("Auto");

				//set menu mode to match setting
				//if isAutoMode is true then disable and hide all manual controls and set program mode to Auto
				//otherwise program enters manual mode
				menuModeAuto.Checked = isAutoMode;
				menuModeManual.Checked = !isAutoMode;
				tabmenuManualOptions.Enabled = !isAutoMode;
				tabmenuManualOptions.Visible = !isAutoMode;

				//if application mode is Auto then fit form to controls, otherwise extend it to allow access to manual controls
				if (isAutoMode)
				{
					this.Height = 150;
				}
				else
				{
					this.Height = 360;
				}

				//convert third position in arlSettingsINIContent to a string and check if it contains the string Windowed
				//if it does set screen mode to windowed otherwise to fullscreen
				if (arlSettingsINIContent[2].ToString().Contains("Windowed"))
				{
					chkVideoModeWindowed.Checked = true;
				}
				else
				{
					chkVideoModeFullscreen.Checked = true;
				}
			}

			//if settings.ini does not exist default application mode to auto
			else
			{
				menuModeAuto.Checked = true;

				//set default window size, disable manual controls, and hide manual controls
				this.Height = 150;
				tabmenuManualOptions.Enabled = false;
				tabmenuManualOptions.Visible = false;
			}
		}

		//method for saving program settings when application is closing
		private void SaveSettings(object sender, FormClosingEventArgs e)
		{
			//create writer swSaveSettingsINI to write settings to settings.ini
			StreamWriter swSaveSettingsINI = new StreamWriter(File.Create("settings.ini"));

			//check to make sure settings.ini exists
			if (File.Exists("settings.ini"))
			{
				//write strRunAfterSet (which contains RunAfterSetting=) and add the checked property of chkRunAfterSet checkbox
				//then close the writer
				swSaveSettingsINI.WriteLine(strRunAfterSet + chkRunAfterSet.Checked);

				//determine which mode setting is checked and write the setting AppMode to match selection
				swSaveSettingsINI.WriteLine(strAppMode + (menuModeAuto.Checked ? "Auto" : "Manual"));

				//determind which screen mode was selected and write the setting ScreenMode to match selection
				swSaveSettingsINI.WriteLine(strScreenMode + (chkVideoModeWindowed.Checked ? "Windowed" : "Fullscreen"));

				swSaveSettingsINI.Close();
			}
			//if settings.ini does not exist close the application without saving changes
			else
			{
				Application.Exit();
			}
		}
	}
}



Controller_AppControls.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger
	{
		//method used to open and rewrite zsnesw.cfg file to match users current resolution settings
		//does NOT set the emulator to default to custom resolution but instead sets the value for the custom resolution setting
		private void btnSetResolution_Click(object sender, EventArgs e)
		{
			//variables relating to zsnesw.cfg line count
			//includes list array to store each line by copying each line read from strLineCount
			ArrayList arlLineContent = new ArrayList();
			int intLineCount = 0;
			string strLineContent;

			//variables used to store current display resolution
			int intHeight;
			int intWidth;

			//set display size variables to current screen resolution
			intHeight = SystemInformation.VirtualScreen.Height;
			intWidth = SystemInformation.VirtualScreen.Width;

			//begin reading zsnesw.cfg
			StreamReader srReadConfig = new StreamReader(File.OpenRead("zsnesw.cfg"));
			do
			{
				//if current line is not null program reads it and stores the line in strLineContent then increments the line counter
				strLineContent = srReadConfig.ReadLine();
				if (strLineContent != null)
				{
					intLineCount++;
					//when line counter is not zero copy the last line read into the list array
					if (intLineCount > 0)
					{
						arlLineContent.Add(strLineContent);
					}
				}
			} while (strLineContent != null);

			//stop reading zsnes.cfg and Close the reader to reallow file access
			srReadConfig.Close();

			if (menuModeAuto.Checked == true)
			{
				//replace users current ZSNESW.exe video settings and replace it with custom video mode with following attributes
				//D (allow filters) R (retain 8:7 aspect ration) and F (fullscreen)
				arlLineContent.RemoveAt(112);
				arlLineContent.Insert(112, "cvidmode=42");

				//modify custom resolution settings on lines 119 and 120 to match those of the current display resolution
				arlLineContent.RemoveAt(118);
				arlLineContent.Insert(118, "CustomResX=" + intWidth);
				arlLineContent.RemoveAt(119);
				arlLineContent.Insert(119, "CustomResY=" + intHeight);

				//create a writer names swWriteConfig to handle the rewriting of the file
				StreamWriter swWriteConfig = new StreamWriter(File.OpenWrite("zsnesw.cfg"));
				//write each line read of the file replacing each line with that stored in the list array
				for (int i = 0; i < intLineCount; i++)
				{
					swWriteConfig.WriteLine(arlLineContent[i]);
				}

				//end writing of zsnesw.cfg
				swWriteConfig.Close();
			}

			//if chkRunAfterSet checkbox is checked emulator will run immidiately following the writing of zsnesw.cfg
			if (chkRunAfterSet.Checked == true)
			{
				System.Diagnostics.Process.Start("zsnesw.exe");

				Application.Exit();
			}
		}

		//method to run the emulator when btnRunZSNES is clicked
		private void btnRunZSNES_Click(object sender, EventArgs e)
		{
			System.Diagnostics.Process.Start("zsnesw.exe");
			Application.Exit();
		}
	}
}


Controller_ModeMenu.cs:
using System;
using System.Text;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger
	{
		//when menuModeAuto is clicked if the value is NOT true set the Checked property to True
		//to ensure only one option is selected set Checked property of menuModeManual to false
		private void menuModeAuto_Click(object sender, EventArgs e)
		{
			if (menuModeAuto.Checked != true)
			{
				menuModeAuto.Checked = true;
				menuModeManual.Checked = false;
			}

			//hide and disable all manual controls when using auto mode
			tabmenuManualOptions.Enabled = false;
			tabmenuManualOptions.Visible = false;

			//use default form size
			this.Height = 150;
		}

		//when menuModeManual is clicked if the value is NOT true set the Checked property to True
		//to ensure only one option is selected set Checked property of menuModeAuto to false
		private void menuModeManual_Click(object sender, EventArgs e)
		{
			if (menuModeManual.Checked != true)
			{
				menuModeManual.Checked = true;
				menuModeAuto.Checked = false;
			}

			//when manual mode is selected show and enable all manual controls
			tabmenuManualOptions.Enabled = true;
			tabmenuManualOptions.Visible = true;

			//resize the form to allow all enabled manual controls to be shown in the application window
			this.Height = 360;
		}
	}
}


Controller_ManualVideoOptions.cs (incomplete):
using System;
using System.Text;
using System.Windows.Forms;

namespace ZSNESResolutionchanger
{
	public partial class formResolutionchanger
	{
		//when chkVideoModeWindowed is clicked if the value is NOT true set the Checked property to True
		//to ensure only one option is selected set Checked property of chkVideoModeFullscreen to false
		private void chkVideoModeWindowed_Click(object sender, EventArgs e)
		{
			chkVideoModeWindowed.Checked = true;
			chkVideoModeFullscreen.Checked = false;
		}

		//when chkVideoModeFullscreen is clicked if the value is NOT true set the Checked property to True
		//to ensure only one option is selected set Checked property of chkVideoModeWindowed to false
		private void chkVideoModeFullscreen_Click(object sender, EventArgs e)
		{
			chkVideoModeFullscreen.Checked = true;
			chkVideoModeWindowed.Checked = false;
		}

		//method that runs when the Checked property of chkCustomResolution is changed
		private void chkCustomResolution_CheckedChanged(object sender, EventArgs e)
		{
			//if Checked property of chkCustomResolution is true then enable labels and masked text boxes for both height and width
			if (chkCustomResolution.Checked == true)
			{
				lblCustomWidth.Enabled = true;
				lblCustomHeight.Enabled = true;

				mtxtCustomWidth.Enabled = true;
				mtxtCustomHeight.Enabled = true;

				//set default values for mtxtCustomWidth and mtxtCustomHeight to match users current resolution settings
				mtxtCustomWidth.Text = SystemInformation.VirtualScreen.Width.ToString();
				mtxtCustomHeight.Text = SystemInformation.VirtualScreen.Height.ToString();
			}
			//if Checked property of chkCustomResolution is false then disable all labels and  masked text boxes for both height and width
			else
			{
				lblCustomWidth.Enabled = false;
				lblCustomHeight.Enabled = false;

				mtxtCustomWidth.Enabled = false;
				mtxtCustomHeight.Enabled = false;

				//after disabling the text box set Text property for each text box to be empty
				mtxtCustomWidth.Text = "";
				mtxtCustomHeight.Text = "";
			}
		}
	}
}

Was This Post Helpful? 0
  • +
  • -

#15 masteryee  Icon User is offline

  • D.I.C Regular

Reputation: 40
  • View blog
  • Posts: 271
  • Joined: 16-May 09

Re: Simplifying string comparisons

Posted 26 July 2009 - 01:45 PM

Everything looks fine from here. Because you're using the same namespace and partial class name across all files, you should be able to access all of your form's controls, and you should also be able to move the events into either of the partial class definitions because the compiler will merge the partial classes together anyway. If you are getting a compile/runtime error, paste the error here.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2