Proper way to setup methods

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2007 Views - Last Post: 11 August 2009 - 12:06 PM Rate Topic: -----

#16 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Proper way to setup methods

Posted 11 August 2009 - 08:00 AM

do you mean your code is something similar to this?

private void BtnNotePadLaunch_Click(object sender, EventArgs e)
{
       //set working directory 
       ExternalAppLauncher.StartInfo.WorkingDirectory = AppLocations.AppLocation;
       ExternalAppLauncher.StartInfo.FileName = "notepad.exe";
       ExternalAppLauncher.Start();
}

private void BtnWordPadLaunch_Click(object sender, EventArgs e)
{
       //set working directory 
       ExternalAppLauncher.StartInfo.WorkingDirectory = AppLocations.AppLocation;
       ExternalAppLauncher.StartInfo.FileName = "WordPad.exe";
       ExternalAppLauncher.Start();
}

private void BtnChromeLaunch_Click(object sender, EventArgs e)
{
       //set working directory 
       ExternalAppLauncher.StartInfo.WorkingDirectory = AppLocations.AppLocation;
       ExternalAppLauncher.StartInfo.FileName = "chrome.exe";
       ExternalAppLauncher.Start();
}



where the only difference is the string that is being assigned to the "FileName"?
Was This Post Helpful? 1
  • +
  • -

#17 blank_program   User is offline

  • D.I.C Regular
  • member icon

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

Re: Proper way to setup methods

Posted 11 August 2009 - 08:19 AM

View Posteclipsed4utoo, on 11 Aug, 2009 - 07:00 AM, said:

do you mean your code is something similar to this?

<code here>

where the only difference is the string that is being assigned to the "FileName"?


Yes. And I cannot figure your tutorial out. I am trying to move a list view so another class can update the items. I need lstModRequirements in the class SelectRequirements so it can when the Done button on form 2 is clicked populate the list.

On form 1:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ModReadmeGenerator
{
	public partial class formMain : Form
	{
		formModRequirements Requirements = new formModRequirements();

//other objects here

		public formMain()
		{
			InitializeComponent();
		}

//other methods here

		private void btnModRequirments_Click(object sender, EventArgs e)
		{
			Requirements.ShowDialog();
		}
	}
}



Form 2:
using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ModReadmeGenerator
{
	public partial class formModRequirements : Form
	{
		SelectRequirements SetRequirements = new SelectRequirements();

		public formModRequirements()
		{
			InitializeComponent();
		}

		private void btnRequirementsDone_Click(object sender, EventArgs e)
		{
			SetRequirements.SelectOptions();
		}
	}
}


Class being called:
using System;
using System.Text;
using System.Windows.Forms;

namespace ModReadmeGenerator
{
	public class SelectRequirements
	{
		public void SelectOptions()
		{
			
		}
	}
}

This post has been edited by blank_program: 11 August 2009 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#18 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: Proper way to setup methods

Posted 11 August 2009 - 10:56 AM

for the launching of applications, here is what the code could be changed to(using my example from my previous post)...

private void BtnNotePadLaunch_Click(object sender, EventArgs e)
{
       LaunchApplication("notepad.exe");
}

private void BtnWordPadLaunch_Click(object sender, EventArgs e)
{
       LaunchApplication("WordPad.exe");
}

private void BtnChromeLaunch_Click(object sender, EventArgs e)
{
       LaunchApplication("chrome.exe");
}

private void LaunchApplication(string applicationName)
{
       ExternalAppLauncher.StartInfo.WorkingDirectory = AppLocations.AppLocation;
       ExternalAppLauncher.StartInfo.FileName = applicationName;
       ExternalAppLauncher.Start();
}



Then, you could move the "LaunchApplication" to a separate class if you wanted to.




And here is a very simple example of changing/updating a ListView from Form2 that is on Form1.

Form1 - I simply have a ListView and a Button on the form
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnOpenForm2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(listView1);
        f2.ShowDialog();
        f2.Dispose();
    }
}



Form2 - I have a Textbox and 2 Buttons on the form(One to add, one to close form).
public partial class Form2 : Form
{
    ListView listViewFromFirstForm;

    public Form2(ListView _listView)
    {
        InitializeComponent();

        listViewFromFirstForm = _listView;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        string textToBeAdded = txtText.Text;
        listViewFromFirstForm.Items.Add(textToBeAdded);
        txtText.Text = string.Empty;
    }

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}



I pass the ListView as a parameter to the constructor of Form2. In Form2's constructor, I assign the parameter to a class level variable so that I can work with the ListView in other methods/events on Form2.

This post has been edited by eclipsed4utoo: 11 August 2009 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#19 blank_program   User is offline

  • D.I.C Regular
  • member icon

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

Re: Proper way to setup methods

Posted 11 August 2009 - 12:06 PM

Thanks eclipse. I opted for a text box and used your tutorial as well as Google to set somethign setup for it. It works now.

When I run the applications I realized that due to many having different paths it would end upt he same amount of code as leaving it in the event as it would be making a new method so I left it as is. I can still change it if how I have it set up would be wrong.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2