|
Check out these two functions for serializing and deserializing data using a BinaryFormatter. It serializes a hashtable but can be replaced by an arraylist and should work just fine.
static void Serialize() <br />
{<br />
Hashtable addresses = new Hashtable();<br />
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");<br />
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");<br />
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");<br />
<br />
FileStream fs = new FileStream("DataFile.dat", FileMode.Create);<br />
<br />
BinaryFormatter formatter = new BinaryFormatter();<br />
try <br />
{<br />
formatter.Serialize(fs, addresses);<br />
}<br />
catch (SerializationException e) <br />
{<br />
Console.WriteLine("Failed to serialize. Reason: " + e.Message);<br />
throw;<br />
}<br />
finally <br />
{<br />
fs.Close();<br />
}<br />
}<br />
<br />
<br />
static void Deserialize() <br />
{<br />
Hashtable addresses = null;<br />
<br />
FileStream fs = new FileStream("DataFile.dat", FileMode.Open);<br />
try <br />
{<br />
BinaryFormatter formatter = new BinaryFormatter();<br />
<br />
addresses = (Hashtable) formatter.Deserialize(fs);<br />
}<br />
catch (SerializationException e) <br />
{<br />
Console.WriteLine("Failed to deserialize. Reason: " + e.Message);<br />
throw;<br />
}<br />
finally <br />
{<br />
fs.Close();<br />
}<br />
<br />
foreach (DictionaryEntry de in addresses) <br />
{<br />
Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);<br />
}<br />
}
Elvis (a.k.a Azerax)
Life is Music listen to it before it fades
|
|
|
|
|
Hello,
I would like to mask the text box such that it would only be for the money amounts, and it would have $ sign in front. So for input in textbox:
4500
Masked to: $ 4,500
Any ideas?
Thank you.
|
|
|
|
|
You could just format the number before you display it.
double x = 4500.00;
textBox2.Text= x.ToString("c");
output = $4,500.00
|
|
|
|
|
This the modification version of MaskedTextControl from
User Interfaces in C# book
using System;
using System.Windows.Forms;
public class MaskedTextBox : TextBox
{
private string mask;
public string Mask
{
get
{
return mask;
}
set
{
mask = value;
this.Text = "";
}
}
Protected override void OnKeyPress(KeyPressEventArgs e)
{
if (Mask != "")
{
e.Handled = true;
string newText = this.Text;
bool finished = false;
for (int i = this.SelectionStart; i < mask.Length; i++)
{
switch (mask[i].ToString())
{
case "#" :
if (Char.IsDigit(e.KeyChar) )
{
if(this.TextLength<mask.Length)
{
newText += e.KeyChar.ToString();
finished = true;
break;
}
else
{
newText=newText.Remove(i,this.SelectionLength);
newText=newText.Insert(i,e.KeyChar.ToString());
finished = true;
break;
}
}
else
{
return;
}
default :
newText += mask[i];
break;
}
if (finished)
{ break; }
}
this.Text = newText;
this.SelectionStart = this.Text.Length;
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
e.Handled = true;
}
}
In the Form Load event add the following
maskTextBox.Mask = "$###,###";
MCAD
|
|
|
|
|
There seems to be an error:
if(this.TextLength {
in this line, the IF statement is not completed, or am I reading it incorectly? Is it:
if (this.TextLength != 0)
Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?
Thank you
|
|
|
|
|
sorry some thing wrong was happend
replace if(this.TextLength {
<br />
with<pre><code><br />
if(this.TextLength<mask.Length)
This prevent the user form enter number longer than mask lenght
zaboboa wrote:
Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?
Yes you cab write mask like $###,###,### or even
$(###)(###),(###)
Note:this quick code so test it before you use it
and you may add another thibgs like backspace...
MCAD
|
|
|
|
|
You know, it will work better if you put this statement insted
if(this.TextLength == this.SelectionStart)
Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6
Thank you for your help, it gave me a big jumpstart.
|
|
|
|
|
zaboboa wrote:
Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6
And Copy and Paste there are many things you can add it
Go on
MCAD
|
|
|
|
|
has anyone come across an open source server in C# or any other language..
I need one....I'll convert it into C# if its not in C#
|
|
|
|
|
|
i have a XML file and i wish to remove a few nodes from it...I am using the XMLDocument class...How can i do this without loading it into a dataset or something..
|
|
|
|
|
What exaclty are you trying to do. Permanently remove nodes from the document? Copy nodes to nodes outside the document?
|
|
|
|
|
like..if i document that was like..
<phonebook>
<contact>
<firstname>A
<lastname>A
<phonenum>A
<titel>A
<nickname>A
<country>
<city>A
A
<zipcode>A
<job>A
<mail>A
<site>A
now suppose i want to remove the field called zipcode..
how do i do that?
|
|
|
|
|
<?xml version="1.0"?><br />
<!--Mohamed's Address Book - WRITING XML IN VB.NET--><br />
<!--AUTHOR - Mohamed Magdy--><br />
<phonebook><br />
<contact><br />
<firstname>A</firstname><br />
<lastname>A</lastname><br />
<phonenum>A</phonenum><br />
<titel>A</titel><br />
<nickname>A</nickname><br />
<country /><br />
<city>A</city><br />
<address>A</address><br />
<zipcode>A</zipcode><br />
<job>A</job><br />
<mail>A</mail><br />
<site>A</site><br />
</contact><br />
</phonebook>
|
|
|
|
|
i dunno why im unable to put xml text here
|
|
|
|
|
|
<?xml version="1.0"?>
<!--Mohamed's Address Book - WRITING XML IN VB.NET-->
<!--AUTHOR - Mohamed Magdy-->
<phonebook>
<contact>
<firstname>A</firstname>
<lastname>A</lastname>
<phonenum>A</phonenum>
<titel>A</titel>
<nickname>A</nickname>
<country />
<city>A</city>
<address>A</address>
<zipcode>A</zipcode>
<job>A</job>
<mail>A</mail>
<site>A</site>
</contact>
</phonebook>
I want to remove the node called zipcode
|
|
|
|
|
Try this
System.Xml.XmlNode node;
System.Xml.XmlDocument doc = new XmlDocument();
doc.LoadXml("<person><name>jon</name><age>35</age><zip>47408</zip></person>");
this.textBox1.Text=doc.InnerXml;
node = doc.SelectSingleNode("person");
node.RemoveChild( doc.SelectSingleNode("person//zip"));
textBox1.Text += "\n" + doc.InnerXml;
|
|
|
|
|
Hello, I'm trying to grab the entire bitmap for a Windows Form when performing a drag/drop operation, using the code below.
This works fine, except for when part of the form is off-screen or hidden by, say, another window -- in this case the bitmap returned includes part of another window / the Windows Taskbar or whatever is there.
Does anyone know of a way to get the entire bitmap for a Window (i.e. the image that would be shown by that window if it was completely on-screen, topmost and visible). Is there perhaps a native API that can help with this?
Any ideas would be much appreciated, as I'm beginning to think this can't be done.
Andy
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hdcDst, int xDst, int yDst, int cx, int cy, IntPtr hdcSrc, int xSrc, int ySrc, uint ulRop);
public Image GetBGImage(System.Windows.Forms.Form formToCopy)
{
Graphics g = Graphics.FromHwnd(formToCopy.Handle);
Bitmap bm = new Bitmap(formToCopy.ClientRectangle.Width, formToCopy.ClientRectangle.Height, g);
Graphics g2 = Graphics.FromImage(bm);
IntPtr hdcScreen = g.GetHdc();
IntPtr hdcBitmap = g2.GetHdc();
BitBlt(hdcBitmap, 0, 0, bm.Width, bm.Height, hdcScreen, 0, 0, 0x00CC0020);
g2.ReleaseHdc(hdcBitmap);
g.ReleaseHdc(hdcScreen);
return bm;
}
|
|
|
|
|
Hello,
Is there a way of formating the number string is such a way, if an input string is:
450000 for example.
How can I make the output string to be something like that:
450,000.
Sort of putting commas for each of the hundreds
Thank you
|
|
|
|
|
I just tested this... you can do the following....
<br />
string buff;<br />
int test = 450000;<br />
buff = test.ToString("###,###");<br />
test = 450000000;<br />
buff = test.ToString("###,###");<br />
And the commas are put in the right places for both tests.
Steve Maier, MCSD MCAD
|
|
|
|
|
Do you have a string or a number? If you have a number, it's easy. Numeric format with zero decimals:
string result = myNumber.ToString("n0");
You can also create a NumberFormatInfo object, where you can specify any format you like:
NumberFormatInfo info = new NumberFormatInfo();<br />
info.NumberGroupSizes = new int[] {1, 2, 1, 0};<br />
info.NumberGroupSeparator = "-beep-";<br />
string result = myNumber.ToString("n0", info);
Edit:
If you have any problems with the first example because of locale settings, use an invariant format info:
string result = myNumber.ToString("n0", NumberFormatInfo.InvariantInfo);
---
b { font-weight: normal; }
|
|
|
|
|
Hello Everybody
I want to print a multiple pages document. But when I draw the content, I don't know how to get a new page, so the content beyond one page will be lost. Does anybody know how to solve the problem please???
Thanks very much
Asura
|
|
|
|
|
To get additional page to be printed
Set PrintPageEventArgs.HasMorePages to true
private void printDocument1_PrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
if(your condition)
e.HasMorePages = false;
else
{
e.HasMorePages = true;
}
}
look at
PrintPageEventArgs.HasMorePages [^]
MCAD
|
|
|
|
|
My dad is trying to teach me C# . NET and i stumbled onto a problem while trying to create my own program. I would like to know how to take the numbers of one text box, multiply them by 2 and project them onto another textbox. Keep in mind though that im a begginer and dont understand hard code. Thanks for your help
|
|
|
|