|
the problem is: i have to do something when the computer start an internet connection.
I can't simply control the status...i want to intercept the event "On Connected"..
I hope you understand
VentoEngine corp.
Program your life ^^
|
|
|
|
|
I have a class I am calling SimTreeNode which is a System.Windows.Forms.TreeNode type. I am planning on using this class within my TreeView class.
Class Definition:
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace SimTree<br />
{<br />
public class SimTreeNode : System.Windows.Forms.TreeNode<br />
{<br />
<br />
}<br />
}
As you can see, I don't do anything more or less than the TreeNode class.
The problem is, when I try to use my SimTreeNode in place of any TreeNode in my TreeView class, it throws runtime errors.
Here is the snippet of code I am doing the conversion.
using System.Collections;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Drawing;<br />
using System.Data;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Xml;<br />
using System.Xml.Serialization;<br />
using SimTree.Properties;<br />
using System.Runtime.InteropServices;<br />
<br />
namespace SimTree<br />
{<br />
public partial class SimTree : System.Windows.Forms.TreeView<br />
{<br />
<br />
private void SimTree_ItemDrag(object sender, ItemDragEventArgs e)<br />
{<br />
Point pt = this.PointToClient(Control.MousePosition);<br />
SimTreeNode tn = (SimTreeNode)this.GetNodeAt(pt);
<br />
...<br />
}<br />
}<br />
}
Runtime Error:
Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'SimTree.SimTreeNode'.
I would like to know why I cant caste my derived type to my base type.
According to a million online forums, I don't need to make an explicit operator to caste to/from base types. I actually tried to make an explicit caste operator if you look at my comments in the SimTree.SimTreeNode class, but gave me a compiler error:
Error 1 'SimTree.SimTreeNode.explicit operator SimTree.SimTreeNode(System.Windows.Forms.TreeNode)': user-defined conversion to/from base class C:\...\SimTree\SimTreeNode.cs 10 23 SimTree
This really doesn't make any sense to me as the compiler SHOULDN'T have anyway of telling how to convert a base type to a derived type without me telling it explicitly.
I am hitting brick walls left and right with this simple thing I want to do.
|
|
|
|
|
Are you sure that all the nodes in your tree actually are SimTreeNodes? How are you adding nodes to the tree?
Melon00 wrote: I would like to know why I cant caste my derived type to my base type.
Aren't you attempting to cast from the base class (TreeNode) to the derived type (SimTreeNode)?
|
|
|
|
|
Post the code where you add nodes to the tree. Let's start there.
Josh
|
|
|
|
|
The object that you are trying to cast simply isn't a SimTreeNode. One reason could be that the GetNodeAt method didn't find any node at the point, and is returning a null value.
You have to check if the node is null before you try to cast it.
If the TreeView can contain other nodes than SimTreeNodes you should use the as keyword to cast the node. If it's not a SimTreeNode it will return null.
---
b { font-weight: normal; }
|
|
|
|
|
Just been through this myself. Life would have been so much easier if the Treeview had a free property of type Object then we could have easily added a helper class with additional properties and methods. Anyway, back to your problem. I suspect you added your SimTreeNode as a TreeNode. You can only cast in one direction. You can cast SimTreeNode stored in a Treenode variable to a SimNode, But you cannot cast a TreeNode stored in a TreeNode variable to SimNode.
You used the word converting. That's not really appropriate as the objects are not converted. All it is doing is copying pointer. We know that the derived class has all the facilities of the parent class so casting from SimtreeNode to TreeNode is safe. If the compiler let you go the other way we would end up with:
TreeNode tn = new TreeNode()
SimtreeNode stn = (SimTreeNode)tn
stn.SomeMethodThatDoesNotExistinTreeNode()
So the compiler does not allow it.
Take a look in debug just as you execute the statement and make sure that you do objects of the type you needed. Overriding ToString is always useful when you extend TreeViews.
To ensure you are safe override in SimTreeView the Add method for a treenode and make it private. Then add your own Add method for SimTreeNode
Paul
|
|
|
|
|
Hi all !
In c#.net 2.0 , there is a class "Persian Calendar", and I want to use this class with DateTimePicker control so that this control(DateTimePicker) return(show) Persian date value . How can I do it ?
Thanks for any help !
Regrads ,
s_mostafa_h
|
|
|
|
|
|
hi !
there is an error in opening file . in vs 2005 , I can not open this file .
regards,
s_mostafa_h
|
|
|
|
|
hi all,
how should i prepare access so as to insert datas for date and time exactly like this :
date: 06/06/2006
and
time: 13:00
ps: i have an example database table. date's format is: short date and time's is: short time.
in database everything seems normal. but if i read data from database:
date becomes: 06/06/2006 12:00:00 PM
time becomes: 12/01/1988 13:00 PM
why i get these bold parts ?
what should i do so as not to get them ?
help please,
thanks in advance.
-- modified at 10:21 Tuesday 6th June, 2006
|
|
|
|
|
Why not put them into the same column then? Your dates are always going to show up like this in a date field. If you want it to be in the database in a specific format, make it a char field.
|
|
|
|
|
Because Access uses a date/time datatype and not seperate date and time types. If you provide only the date it will automatically use a default time and vis-vesa.
Just ignore them and read the date or time as appropriate. Or else store the date and the time as strings. Or if the problem is with how things look in Access you can specify the format within Access itself (but it will still be stored with a date and a time).
|
|
|
|
|
Hi there,
I'm quite exhausted now trying to directly set the Scan0 Paramater of a Bitmap to avoid slow copying bitmaps in memory.
Basically, I try to do the following:
1.) I have a buffer containing already loaded Bitmap files in memory. Fo r performance reasons, this buffer is allocated via VirtualAlloc and therefore unsafe.
2.) I'm passing a UnmanagedMemoryStream with a subportion of the buffer to my Drawing function, pointing to the beginning of the bitmap file.
3.) I'll try to instantiate a bitmap, using the Streampointer to use the buffer directly and draw it to the screen ( or better, a backbuffer).
This looks like this:
<br />
<br />
unsafe<br />
{<br />
UnmanagedMemoryStream mstr= new UnmanagedMemoryStream((byte*)buf.ptrStart.ToPointer(), buf.Count, buf.Count, FileAccess.Read);<br />
}<br />
BitmapData bmd;<br />
<br />
BufferBitmap = new Bitmap(1920, 1080);<br />
<br />
ImageLockMode fLock = ImageLockMode.UserInputBuffer;<br />
<br />
bmd = BufferBitmap.LockBits(new Rectangle(0,0,1920,1080),<br />
fLock,PixelFormat.Format24bppRgb);<br />
unsafe<br />
{<br />
bmd.Scan0 = new IntPtr(mstr.PositionPointer+14);<br />
}<br />
<br />
BufferBitmap.Unlock(bmd); <br />
<br />
GfxBuffer.Graphics.DrawImageUnscaled(BufferBitmap, new Point(0,0));<br />
<br />
Well, all I get is a "Invalid Parameter" Exception when setting the LockBits with the UserInputBuffer Flag, or, if I use the ReadWrite Flag, I would probably have to use the buffer the Bitmapobject manages, which would be an unnecessary copy operation. I just want to display the bitmap I've already in memory which the smallest possible effort.
Does anybody have an idea whats going wrong here ? According to some examples I found, it should work this way. Interestingly, even if a try to pass a buffer allocated with Marshal.AllocHGlobal the LockBits operation fails...
The buffer contains valid data, because this code works, but I think, this causes every time an instantiation and copy procedure:
<br />
<br />
Bye,
Florian
|
|
|
|
|
Hey Ya'll.. I'm building a little analyzer utility that will analyze text files automatically. Currently you have to click the Open Files button to load a file to analyze.
I'd like to mod this program so that if you double click the file you want to analyze, it will spawn the analyzer and automatically analyze the contents of the clicked file.
I've set my PC to always Open With the analyzer when you click on the files with my extension, but I'm at a loss on how to get the filename / path of the file that spawned it.
Can anyone point me in the right direction?
Thanks in advance
-Chris
|
|
|
|
|
Chris,
I'm going to assume that you've set the file type in Folder Options in Explorer to load your application automatically. In the Advanced dialog, you get the command line to run. Using %1 on the command line should give you the FQ path to the file that was double-clicked.
Change
"c:\program files\myapp\myapp.exe"
to
"c:\program files\myapp\myapp.exe" %1
Make your Main() into Main(string[] args), and args should contain the path/file that was clicked on.
Stephan
|
|
|
|
|
Got it.. Seems to be working great!
Thanks a lot!!
-Chris
|
|
|
|
|
It should be passing the filename as the first argument to your app.
void Main(string[] args)<br />
{<br />
}
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
What was working is now not! I'm not exactly sure where the problem resides, but after I built my installer, and installed the application on my PC everything seemed to be fine.
Then when I click the log file to analyze, it keeps opening back up using notepad.
So I went to Open With.., and browsed to my .exe, and checked the option to always open files with this extention. When I double click the file now I get a critical error stating that the LOG file I double clicked and it's path is not a valid Win32 Application.
How do I reset the target so that when I click the log file, it uses my .exe as the application?
|
|
|
|
|
Hi,
I am developping an Add in in C# with Visual Studio 2005 for Excel 2003 and I have a problem when Garbage collector is called for freeing memory. Let see a little example to explain the problem.
first test:
I have created a tool bar with a simple button, here is the code of the Click button events :
private void test_Click(Office.CommandBarButton cmdBarbutton, ref bool cancel)
{
string[] test = new string[10000000];
for (int i = 0; i < 10000000; i++)
{
test[i] = very long string ;
}
}
after the process was executed once, I click a second time the button and then see with Process Explorer that a new thread called "CreateApplicationContext" is created in Excel process and I never can click again on my button (test_Click is never executed again).
the second test i have done is to put this 2 lines at the end of my click event code:
test = null;
GC.Collect();
so after GC.Collect() is called the same thread is created and I can't click again on my button.
So, I am sure that in first test, it is the implicit call to the Garbage collector that crashs my functionnality.
DOes Anyone have any info about this issue?
|
|
|
|
|
Hi everybody
I'm currently experimenting with design patterns, one of them is the design pattern. First some code:
This will init my application, it will init itself then by switching from state to state
static void Main(string[] args)
{
ScreenInitState startState = ScreenInitState.GetInstance();
Console.WriteLine("All states exited");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
Here we switch from state to state
abstract class InitState
{
protected void Switch(InitState state, bool bStopSelf)
{
if (bStopSelf)
Stop();
}
abstract protected void Stop();
}
class ScreenInitState : InitState
{
private ScreenInitState()
{
Switch(InputInitState.GetInstance(), true);
}
}
class InputInitState : InitState
{
private InputInitState()
{
Switch(SoundInitState.GetInstance(), true);
}
}
So the app starts like this:
Main -> SreenInitState -> InputInitState -> SoundInitState -> ...
Now when I'm in the last state, do all previous states still exist in memory or are the collected by the GC? Though this pattern is very useful I'm concerned about memory usage here. Does anyone know what happens to the objects during runtime?
regards
-- modified at 9:54 Tuesday 6th June, 2006
modified 12-Sep-18 21:01pm.
|
|
|
|
|
What's behind SoundInitState.GetInstance()? Every time I've written something with an Instance property or a GetInstance(), it's been a singleton. If all of your states are singletons, the GC isn't collecting them. Also, what does Stop() do in each of the states?
You'd need to be designing something pretty complicated to warrant singleton states. In general, states work better as flyweights. I'd say use structs, but they break the inheritance model.
|
|
|
|
|
Stephan Samuel wrote: What's behind SoundInitState.GetInstance()? Every time I've written something with an Instance property or a GetInstance(), it's been a singleton.
Correct, that's the case here
Stephan Samuel wrote: Also, what does Stop() do in each of the states?
Just a function where the state will cleanup itself (remove resources etc) before switching to the next state
Stephan Samuel wrote: the GC isn't collecting them
So you suggest to use some sort of structs + switch-calls to keep the memory footprint as small as possible? Or does it help to design my states as normal instances instead of singletons?
modified 12-Sep-18 21:01pm.
|
|
|
|
|
You should keep your states small. Structs won't work because they don't implement inheritance, but try to keep the information inside your concrete state classes to a minimum. I would avoid the singleton pattern for states.
Currently, your states don't really switch. Each tunnels through to the next. There should probably be one top-level controller (it may be a singleton but may not need to be) that decides which state you're in and runs the states:
(Please pardon any typos or syntax errors; I'm making this up as I go.)
<br />
public class MyController<br />
{<br />
State currentState;<br />
<br />
public void RunMe()<br />
{<br />
while (this.currentState != null)<br />
this.currentState = this.currentState.RunMe();<br />
}<br />
}<br />
<br />
public abstract class State<br />
{<br />
public abstract State RunMe();<br />
}<br />
<br />
public class ConcreteState1()<br />
{<br />
public RunMe()<br />
{<br />
return new ConcreteState2();<br />
}<br />
}<br />
<br />
public class ConcreteState2()<br />
{<br />
public RunMe()<br />
{<br />
return null;<br />
}<br />
}<br />
Likely, you'll want to pass around some objects, and your states will pass out different "next state" objects as RunMe() returns based on what those objects look like. Each state lives only as long as it's executing, so there's no memory overhead. If you're worried about setup/tear-down performance of your state objects (e.g. -- if they have a complicated DB open process and you need them to run really quickly with persistent connections), consider some level of factory for the states: either have a connection factory, give each state its own factory and let it decide when to create a new one of itself, or create a factory class (or implement one in MyController) that decides the lifetime of your state objects.
I'd also implement IDisposable and the C# disposal pattern for each of your state objects to clear up unmanaged resources. Managed resources should get cleaned up automatically.
The other way to implement this is to have MyController decide which state is next based on the state of some object. My personal belief is that method isn't as good. It's less OO: you might as well just code all the possibilities within the controller and forget the states.
|
|
|
|
|
Great. The Controller object really makes sense and keeps memory overhead at a minimum, that's a really good way, I like. The only addition I will make is to keep a Stack<State> in my controller, so that some states will still be able to overlap each other, like a menu on top of a game.
Thanks a bunch!
modified 12-Sep-18 21:01pm.
|
|
|
|
|
As you are switching from state to state by using a method in each object, the objects can't be garbage collected. The Switch method of each previous state is still executing until the last state is finished. When the last state returns from the Switch method, all previous Switch calls will finish in turn and release the objects.
This is not a problem, however, as long as you make the Stop method release all objects contained in it's state object.
---
b { font-weight: normal; }
|
|
|
|