|
||
|
|
#1 (permalink) | |
|
Forum Expert
|
Ok, first, I'm new to C#. I bought a book (C# Build a program NOW!), flew threw it. Basically, I want to code a simple program that would act similar to the XP Welcome tips, you know the dialog box that would pop up and offer handy (hehe) tips to using XP.
So far, I've got an internal setting called tipnumber. I have it incrimenting, and saving to the settings this number each time my button is clicked. I have a textbox which displays (right now for testing) "this is tip number" and the proper number. What I lack is a method to actually get a tip there. I originally thought I'd just do a ton of IF statements, you know one for each tip, and an else at the end to reset the number back to 0.. Then I decided to think out of the box and realized it would be pretty cool if I had another file within my app which was basically a txt/xml/other?? file which contained ALL my tips (making them easier to manage/add/alter in the future when used with other programs). Problem is, I have no idea HOW to go about this.. If anyone is bored and could point me in the right direction, I'd really appreciate it. All the stuff I've done I'm sure is elementary to you all, but I assure you a fair amount of sweat has gone into this, lol.. The book, while 'fun', doesn't really teach you a whole lot. Rooster
__________________
Quote:
|
|
|
|
|
|
|
#2 (permalink) |
|
RunUO Forum Moderator
|
I would go with the XML solution for this program.
You'll need to do the following things: 1. Make an XML file in the following format: Code:
<root><tip>this is tip 0</tip> <tip>this is tip 1</tip> <tip>this is tip 2</tip> ... <tip>this is tip N-1</tip></root> Code:
XmlDocument tipsXML = new XmlDocument();
tipsXML.load("tips.xml"); //change "tips.xml" to the file name and location of your xml tips file you created in step 1.
Code:
int numberOfTips = tipsXML.FirstChild.ChildNodes.Count; //go to "root" and get all the "tip" childs count Code:
private string getTipFromNumber( int number )
{
P.S. I haven't done any try/catch in the code so you should add them. You will need to have access to the tipsXML variable from all the class so it should be a property and initiated in the constructor.
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#4 (permalink) | |
|
Forum Expert
|
Tnx fellas! Never thought of using runuo stuff as a resource, lol! That's a pretty good idea.. I always refered to the code as merely scripts and mentally, even though I know better, well, you know... lol.. Tnx for the advice!
__________________
Quote:
|
|
|
|
|
|
|
#5 (permalink) | |
|
Forum Expert
|
Well, I'm chokin here, rofl.. Here's what I have thus far:
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using FortuneCookies.Properties;
using System.Configuration;
namespace FortuneCookies
{
public partial class Main : Form
{
//Loads the About screen
AboutFortuneCookies aboutScreen = new AboutFortuneCookies();
//This should load the tips(aka fortunes)
XmlDocument tipsXML = new XmlDocument();
//sets currentFortune to the settings number. This is no longer used and will be removed.
int currentFortune = FortuneCookies.Properties.Settings.Default.FortuneNumber;
public Main()
{
InitializeComponent();
tipsXML.Load("tipsXML.xml");
//go to "root" and get all the "tip" childs count
int numberOfTips = tipsXML.FirstChild.ChildNodes.Count;
//simple text instruction...
this.tbFortune.Text = "Select New Fortune to see your fortune";
}
//private string getTipFromNumber(int number)
private string getTipFromNumber (int number)
{
//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
return tipsXML.FirstChild.ChildNodes[number].InnerText;
}
private void tsbNew_Click(object sender, EventArgs e)
{
//My trial run to ensure the Settings were working, updating and saving. Enabled me to verify a
//new number was stored and used per each click of the button. Will be removed, REM'd for now:
//-- tbFortune.Text = "You have clicked NEW. Next number is: " + FortuneCookies.Properties.Settings.Default.FortuneNumber + ".";
// failed attempt tbFortune.Text = FortuneCookies.Properties.Settings.Default.FortuneNumber + tipsXML.FirstChild.ChildNodes[currentFortune].InnerText;
tbFortune.Text = tipsXML.FirstChild.ChildNodes[currentFortune].InnerText;
//This will take the fortune # and add 1 to it in the settings.
FortuneCookies.Properties.Settings.Default.FortuneNumber++;
//This will save the new fortune number to the settings.
FortuneCookies.Properties.Settings.Default.Save( );
}
private void button1_Click(object sender, EventArgs e)
{
//This area is identical to the tsbNew_Click section directly above this section. This will eventually be removed and was
//here only for testing reasons.
tbFortune.Text = "You have clicked NEW. Next number is: " + FortuneCookies.Properties.Settings.Default.FortuneNumber + ".";
FortuneCookies.Properties.Settings.Default.FortuneNumber++;
FortuneCookies.Properties.Settings.Default.Save( );
}
private void helpToolStripButton_Click(object sender, EventArgs e)
{
aboutScreen.ShowDialog();
}
}
}
Code:
tbFortune.Text = tipsXML.FirstChild.ChildNodes[currentFortune].InnerText; My setting (fortuneNumber) is set to one. The file name (tipsXML.xml) is correct (left it as that from the example above till I work this out). I'm also aware I'm missing some form of statement to say "if fortuneNumber is greater than numberOfTips than reset it to 1 again".. Really at this point that isn't my concern (since I cant get it to display tip #, hehe), but if ya wanna help with that too... No one would be mad at you ![]() Rooster
__________________
Quote:
|
|
|
|
|
|
|
#6 (permalink) |
|
RunUO Forum Moderator
|
Tell me what the output of this line before it crash:
Code:
Console.Writeline("cur: '"+currentFortune+"' count: '"+tipsXML.FirstChild.ChildNodes.Count+"'.");
P.S. Regardless of the crash you can use the method getTipFromNumber to get the tip like this: Code:
tbFortune.Text = getTipFromNumber(currentFortune); P.P.S. I've added some useful console.writelines to the method to catch exceptions, use this code instead of the method you have: Code:
private string getTipFromNumber (int number)
{
//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
if (tipsXML==null) //if we don't have a valid xml document open
Console.WriteLine("I don't have an xml file to read.");
else if (tipsXML.FirstChild==null) //if our xml document doesn't have a root element (aka first child)
Console.WriteLine("The XML document doesn't have a root element.");
else if (tipsXML.FirstChild.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
{
Console.WriteLine("I don't have a tip number: '"+number+"' in the XML file.");
Console.WriteLine("I can accept only tips from '0' to '"+tipsXML.FirstChild.Count-1+"'.");
}
else //everything seems to be ok, return the message
return tipsXML.FirstChild.ChildNodes[number].InnerText;
//if we reached this point than we have a problem.
//in real programming we'll throw an invalid arguments exception and let the program crash.
//throw new ArgumentException("Unable to access tip number '"+number+"'.");
//but if you don't want to crash for some weired reason (like RunUO scripting) you just return an error string back:
return "NULL REFERENCE EXCEPTION WAS PREVENTED while trying to access tip '"+number+"', please contact the administrator and report this message and the number '"+number+"'.";
//Only have ONE option enabled and the other marked out, either throw the exception (real programmer) or return the bad message ("programming for kids"), don't have both lines active (without remarks) at the same time or you'll get "unreachable code detected" warning!
}
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- Last edited by daat99; 10-13-2007 at 12:33 PM. |
|
|
|
|
|
#7 (permalink) | ||||
|
Forum Expert
|
Quote:
Quote:
Quote:
__________________
Quote:
|
||||
|
|
|
|
|
#8 (permalink) |
|
RunUO Forum Moderator
|
sorry about that
change: Code:
else if (tipsXML.FirstChild.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child. Code:
else if (tipsXML.FirstChild.ChildNodes.Count<=number || number<0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
|
#9 (permalink) | |
|
Forum Expert
|
It is giving me the Null Reference Exception. The number reported counts up per click, and originally started at 3 and I tried a few times, so at about 7 now. There are 19 current tips.. tipsXML.xml file looks as such:
Code:
<?xml version="1.0" encoding="utf-8" ?> <root> <tip>this is tip 0</tip> <tip>this is tip 1</tip> <tip>this is tip 2</tip> <tip>this is tip 3</tip> <tip>this is tip 4</tip> <tip>this is tip 5</tip> <tip>this is tip 6</tip> <tip>this is tip 7</tip> <tip>this is tip 8</tip> <tip>this is tip 9</tip> <tip>this is tip 10</tip> <tip>this is tip 11</tip> <tip>this is tip 12</tip> <tip>this is tip 13</tip> <tip>this is tip 14</tip> <tip>this is tip 15</tip> <tip>this is tip 16</tip> <tip>this is tip 17</tip> <tip>this is tip 18</tip> <tip>this is tip 19</tip> </root>
__________________
Quote:
|
|
|
|
|
|
|
#10 (permalink) | |
|
RunUO Forum Moderator
|
Quote:
So, we change the method like this: Code:
private string getTipFromNumber(int number)
{
//first go to the root, get the tip based on number between 0 and N-1, get the inner text (the tip itself).
if (tipsXML == null) //if we don't have a valid xml document open
Console.WriteLine("I don't have an xml file to read.");
else if (tipsXML.DocumentElement == null) //if our xml document doesn't have a root element (aka first ELEMENT child)
Console.WriteLine("The XML document doesn't have a root element.");
else if (tipsXML.DocumentElement.ChildNodes.Count <= number || number < 0) //make sure the number we received is between 0 and the childs count minus 1 for last child.
{
Console.WriteLine("I don't have a tip number: '" + number + "' in the XML file.");
Console.WriteLine("I can accept only tips from '0' to '" + (tipsXML.DocumentElement.ChildNodes.Count - 1) + "'.");
}
else //everything seems to be ok, return the message
return tipsXML.DocumentElement.ChildNodes[number].InnerText;
//if we reached this point than we have a problem.
//in real programming we'll throw an invalid arguments exception and let the program crash.
throw new ArgumentException("Unable to access tip number '"+number+"'.");
//but if you don't want to crash for some weired reason (like RunUO scripting) you just return an error string back:
//return "NULL REFERENCE EXCEPTION WAS PREVENTED while trying to access tip '" + number + "', please contact the administrator and report this message and the number '" + number + "'.";
//Only have ONE option enabled and the other marked out, either throw the exception (real programmer) or return the bad message ("programming for kids"), don't have both lines active (without remarks) at the same time or you'll get "unreachable code detected" warning!
}
__________________
I always try to help
![]() Sometimes, I don't know how.... ![]() My Web Page Forum Rules ------------------------------------------------------------- Extensive OWLTR System | Token System | World Teleporters ------------------------------------------------------------- |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|