
Cags
Avatar/Signature-
Posts
699 -
Joined
-
Last visited
Content Type
Profiles
Forums
Blogs
Events
Articles
Resources
Downloads
Gallery
Everything posted by Cags
-
vbMarkO this will suit your purposes.... // get the original document System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(@"C:\input.xml"); // create a new node System.Xml.XmlNode memberNode = xmlDoc.CreateElement("Members"); // create the child nodes System.Xml.XmlNode nameNode = xmlDoc.CreateElement("Name"); nameNode.InnerText = "Peter"; System.Xml.XmlNode phoneNode = xmlDoc.CreateElement("Phone"); phoneNode.InnerText = "01664 123456"; System.Xml.XmlNode addressNode = xmlDoc.CreateElement("Address"); addressNode.InnerText = "This is my address"; // add the child nodes to the new node memberNode.AppendChild(nameNode); memberNode.AppendChild(phoneNode); memberNode.AppendChild(addressNode); // add the new node to the file and save xmlDoc.FirstChild.AppendChild(memberNode); xmlDoc.Save(@"C:\output.xml"); ' get the original document Dim xmlDoc As New System.Xml.XmlDocument xmlDoc.Load("C:\input.xml") ' create a new node Dim memberNode As System.Xml.XmlNode = xmlDoc.CreateElement("Members") ' create the child nodes Dim nameNode As System.Xml.XmlNode = xmlDoc.CreateElement("Name") nameNode.InnerText = "Peter" Dim phoneNode As System.Xml.XmlNode = xmlDoc.CreateElement("Phone") phoneNode.InnerText = "01664 123456" Dim addressNode As System.Xml.XmlNode = xmlDoc.CreateElement("Address") addressNode.InnerText = "This is my address" ' add the child nodes to the new node memberNode.AppendChild(nameNode) memberNode.AppendChild(phoneNode) memberNode.AppendChild(addressNode) ' add the new node to the file and save xmlDoc.FirstChild.AppendChild(memberNode) xmlDoc.Save("C:\output.xml") Hope the vb is right i'm a little rusty
-
The user can both pick from a menu, and load from a file. Static methods is a good idea, I must admit I never thought of that. Great now three methods to choose from, my biggest problem with programming is I tend to spend days refactoring and never getting any further along. EDIT: For now I'll probably stick with the inheritance as it provides the ability for extensibility through the use of plugins.
-
I'm looking for guidance here on best practice. I recently programmed a Sudoku application which can help solve various forms of 'Doku' puzzles. These include normal Sudoku (9x9), GoDoku (9x9 with letters), SuperSudoku (12x12) plus a few more different types. Now each of these puzzles share exactly the same properties which include grid dimensions, mini grid dimensions, character set etc. I created a 'Doku' class that contained each of these properties, but have been torn how to proceed after that. One option is to have an overload for the constuctor that excepts all the relavent data so for example... Doku myDoku = new Doku(new Size(9,9), new Size(3,3), new Char { ... });Another option I considered (and is the way I'm currently doing it) is to inherit from the doku class and simply have the constructor of the inherited class set all the default values for me. This in my opinion makes the code more readable as it allows me to simply use... Doku myDoku = new Sudoku();Basically all I'm asking for is if there is any reason this would be bad practice, it certainly works.
-
You say the label contains '3' which you obviously don't think is right, but you don't say what you think it should be. The value 3 is correct for the code you have posted, which is why there no compile or runtime errors. I've not checked to see if the code your using to add a new node in the WriteXML adds the node correctly, but you don't call doc.Save() at any point. Since you don't save the document, when LoadXML is being called it's seeing the original xml file. Just as a pointer I'm not entirely sure why you have while loops in either method since you have a break statement without a condition this loop is only ever run once.
-
I'm not going to even pretent to understand exactly what your trying to achieve. As such this might be of no use to you at all, if thats the case, just ignore my babblings. Creating an instance of a class from a string can be achieved through the use of Activator.CreateInstace().
-
Perhaps something like this would suit your purposes.... Timer myTimer = new Timer(); myTimer.Tick += new EventHandler(myTimer_Tick); private void myTimer_Tick(object sender, System.EventArgs e) { timer1.Enabled = false; // your loop code timer1.Interval = 100; // whatever time you wanna wait timer1.Enabled = true; }
-
Doing as teixeira suggests will allow a dialog, but being on the Pocket PC it will be full screen. This might suit your purposes but I suspect you wanted a floating dialog like the actual MessageBox. As for how todo this or if it's infact possible I'm not entirely sure.
-
OnClick isn't a method it's an event. What PlausiblyDamp is suggesting is that instead of calling the method attached to the OnClick event (txtReset_Click(sender, e);) you instead call txtReset.PerformClick().
-
It would help if you posted the code from the txtReset_Click method. One thing you may be doing wrong, if you are using the sender object at all in the txtReset_Click event it would work when you click on the txtReset but not when you clicked the btnWissen since you would have passed down the button not the textbox.
-
To the best of my knowledge there is no way of sorting an xml file. If you wish to sort the data contained in the xml file, the only way I know of achieving it is to load the data into some kind of array sort it in your application. Then write out the array as a new xml file.
-
I don't think your quite understanding how easy it is todo. The fact that your comboBox is empty to start with is fine. It's really not complicated, in my opinion its simpler than the method your using. Imagine an example using coloured sweets such as smarties. You want a set of sweets containing one of each colour... a.) you put all the sweets on your pile one at a time, then remove any duplicates one at a time. Thus you handle each sweets many times. b.) you take each sweet out of it's packet, see if you have one of that colour in the pile, if you do then just ignore the sweet (drop it or eat it, whatever) if you don't you add that sweet to the pile. You are currently doing b. to do b. all you need to do is anywhere in the loop of your FormLoad where you call comboBox1.Items.Add(myCity), you replace it with If comboBox1.Items.Contains(myCity) = False Then comboBox1.Items.Add(myCity) End If NB. I normally use C# the syntax should be at least close enough for you to understand though.
-
I personally would have thought it easier and perhaps more efficient to test for a city before adding it to the list in the first place. What I mean by this is that when you are parsing through the xml loading in the names you check ComboBox.Items.Contains() before adding it.
-
Two first time posters? Am I becoming synical in my old age?
-
In an attempt to create a true ASCII string in C# I decided to look up the decimal values of each char in order to create a byte array representing the string. It was in doing this I realised your problem. ASCII is a 7bit encoding meaning that any decimal value above 127 is invalid, in the 'ASCII' you provide several of the chars aren't within this range. They are considered part of the Extended ASCII set, which is to say they have values above 127 and thus aren't really ASCII. Since this is the case it is my belief you will have very little success in using the Encoding.ASCII class to convert that string to what you consider Unicode.
-
Having thought about it, i'm suspecting that the reason the method doesn't work could be down to the way the .Net Framework views a string. If the .Net Framework uses unicode encoding by default on its string class then storing the ascii you are trying as a string object would simply make it no longer ascii. Basically what I'm getting at is its not the conversion method itself thats wrong, but the way you are inputting the ascii string. It's hard to explain exactly what I mean, but I think theres something to my thoughts. Either that or my brains just ran off at a tangent as per normal.
-
Try this... It should be essentially the same as MrPauls example, but I provided both conversion methods. using System; using System.Text; namespace Convert_Example { public class MyConvertExampleClass { public static string ConvertAsciiToUnicode(string asciiString) { // Create two different encodings. Encoding encAscii = Encoding.ASCII; Encoding encUnicode = Encoding.Unicode; // Convert the string into a byte[]. byte[] asciiBytes = encAscii.GetBytes(asciiString); // Perform the conversion from one encoding to the other. byte[] unicodeBytes = Encoding.Convert(encAscii, encUnicode, asciiBytes); // Convert the new byte[] into a char[] and then into a string. // This is a slightly different approach to converting to illustrate // the use of GetCharCount/GetChars. char[] unicodeChars = new char[encUnicode.GetCharCount(unicodeBytes, 0, unicodeBytes.Length)]; encUnicode.GetChars(unicodeBytes, 0, unicodeBytes.Length, unicodeChars, 0); string unicodeString = new string(unicodeChars); // Return the new unicode string return unicodeString; } public static string ConvertUnicodeToAscii(string unicodeString) { // Create two different encodings. Encoding encAscii = Encoding.ASCII; Encoding encUnicode = Encoding.Unicode; // Convert the string into a byte[]. byte[] unicodeBytes = encUnicode.GetBytes(unicodeString); // Perform the conversion from one encoding to the other. byte[] asciiBytes = Encoding.Convert(encUnicode, encAscii, unicodeBytes); // Convert the new byte[] into a char[] and then into a string. // This is a slightly different approach to converting to illustrate // the use of GetCharCount/GetChars. char[] asciiChars = new char[encAscii.GetCharCount(asciiBytes, 0, asciiBytes.Length)]; encAscii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0); string asciiString = new string(asciiChars); // Return the new ascii string return asciiString; } } }
-
As far as I can see you have two real choices here. Once a user selects a person, you re-open the xml file, search for that person and put the contents of the description file into the textbox. Alternatively (and possibly a better solution) is to load the descriptions at the same time as the names and store them in a class. How exactly you do this will depend on how you are currently reading in the xml file.
-
First out I should mention that my project is for the Pocket PC and using .Net 1.1, thus there are certain restraints due to the Compact Framework. Essentially what I'm wondering is the best way to handle multiple 'screens' what I mean by this is 'Main Menu', 'Game', 'About', 'Options' etc. Logically speaking I suppose you can think of each 'screen' as a seperate form, but I need to only use a single form. The way I was thinking about achieving this is to have a 'Screen' class or Interface that would implement methods for Painting and for handling input events such as Key events and Mouse events. Then each screen would Inherit this class/interface. The main form would then have a ChangeScreen method that would unhook the old classes methods from the events and hook up the new ones. Alternatively if there is some speed issue with event hooking/unhooking a switch statement could be used to call the correct method based on which screen is showing. This method would be less dynamic, but as the amount of different screens in the game would be static this wouldn't really matter. Here's a quick example... public interface IScreen { void PaintEvent(object sender, PaintEventArgs e); void ClickEvent(object sender, EventArgs e); void KeyPressEvent(object sender, KeyPressEventArgs e); } public class MenuScreen : IScreen { public void PaintEvent(object sender, PaintEventArgs e) { // do stuff } public void ClickEvent(object sender, EventArgs e) { // do stuff } public void KeyPressEvent(object sender, KeyPressEventArgs e) { // do stuff } } // on the form public void ChangePage(IScreen screen) { // unhook events this.Paint = null; this.Click = null; this.KeyPress = null; // hook the new methods to the event this.Paint += new PaintEventHandler(screen.PaintEvent); this.Click += new EventHandler(screen.ClickEvent); this.KeyPress += new KeyPressEventHandler(screen.KeyPressEvent); } It's worth noting at this point that this is all theoretically I haven't tried it. I was just wondering if anyone else had either an opinion on this approach or an alternative approach for achieving the same result.
-
This adds to a combo but how can it add to an array
Cags replied to vbMarkO's topic in Database / XML / Reporting
Sub DisplayRecords() Dim xmlTr As New XmlTextReader(xmlFile) Dim myArrayList as New ArrayList() While xmlTr.Read If xmlTr.Name = "Last_Name" AndAlso xmlTr.NodeType = XmlNodeType.Element Then myArrayList.Add(xmlTr.ReadString) End If End While xmlTr.Close() You can add to an arraylist as follows, if you required a typed array you could create it afterwards (when you know how many elements their are) and copy the items over from the arraylist. -
I'd personally say write the book. If you can manage to get the book published then all well and good, wealth and fame galore ;). If you can't, then you could easily convert it to a freely distributable e-book or wiki.
-
Well I've managed to solved the problem myself after much hair pulling. // changing this line of code memoryBitmap = CreateCompatibleBitmap(memoryDC, this.Width, this.Height); // to this, solved the problem memoryBitmap = CreateCompatibleBitmap(formDC, this.Width, this.Height);I'm not going to pretend to fully understand why this works, but at least it did. I personally would have thought that since the memoryDC was being created to be compatible with formDC, that creating something compatible with memoryDC would have also been compatible with formDC, but apparently not. Now to try and understand the tidying up code :S
-
As far as I'm aware the DrawImage overloads that accept a point array can only be used to draw a parrallelagram (i.e. both vertical and both horizontal lines have to be parrallel with each other). If you are attempting to draw a parrallelagram then 3 points will be entirely accurate since the sides in both pairs will be exactly equal in length a 4th point isn't required. If you wish to draw a shape which isn't a parrallelagram, then I believe you are out of luck as far as the DrawImage method goes.
-
I wasn't sure whether to place this in the Graphics section or the Interop section, but eventually decided on here. My problem is as follows, the following code works as I expect it to.... this.Capture = true; formHandle = GetCapture(); this.Capture = false; formDC = GetDC(formHandle); spriteDC = CreateCompatibleDC(formDC); spriteBitmap = SHLoadDIBitmap(@"\My Documents\Cags\Bitmap.bmp"); SelectObject(spriteDC, spriteBitmap); // draw sprite to form BitBlt(formDC, 0, 0, 25, 25, spriteDC, 0, 0, SRCCOPY); It copies a small section of the Bitmap.bmp to the screen. After getting this working I decided to try and buffer it before blitting it to the screen. So I amended the code as follows. this.Capture = true; formHandle = GetCapture(); this.Capture = false; formDC = GetDC(formHandle); memoryDC = CreateCompatibleDC(formDC); memoryBitmap = CreateCompatibleBitmap(memoryDC, this.Width, this.Height); SelectObject(memoryDC, memoryBitmap); spriteDC = CreateCompatibleDC(formDC); spriteBitmap = SHLoadDIBitmap(@"\My Documents\Cags\Bitmap.bmp"); SelectObject(spriteDC, spriteBitmap); // draw sprite to buffer BitBlt( memoryDC, 25, 25, 25, 25, spriteDC, 26, 26, SRCCOPY ); // draw buffer to form BitBlt( formDC, 25, 25, 25, 25, memoryDC, 25, 25, SRCCOPY); The problem is this section of code simply draws a black square rather than a small section of the Bitmap.bmp as I expected. Has anyone got any idea what I'm doing wrong here? Before I forget the API methods are declared as follows, I'm pretty sure they are all right so I don't think thats the problem. [DllImport("coredll.dll")] public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop); [DllImport("coredll.dll")] public static extern IntPtr SHLoadDIBitmap(string szFileName); [DllImport("coredll.dll")] static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight); [DllImport("coredll.dll")] public static extern IntPtr CreateCompatibleDC(IntPtr hdc); [DllImport("coredll.dll")] public static extern IntPtr GetDC(IntPtr hWnd); [DllImport("coredll.dll")] public static extern IntPtr GetCapture(); [DllImport("coredll.dll")] static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj); const int SRCCOPY = 0x00CC0020;
-
The way I generally do it is as you say with the XmlTextReader and the XmlTextWriter classes. Another way of doing it is to create a Settings class and to serialize it using xml serialization.
-
Setting the Background of an Graphics [C#]
Cags replied to Shaitan00's topic in Graphics and Multimedia
As far as I'm aware there is no simple way to change the background in the manner you describe, because quite simply there is no such thing as a background. To the best of my knowledge the bitmap object is single layer meaning that it has no way of distinguishing a difference between what you think is the background and the image. One way of doing this would be to research some of the various flood fill options, but that could potentially become complicated. The simplest approach would be to set the background colour before drawing the imagine by calling gLevel.Clear(backCol);