|
Use the ItemCreated event to add whatever text you want to the datagrid row.
|
|
|
|
|
Sounds interesting, but I don't see the ItemCreated Event in the datagrid class... Like I said earlier, I'm new to C# and the property soup for the various controls. I'm probably missing something obvious!
|
|
|
|
|
I have a fileSystemWatcher object to watch for file changes under some directory and display a message for each file change. However, enven though I change the file only once, the message get displayed twice. The file is a .txt file. Anybody knows why this happens and how to fix it?
Thanks a lot
|
|
|
|
|
Greetings,
I have created a custom collection class that I would like to return from a function (i.e. return customcollection;). However, a runtime error is generated when I call the function that's suppose to return the custom collection class. The error states that I should be returning the object type that's contained in the custom collection. (i.e. if my custom collection holds customer objects, then I should be returning a customer object, not the customcollection object.) The problem is that I need the collection returned with all the customer objects that it contains. (I want to bind this collection to a datagrid) Can anyone help me out with this. I know it's something simple that I'm missing.
Thanks in advance,
Andrew
|
|
|
|
|
[quote]
The error states that I should be returning the object type that's contained in the custom collection
[/quote]
My $0.02:
Sounds to me like a case of mis-match between the function signature and the function code.
What is your function's signature?
For instance, if it is
public myObject myFunction()
You should return an object of type myObject, but if it is
public myCollection myFunction()
you should be able to return an object of type myCollection, and I don't think anyone
-let alone the compiler- would look at what the myCollection object is holding...
Maybe you could post a snipplet?
HTH,
Frank
|
|
|
|
|
Frank,
It looks like I'm on the right track, this is basically what I've got:
public IndividualCustomerCollection findCustomer()
{
IndividualCustomerCollection search_results = new IndividualCustomerCollection();
search_results.Add(new IndividualCustomer("John", "Smith", "(905) 428-1122"));
return search_results;
}
Your help is much appreciated,
Andrew
|
|
|
|
|
hmmm...
...so, search_result is an instance of the IndividualCustomerCollection class...
which looks right...
So, what exactly is the error message you get?
..is it a compile-time error or run-time error?
...also, does the erro rpoint you to a line in that snipplet,
or does it point you to the line where you are calling this function?
FOR
|
|
|
|
|
The program compiles successfully, I get the run-time error when I try to call this function. The error points to the calling function. Here's a bigger snippet:
The following code is located in the cmdSearch_click event handler of the Customer Search Form:
If customer_type = "Individual" Then
Dim search_results As IndividualCustomerCollection = CustomerFacade.processCustomerSearch(customer_type)
lstViewResults.DataSource = search_results
End If
The following code is located on the client, within the business facade layer:
public static IndividualCustomerCollection processCustomerSearch(string customer_type)
{
IndividualCustomerCollection search_results;
string _customer_type = customer_type;
ICustomerFactory CustomerFactory = (ICustomerFactory) Activator.GetObject(typeof (ICustomerFactory, "http://localhost:1234/customerfactory.soap");
//This is where the runtime error occurs
search_results = CustomerFactory.findCustomer(customer_type);
return search_results;
}
The following code is located on the server, within the business rules layer:
public IndividualCustomerCollection findCustomer(string customer_type_
{
string _customer_type;
_customer_type = customer_type;
IndividualCustomerCollection search_results = new IndividualCustomerCollection();
search_results.Add(new IndividualCustomer("John", "Smith", "(905) 428-1122"));
return search_results;
}
NOTE: .NET Remoting is used to pass the collection from the server to the client. I know the remoting works because I've got debug messages that output the status of the server-side code execution. (For example, a message is displayed on the console window when an IndividualCustomer is added to the collection.
I'm truly stumped on this one, and it's something that should be so simple to do. I must be missing something, but what???
Your help is appreciated.
Andrew
|
|
|
|
|
Andrew,
I have no experience of .NET remoting, nor actually of mizing languages...
At first sight I don't spot any red flag...
If the run-time error points to the line
search_results = CustomerFactory.findCustomer(customer_type);
in processCustomerSearch, and has to deal with the data types involved, I would try
and step through the code, to see exactly where the problem arises...
For instance, if you use the debugger and step into the call to findCustomer,
when you reach the last statement in that function (return search_result; ) is
everything in order?
If so, when you execute that last statement, and return control to processCustomerSearch, is everything still in order?
I would add a dummy statement between
ICustomerFactory CustomerFactory = (ICustomerFactory) Activator.GetObject(typeof (ICustomerFactory, "http://localhost:1234/customerfactory.soap");
and
search_results = CustomerFactory.findCustomer(customer_type);
and another one between
search_results = CustomerFactory.findCustomer(customer_type);
and
return search_results;
to set up a break point and check if everything is as it should...
I wish I could point you to the problem, but I don't see what could be the issue...
...if you want, post the error message, which might provide some hints...
but if I were in you, I'd be playing with the debugger over and over to see exactly *where* something goes out of synch... (for instance, does the call to Activator.GetObject in processCustomerSearch act as expected?)
As I said, I haven't played with this kind of stuff, so I might be a poor help on this one (sorry
FOR
|
|
|
|
|
Frank,
Thanks for the debugging suggestions. I haven't tried that yet. I'll let you know when I discover the solution to this issue. I really appreciate you taking the time to help me solve this problem.
Thanks again,
Andrew
|
|
|
|
|
No problem...
..let us know what you find out stepping through the code !
I must admit I am curious...
Frank
|
|
|
|
|
Frank,
I finally solved the problem. The issue was with the following code located in the IndividualCustomerCollection:
#region CollectionBase Overridable Properties
/*Implementation of CollectionBase Property
*Performs additional custom processes before
*inserting a new element into the CollectionBase instance.*/
protected override void OnInsert(int index, Object value)
{
if (value.GetType() != Type.GetType("IndividualCustomer"))
{
throw new ArgumentException("value must be of type IndividualCustomer.", "value");
}
}
/*Implementation of CollectionBase Property
*Performs additional custom processes when
*removing an element from the CollectionBase instance.*/
protected override void OnRemove(int index, Object value)
{
if (value.GetType() != Type.GetType("IndividualCustomer"))
{
throw new ArgumentException("value must be of type IndividualCustomer.", "value");
}
}
/*Implementation of CollectionBase Property
*Performs additional custom processes before
*setting a value in the CollectionBase instance.*/
protected override void OnSet(int index, Object oldValue, Object newValue)
{
if (newValue.GetType() != Type.GetType("IndividualCustomer"))
{
throw new ArgumentException("newValue must be of type IndividualCustomer.", "newValue");
}
}
/*Implementation of CollectionBase Property
*Performs additional custom processes when
*validating a value.*/
protected override void OnValidate(Object value)
{
if (value.GetType() != Type.GetType("IndividualCustomer"))
{
throw new ArgumentException("value must be of type IndividualCustomer.", "value");
}
}
#endregion
I found the error by implementing a try, catch block. I caught the error and displayed the details in a messagebox. The details pointed me directly to the problem.
I'm guessing that I should change Object to IndividualCustomer in all the above method signatures, does this sound correct?
Thanks for steering me in the right direction,
Andrew
|
|
|
|
|
RS2003 wrote:
I found the error by implementing a try, catch block. I caught the error and displayed the details in a messagebox. The details pointed me directly to the problem.
I'm guessing that I should change Object to IndividualCustomer in all the above method signatures, does this sound correct?
Andrew,
glad to hear you found the problem !
As far as using try and catches, my personal experience is that they are a *great tool*, especially during development. While coding (especially in .NET since I rely upon so many .NET classes), I found that my instinct is now to "first of all wrap everything that might throw an exception in a try/catch", and moreover, I try to have a different catch for every different thing that might throw an exception (so that I can quickly find out where the problem is). This might sound an overkill, but a) the VisualStudio documentation (with the Dynamic help) helps a lot in checking what exception (if any) might be thrown by the various .NET classes, and b) I admit I am not that throughfull... There are cases where I know I will never hit a specific error condition because of the way my code is structured, so in some cases I skip the try/catch...
In any case,
as far as changing Object to IndividualCustomer, it sounds like a good idea, especially if this collection should and will always contain only IndividualCustomer objects.
Hope you'll solve this out now,
Frank
|
|
|
|
|
OK, I've got it now. The method below validates the new object by getting its type and making sure its of type IndividualCustomer. The runtime error I was seeing in the beginning (the cause of this whole thread) was the error message contained in the exception arguments (throw new ArgumentException("value must be of type IndividualCustomer.", "value");". According to Microsoft this method is used to:
"OnValidate can be used to impose restrictions on the type of objects that are accepted into the collection. The default implementation prevents a null reference (Nothing) from being added to or removed from the underlying System.Collections.ArrayList."
The question is why is the value null after I perform the IndividualCustomerCollection.Add function?? When I remove the code, the IndividualCustomer is added to the constructor without any problems. According to Microsoft :
"The On* methods are invoked only on the instance returned by the List property, but not on the instance returned by the InnerList property."
**Definition**
The CollectionBase.List Property - Gets an IList containing the list of elements (objects) in the CollectionBase instance.
By this definition, the onValidate method should execute after the the List property returns the list of objects in my instance of the IndividualCustomerCollection. I don't recall overriding the CollectionBase.List property in my IndividualCustomerCollection class. I'll have to check this when I get home and make the appropriate changes.
/*Implementation of CollectionBase Property
*Performs additional custom processes when
*validating a value.*/
protected override void OnValidate(Object value)
{
if (value.GetType() != Type.GetType("IndividualCustomer"))
{
throw new ArgumentException("value must be of type IndividualCustomer.", "value");
}
}
You'll have to excuse me, I'm knew to creating data binding collections and I want to make sure I understand this inside and out. It's nice to use examples from MSDN, but if you don't take the time to understand everything your implementing you'll run into these kinds of problems. I have definitely learned my lesson Does anyone have any suggestions as to why my value is null?
Thanks,
Andrew
|
|
|
|
|
I have an interesting behavior:
[DllImport("user32.dll")]
static public extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
static public extern IntPtr GetDesktopWindow();
When I get the Graphics object through
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
any drawing on the desktop do not result in anything.
When I get the Graphics object through
Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero));
I can draw on the desktop.
Any explanation?
Juergen
|
|
|
|
|
i did it by accident a few weeks ago and im not sure if this is the code i used but i think you could give it a shot
//
okay i remeber how i did it..i originally posted a message here that was long..but i found the solution. you can do it using a class that was already wrote..and ill show you how... first off download the clas here : Link[^]
second....adapt this code segment i just wrote into your needs...and it should work....remeber to add the namespaces that the class you downloaded uses... also the class you downloaded uses unsafe code so you will have to turn on the unsafe option in vs.net you can do it by selecting the properties of your project
<br />
<br />
[DllImport("user32.dll")]<br />
private static extern IntPtr GetWindowDC(int Pointer);<br />
Win32Window ww = Win32Util.Win32Window.DesktopWindow;<br />
IntPtr DC = GetWindowDC(ww.Window.ToInt32());<br />
Graphics jm = Graphics.FromHdc(DC);<br />
jm.DrawLine(Pens.Yellow,130,30,30,130);<br />
jm.DrawRectangle(Pens.Red,100,100,300,300);<br />
|
|
|
|
|
Thanks for your reply,
in my post there already was a working solution.
But the question was why the first solution don't work
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
while the second solution works fine.
Graphics g = Graphics.FromHdc(GetDC(IntPtr.Zero));
In my understanding both should work.
I like to know the reason for this behaviour.
Juergen
|
|
|
|
|
GetDesktopWindow() only returns the handle to the Device Context (DC) so passing that handle into the graphics object is incorrect.
when you call GetDC you are grapping the device context its self from the handle. The device is what you work with to do your drawing function in this case.
hope this helps.
Jesse
The Code Project Is Your Friend...
|
|
|
|
|
The GetDesktopWindow function returns a handle to the desktop window (MSDN).
Graphics g = Graphics.FromHwnd(GetDesktopWindow());
creates the Graphics object.
In the debugger it looks like g is created properly,
(exactly the same values as when created with
Graphics.FromHdc(GetDC(IntPtr.Zero));)
but any Drawing on it, is at least invisible.
Juergen
|
|
|
|
|
it returns the handle to the window its self...but that doesnt necessarily
mean the graphics object. the window propertie in the api is kinda like a form in .net it has many methods/properties to it but you need the Graphics object to draw to the form. Simular to that you need the device context of the 'Window' to draw to it. g would be created properly because it is a handle and it can be drawn to..its just not creating the handle on the correct drawing object.
there are 2 handles....a Form.Handle and Graphics().GetHdc() (i think thats it) GetDesktopWindow() returns the Form.Handle essentially. from that handle u get the GraphicsHandle from GetDC
jesse
The Code Project Is Your Friend...
|
|
|
|
|
I'm trying to serialize a complex data structure, and I have some questions.
- How do I serialize an array? In the GetObjectData that I have to implement, I use the AddInfo function. But it takes two parameters : a "key" in a value.
If I want to save a float[8], how can I do this?
Do something like :
for(int i = 0; i <8; i++)
{
string key = "Array" + i.ToString();
SerializeInfo.AddValue(key, array[i]);
}
- How to I serialize complex object?
Suppose I have a class CDrawing, that cointains an array of CShape. A CShape can be derived into a CCircle or a CRectangle
Somewhere in my code I do
shapeArray[0] = new CCircle;
shapeArray[1] = new CRectangle;
Serializing is not a pb, because I will make the GetObjectData function virtual, so it will use the correct one to write CCircle parameters for the first shape, and CRectangle paramters for the second shape.
But how can I deserialize? How do I know if I must do a new CCircle, or a new CRectanle?
- How can I serialize to a text file? I've found only a binary and a SOAP formatter.
|
|
|
|
|
Stephane David wrote:
How can I serialize to a text file?
If you want to serialize to a text file, you will have to come up with your own format (well, that is assuming you want to be able to read it with your own eyes).
But if you are willing to just save an object or array out to disk and only have the computer read it, you must mark each of your classes with the Serializable attribute. If all the objects (Shapes for you) are serializable, then the array that contains them is automatically serializable.
Serializing is not a particularly difficult thing, I would just need a bit more info on your classes...
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
The real classes are a bit to complexes to explain in details.
But here is I think an exemple which illustrates the system well (warning : minimum syntax!)
class CPoint
{
int X;
int Y;
}
class CShape
{
string name;
virtual void Draw();
}
class CCircle : CShape
{
int Radius;
CPoint center;
override void Draw();
}
class CPolygon : CShape
{
CPoint[] geometry;
override void Draw();
}
class CDrawing
{
public CShape[] shapes;
}
And now, I do this
CDrawing drawing = new CDrawing();
drawing.shapes[0] = new CCircle();
// Initialize the circle
//...
drawing.shapes[1] = new CPolygon();
// Initialize the polygon
//...
Now, I want to save my drawing to a binary or text or XML file, and then load it.
|
|
|
|
|
Stephane David wrote:
class CPoint<br />
{<br />
int X;<br />
int Y;<br />
}
First: This little class is unneeded; check out System.Drawing.Point .
Second: You then need to mark your classes with Serializable attributes.
[Serializable(true)]
class CShape
{
string name;
virtual void Draw();
}
[Serializable(true)]
class CCircle : CShape
{
int Radius;
CPoint center;
override void Draw();
}
[Serializable(true)]
class CPolygon : CShape
{
CPoint[] geometry;
override void Draw();
}
[Serializable(true)]
class CDrawing
{
public CShape[] shapes;
}
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
void SaveToFile(string path,CDrawing drawing)
{
using(FileStream fs = File.OpenWrite(path))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs,drawing);
}
}
CDrawing LoadFromFile(string path)
{
using(FileStream fs = File.OpenRead(path))
{
BinaryFormatter bf = new BinaryFormatter();
return (CDrawing)bf.Deserialize(fs);
}
}
Hope this gets you on your way...
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
The actual name of classes were just illustration, ht class architecture is muh more complex. I'll try what you say. It was what I read in the documentation, but I was doubtfull it would actually save an array of derived classes as the shapes member in my CDrawing class.
|
|
|
|
|