|
I need to allow my application to only be run once. I presume I need to set some sort of system wide flag to do this ?
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
|
Okay, my project is almost finished and this seems to be the last bug. My error is on line 613 during the last 'while' code. It seems that garbage collection has 'dumped' my initial 'hash' instance??? I then get the error message "Object reference not set to an instance of ans object."!
Q. How do I fix the code to eliminate the error???
try
{
while((line = reader.ReadLine()) != null)
{
string [] tmp = line.Split('\t');
if(tmp.Length == 2)
{
if(!hash.Contains(tmp[0]))
{
hash.Add(tmp[0], tmp[1]);
}
listBox1.Items.Add(tmp[0] + "\t" + tmp[1]);
}
}
}
finally
{
reader.Close();
}
while(myReader2.Read())
{
if (! hash.ContainsKey(str_col_DestIP.Trim()))
{
string strDestDns = "Use Other...";
}
else
{
string str = (hash[1].ToString()); // line 613
}
|
|
|
|
|
It would seem that your Hashtable (I'm assuming that's what 'hash' is) does not have an object stored at the index you're trying to retrieve. Did you mean to say hash[0].ToString() ? Or possibly whatever you're reading in the first while loop contains only one line.
These are only guesses, as not enough code is posted for a definitive answer. The garbage collector won't collect something you still hold a reference to. It's not the hash that's null, though, so that wouldn't be it anyway.
Charlie
if(!curlies){ return; }
|
|
|
|
|
There wouldn't be an item at any index: a Hashtable is not a collection or a list (but can expose them through properties and methods). Coding hash[1] would only work if an object was added to the Hashtable using the integer (Int32 , unless otherwise cast) value 1 for the key.
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
D'oh!
Charlie
if(!curlies){ return; }
|
|
|
|
|
No, your hash variable is fine (you should really use a debugger and examine the state of variables in your Autos or Locals window). It's because hash[1] is returning null , which you're trying to call ToString on. Unrolling it, it would look something like this:
object o = hash[1];
o.ToString(); If you added "1" to your Hashtable , then you need to pass "1" - not 1 - to your indexer. They are very different (see the documentation for GetHashCode on any class).
If you're trying to index the second element (remember that in .NET indexes are 0-based) of the Hashtable , you're out of luck. Hash tables don't sort their collections - they use hash keys to store and reference data. You should do a search on google or something for basic theory on hash tables to find out more.
There's never a good reason for NullReferenceException to be thrown, either. When there's a chance that a variable may be null, check it. Even if you could access items like a collection, assuming that's there 2 - or even 1 - element is a bogus assumption. What if the file was empty and you didn't collect any data? There would be nothing in your Hashtable . It would simply be empty.
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
While adding the object in the HashTable hash you have used
hash(tmp[0], tmp[1])
where tmp[0] is a string and also the key value for string object tmp[1]
where as while accessing the hash value you are using hash[1], here key u r using as int (value 1). This is the cause of the error.
Replace the line 613 as below.
if(hash.ContainsKey("1"))
{
string str = hash["1"].ToString();
}
Regards,
Jay
|
|
|
|
|
Well, to draw on a panel, i use:
Graphics pg = panel.CreateGraphics();
pg.FillEllipse( new SolidBrush( Color.Black ), e.X, e.Y,2,2);
Its draw, but, when i minimize the form, the panel is clear!
What can i do to not clear the draw?
Thanks.
|
|
|
|
|
Anytime a window is moved off screen, minimized, or is obstructed from view (by another window) it needs to be repainted then next time it is shown.
In order for your graphics to survive this process, the drawing code needs to be called in response to the Paint event of the control in question (or in an override of OnPaint for a derived control).
Also, when you move the code you posted to the Paint event handler, use the Graphics object supplied in the PaintEventHandler parameter instead of calling CreateGraphics .
Charlie
if(!curlies){ return; }
|
|
|
|
|
You need to modify your code as below:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics pg = e.Graphics;
pg.DrawEllipse(new Pen(Color.Black ), 0, 0,
this.panel1.Width,this.panel1.Height);
pg.FillEllipse( new SolidBrush( Color.Red ), 0, 0,
this.panel1.Width, this.panel1.Height);
}
|
|
|
|
|
In this way, how i ll get the mouse position to draw?
|
|
|
|
|
Moon Boy wrote:
In this way, how i ll get the mouse position to draw?
Thats windows job! Not yours....
top secret xacc-ide 0.0.1
|
|
|
|
|
Hi MoonBoy,
You want me to write the entire code and logic, then below is it:
int mouseX =0, mouseY =0;
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics pg = e.Graphics;
pg.DrawEllipse(new Pen(Color.Black ), mouseX, mouseY, 50,50);
pg.FillEllipse( new SolidBrush( Color.Red ),mouseX, mouseY, 50, 50);
}
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.mouseX = e.X;
this.mouseY = e.Y;
this.panel1.Invalidate();
}
Do revert back whether it could solve your purpose or not?
Regards,
Jay.
|
|
|
|
|
Hi,
I am learning C# Express and have hit a roadblock with UserControl.
What is the minimum set of steps needed to create and display a dialog derived from UserControl?
So far, I think that I have created a form derived from UserControl.
From the IDE:
Project > Add Windows Form > User Control
This gives me a form and a .cs file.
namespace MyPropertiesDialog
{
public partial class MyPropertiesControl : UserControl
{
public MyPropertiesControl()
{
InitializeComponent();
}
private void MyPropertiesControl_Load(object sender, EventArgs e)
{
}
}
}
How do I invoke this dialog from the main application??
Since this works for message box,
MessageBox.Show("Hello!", "Greeting Box", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
I tried variations on this theme, but no success yet.
Thanks for your help,
Mark
|
|
|
|
|
It's a UserControl , so for example you could place a series of Button 's on it that have specific functionality and then make sure your Form class accesses the Controls collection and Add 's your control, then it should work. Do you have something specific you are trying to do?
- Nick Parker My Blog | My Articles
|
|
|
|
|
A Form is a top-level control that can be displayed directly on the screen. A UserControl is a control which can contain other controls and is placed on a Form . A UserControl is not directly shown on the screen which it seems is what you're trying to do.
So, you'd need to derive from Form and add your MyPropertiesControl to it.
public class MyDialog : Form
{
public MyDialog()
{
Controls.Add(new MyPropertiesControl());
}
} And then show it...
MyDialog dialog = new MyDialog();
dialog.ShowDialog();
Charlie
if(!curlies){ return; }
|
|
|
|
|
Thank you Nick and Charlie,
That was exactly what I was looking for!
I am thinking about doing some data collection via the serial port.
It will be a translation and enhancement of something I wrote in Borland Builder to access RS-485 devices. So far, C# Express seems to have a lot in common with Builder.
Mark
|
|
|
|
|
Salut everyone, i have done lotz of seaching but i haven't find much. Anyhelp is greatly appreciate.
I export a report into a csv file onto the webserver folder, now what i would like to do is zip that file that i just saved and allow the user to download it when they click a button.
Anyone know what class .NET support for zipping a file?
or any sample code on C# is great because i'm stuck here.
merci beaucoup ;)
*HyVong*
|
|
|
|
|
|
|
using java.util.zip;
using java.io;
..
..
i created method to zip the file based on the codes have given.
<br />
private void ZipFile(string fileName, string ExpType)<br />
{<br />
string extType = "zip";<br />
string zipFileName = fileName + "." + extType; <br />
string soureFileName = fileName + "." + ExpType;<br />
<br />
FileOutputStream fos = new FileOutputStream(zipFileName); <br />
<br />
ZipOutputStream zos = new ZipOutputStream(fos); <br />
<br />
FileInputStream fis = new FileInputStream(soureFileName); <br />
<br />
ZipEntry ze = new ZipEntry(soureFileName); <br />
<br />
zos.putNextEntry(ze); <br />
sbyte[] buffer = new sbyte[1024]; <br />
int len; <br />
<br />
while((len = fis.read(buffer)) >= 0) <br />
{ <br />
zos.write(buffer, 0, len); <br />
}<br />
<br />
zos.closeEntry(); <br />
fis.close(); <br />
zos.close(); <br />
fos.close();<br />
}<br />
I have the folder that held the file temporary has full access for users.
I don't know if i have to give the path for it to be able to zip too? Please help, i have no clue why it says access is denied. And is the code above correct?
I haven't had a chance to test the whole codes since the first line had some error. hope to hear from everyone soon. thank you so much.
*HyVong*
oh and when i tried to delete the file after it's being zipped, but i can't, 'File' is an ambiguous reference, how do i delete the file after being zipped?
File.Delete(Server.MapPath(@"~\import\\" + soureFileName));
Thanx so much for all your help.
|
|
|
|
|
actually i got this work, thank you so much for all your help
i changed <code
file.delete(server.mappath(@"~\import\\" +="" sourefilename));="">
to
System.IO.File.Delete(Server.MapPath(@"~\import\\" + soureFileName));<br />
and the file did got zip, i gave it a path for specific folder
FileInputStream fis = new FileInputStream(Server.MapPath(@"~\import\\" + soureFileName));
FileOutputStream fos = new FileOutputStream(Server.MapPath(@"~\import\\" + zipFileName));
ZipOutputStream zos = new ZipOutputStream(fos);
once again, thank you for all your help
*HyVong*
|
|
|
|
|
Search for a library called SharpZipLib.dll on google
|
|
|
|
|
I have discussed this topic in this forum before, but check out #ziplib[^], it should do what you are looking for.
- Nick Parker My Blog | My Articles
|
|
|
|