Quantcast

Jump to content

» «
Photo

Coding for my Bus Driver Control Panel

3 replies to this topic
SecondEmeraldMaster
  • SecondEmeraldMaster

    Transit Specialist

  • Members
  • Joined: 30 Jan 2012

#1

Posted 28 April 2013 - 03:59 AM

I've begun to work on a new mod that is supposed to work along side another mod I've been working on lately. Seeing as how I'm new to using C#, this is gonna take some time before I finish it. When I plan on releasing a beta, I'm not sure yet.

Right now, I've been trying to do a few things...
  • Write entered values within text boxes to an *.ini file
  • Save file that is currently open in the application (not save as)
  • Undo/Redo functions
...And now I need some help with that. What am I supposed to do now? Any help would be greatly appreciated.

Here's the code below of what I have so far:

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bus_Driver_Control_Panel
{
   public partial class MainWindow : Form
   {
       public MainWindow()
       {
           InitializeComponent();
       }

       private void NewFile_Click(object sender, EventArgs e)
       {
           EnterRouteNumber.Clear();
           EnterRouteName.Clear();
           EnterRouteDestination.Clear();
           EnterVIA1stDestination.Clear();
           EnterVIA2ndDestination.Clear();
           EnterVIA3rdDestination.Clear();
           EnterVIA4thDestination.Clear();
           EnterVIA5thDestination.Clear();
           ServesLocations.Clear();
       }

       private void OpenFile_Click(object sender, EventArgs e)
       {
           OpenFileDialog ofd = new OpenFileDialog();
           ofd.Filter = "Configuration File (*.ini)|*.ini|All Files (*.*)|*.*";
           ofd.Title = "Open File";

           if (ofd.ShowDialog() == DialogResult.OK)
           {
               System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName);
               MessageBox.Show(sr.ReadToEnd());
               sr.Close();
           }
       }

       private void SaveFile_Click(object sender, EventArgs e)
       {
           //Right here is where the file that is open is supposed to be saved, but I'm not sure of what the code is.
       }

       private void SaveFileAs_Click(object sender, EventArgs e)
       {
           SaveFileDialog sfd = new SaveFileDialog();
           sfd.Filter = "Configuration File (*.ini)|*.ini|All Files (*.*)|*.*";
           sfd.Title = "Save File As";
           sfd.ShowDialog();

           if (sfd.FileName != "")
           {
               System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();
           }
       }

       private void CloseFile_Click(object sender, EventArgs e)
       {
           Application.Restart();
       }

       private void ExitApplication_Click(object sender, EventArgs e)
       {
           Application.Exit();
       }
   }
}

pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#2

Posted 28 April 2013 - 12:21 PM

Is there a good reason for you to avoid Sctiphook's SettingsFile Class ? It would have made your life much easier.
Take a look on that, here's the documentation for it -> http://www.hazardx.c...ils.php?file=89

But if you happen to have a good reason not use that class, this is pretty huge request to put here.

That said here are some guidelines :

Write entered values within text boxes to an *.ini file
You can have a read-only template file, with some keywords that you change in order to create the new ini file. String.Replace method should do the job.

Save file that is currently open in the application (not save as)
Technically no file is opened in the application you posted, but what does this differ from the first point ? Isn't the same thing ? Or is it the first a 'Save As' and this one a 'Save' function (if yes, the only thing that changes is the filename)?

Undo/Redo functions
Undo / Redo is a project on itself, mainly because you need to implement a 'Do' as well. You need to commit all changes the user makes to somewhere, and then you can go back and forth between your records. But I do suggest you to take some time to read about this type of feature, instead of waiting for someone to do it for you. Here is an excelent article on that subject -> http://www.codeproje...-Generics-NET-C

Hope this can help you.

freshgamer10
  • freshgamer10

    Player Hater

  • Members
  • Joined: 27 Apr 2013

#3

Posted 28 April 2013 - 01:26 PM Edited by freshgamer10, 28 April 2013 - 01:33 PM.

That's how it should work, but I didn't paste the code into VS, so if it doesn't work, it's most likely because I mixed up the order of the arguments in the File.WriteAllText-Statement.
CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bus_Driver_Control_Panel
{
  public partial class MainWindow : Form
  {
      string text; //variable which will hold the text of the .ini
      string filePath; //will hold the file path of the .ini
      public MainWindow()
      {
          InitializeComponent();
      }

      private void NewFile_Click(object sender, EventArgs e)
      {
          EnterRouteNumber.Clear();
          EnterRouteName.Clear();
          EnterRouteDestination.Clear();
          EnterVIA1stDestination.Clear();
          EnterVIA2ndDestination.Clear();
          EnterVIA3rdDestination.Clear();
          EnterVIA4thDestination.Clear();
          EnterVIA5thDestination.Clear();
          ServesLocations.Clear();
      }

      private void OpenFile_Click(object sender, EventArgs e)
      {
          OpenFileDialog ofd = new OpenFileDialog();
          ofd.Filter = "Configuration File (*.ini)|*.ini|All Files (*.*)|*.*";
          ofd.Title = "Open File";

          if (ofd.ShowDialog() == DialogResult.OK)
          {
              filePath = ofd.FileName; //setting the filepath to the file the user selected
              System.IO.StreamReader sr = new System.IO.StreamReader(filePath); //using variables is always good
              text = sr.ReadToEnd(); //setting our text
              MessageBox.Show(text); //again, variables make it less complicated
              sr.Close();
          }
      }

      private void SaveFile_Click(object sender, EventArgs e)
      {
          File.WriteAllText(text, filePath, Encoding.UTF8); //simple method, it will also replace any file which has been there before
      }

      private void SaveFileAs_Click(object sender, EventArgs e) //you should re-code this void
      {
          SaveFileDialog sfd = new SaveFileDialog();
          sfd.Filter = "Configuration File (*.ini)|*.ini|All Files (*.*)|*.*";
          sfd.Title = "Save File As";
          sfd.ShowDialog();

          if (sfd.FileName != "")
          {
              System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();
          }
      }

      private void CloseFile_Click(object sender, EventArgs e)
      {
          Application.Restart();
      }

      private void ExitApplication_Click(object sender, EventArgs e)
      {
          Application.Exit();
      }
  }
}

pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#4

Posted 28 April 2013 - 01:32 PM

everyone is watching ...




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users