Hello! I'm creating a simple markup type thing with a XML reader to edit the properties of this window and at some point add components to it. I had it working, but had a few issues since it wasn't originally a WinForms project, didn't have looping working right, so I did it this way and now it just crashes... Hopefully it's a simple fix but here is Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace SIRIP
{
static class Program
{
public static string filename;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
string[] cmdLine = Environment.GetCommandLineArgs();
int count = 0;
foreach (string s in cmdLine)
{
if (count == 1)
{
filename = s;
}
count++;
}
if (cmdLine.Length < 1)
{
Application.Exit();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new parser(filename));
}
}
}
Everything in the main method is just fetching the file path the program was opened with. And is passed in with this Application.Run(new parser(filename));
.
Now here is parser.cs (the largest one)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Xml;
using System.Drawing;
namespace SIRIP
{
public partial class parser : Form
{
public static string filename;
public parser(string path)
{
filename = path;
if (filename != null)
{
parse(filename);
}
else
{
Environment.Exit(0);
}
}
public static Form window;
public static int valid = 0;
public static Console console = new Console();
public void parse(string fileName)
{
XmlReader xml = XmlReader.Create(fileName);
while (xml.Read())
{
if (xml.NodeType == XmlNodeType.Element && xml.Name == "sirip")
{
valid = 1;
string debug = xml.GetAttribute("debug");
if (debug == "true")
{
console.Show();
console.print("Running...");
}
}
if (valid == 1)
{
if (xml.NodeType == XmlNodeType.Element && xml.Name == "window")
{
//Style window
this.Show();
console.print("Created new window");
//Fetch and set title
string title = xml.GetAttribute("title");
this.Text = title;
console.print("New window title \"" + title + "\"");
//Get and set window sizes.
string stringWidth = xml.GetAttribute("width");
string stringHeight = xml.GetAttribute("height");
int width;
int height;
if (int.TryParse(stringWidth, out width) && int.TryParse(stringHeight, out height))
{
this.Size = new Size(width, height);
}
console.print("Set window size");
string state = xml.GetAttribute("state");
if (state == "max")
{
this.WindowState = FormWindowState.Maximized;
}
else if (state == "min")
{
this.WindowState = FormWindowState.Minimized;
}
else if (state == "norm")
{
this.WindowState = FormWindowState.Normal;
}
else
{
this.WindowState = FormWindowState.Normal;
}
string icon = xml.GetAttribute("icon");
if (icon != null)
{
try
{
this.Icon = new Icon(icon);
console.print("Icon shown, " + icon);
}
catch
{
console.print("Unable to find icon.");
this.ShowIcon = false;
}
}
else
{
this.ShowIcon = false;
console.print("Icon is not shown.");
}
}
}
else
{
MessageBox.Show("Missing opening and closing <sirip></sirip> tags.");
}
}
window.FormClosed += window_FormClosed;
}
public static void window_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
Environment.Exit(0);
}
private void parser_Load(object sender, EventArgs e)
{
}
}
}
That just scrubs through the opened XML file and changes the properties of the form dependant on the attributes of the <window> Xml tag.
I also have another form which is the console you can see used with console.print("example")
which is here:
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 SIRIP
{
public partial class Console : Form
{
public Console()
{
InitializeComponent();
}
private void Console_Load(object sender, EventArgs e)
{
}
public void print(string print)
{
richTextBox1.AppendText("\n" + print);
}
private void Console_FormClosed(object sender, FormClosedEventArgs e)
{
Environment.Exit(0);
}
}
}
Yes this code may be sloppy, I'm new to C# let alone desktop development.
So it would help alot if anyone could tell my why this is crashing on startup!
Also here is the link to the file so you can see whats going on more:
https://dl.dropbox.com/u/90422453/SIRIP.exe
An example file that would be opened with it:
https://dl.dropbox.com/u/90422453/test.sp
Again thanks for any help!
EDIT:
I feel so stupid, for some reason this happens to me alot, I work on it for like 45 Minutes, then post the thread, look at it some more then 5 minutes later I fix it -.- I feel so embarrassed right now, but Just so you know it's in parser.cs and it is:
window.FormClosed += window_FormClosed;
should be this.FormClosed
sorry about that, that always seems to happen to me, please disregard.