|
Did You added the attributes [Serializable] in classes ?
this should be fix the problem :
[Serializable]
class Config
{
public int a;
public int b;
public SimpleClass classinst;
public Config()
}
[Serializable]
class SimpleClass
{
public int c;
public int d;
public SimpleClass()
}}
}
|
|
|
|
|
Hi
i want to cast some xEventHandlers to base System.EventHandler, i write this sample class :
public class myClass
{
public event EventHandler ControlVlaueChanged;
public event DataGridViewCellEventHandler DataGridViewCellDataChanged;
public FormEx()
{
this.ControlVlaueChanged += new EventHandler(this.OnControlValueChanged);
}
private void myMethod(Control control)
{
if (control is DataGridView) (control as DataGridView).CellValueChanged += (DataGridViewCellEventHandler)this.ControlVlaueChanged;
}
protected virtual void OnControlValueChanged(object sender, EventArgs e)
{
}
}
i want when CellValueChanged was occurs, OnControlValueChanged execute automatically, for this, in myMethod method, i try to cast our EventHandler to DataGridViewCellEventHandler, but the following error message shown me :
Cannot convert type 'System.EventHandler' to 'System.Windows.Forms.DataGridViewCellEventHandler'
how to solve my problem ?
thanks
|
|
|
|
|
Hi,
What are you trying to do is not very clear
You are trying to cast delegates? In dont think that casting is possible for delegates.
You can create a delegate that handles your controls event :
public delegate void MyEventHandler (object sender, EventArgs e);
...
void AMethod (Control c)
{
c.OnSomeEvent += new MyEventHandler(SomeMethod);
}
void SomeMethod (object sender , EventArgs e)
{
...
}
|
|
|
|
|
Thanks lisan_al_ghaib
as you can see in above code, i have a general eventHandler method to handle all events that occurs:
protected virtual void OnControlValueChanged(object sender, EventArgs e)
{
}
i want when DataGridViewCellDataChanged event occured, the above event handler method handle it, but how to do ?
thanks
|
|
|
|
|
Hi !
Here is a little sample:
void AddControlToMyEventHandler (Control c)
{
if (c is DataGrid)
{
((DataGrid)c).DataGridViewCellDataChanged += new EventHandler (OnControlValueChanged);
}
if (c is TextBox)
{
((TextBox)c).TextChanged += new EventHandler (OnControlValueChanged);
}
....
}
This should work
|
|
|
|
|
Thanks lisan_al_ghaib
but i try this, before post, this code :
private void AddControlToMyEventHandler
{
if (control is DataGridView) (control as DataGridView).CellValueChanged += new EventHandler(this.OnControlValueChanged);
}
couse the following error message shown me :
Cannot implicitly convert type 'System.EventHandler' to 'System.Windows.Forms.DataGridViewCellEventHandler'
|
|
|
|
|
hdv212 wrote: Cannot implicitly convert type 'System.EventHandler' to 'System.Windows.Forms.DataGridViewCellEventHandler'
That is pretty self explanatory. Your event hooking should be
(control as DataGridView).CellValueChanged += new DataGridViewCellEventHandler(this.OnControlValueChanged);
|
|
|
|
|
Thanks N a v a n e e t h
This code work, but my problem was not solved, i need to cast my event handler to another event without create new event handler, in other word, i want cast current eventHandler (this.OnControlValueChanged) and pass it to DataGridView.CellValueChanged eventHandler.
thanks
|
|
|
|
|
Hi,
I'm a rather new C# user and I'm still figuring out the ropes of forms programming and such.
For the part of the program I'm working on, I basically want to move an image around on the screen, in a region (advanced, I know <end sarcasm="">). I achieved this just fine with mouse click and drag, but I'm unable to get any sort of keyboard input. Here's the part of the code I'm using:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
using(Region boardRegion = new Region(new Rectangle(ClientRectangle.X, ClientRectangle.Y,
(int)(ClientRectangle.Width), (int)(ClientRectangle.Height * boardRegionSize))))
{
g.Clip = boardRegion;
using(Bitmap boardImage = new Bitmap(board.ImagePath)){
g.DrawImage(boardImage, board.X, board.Y);
}
}
base.OnPaint(e);
}
private void CMGame_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
board.Y -= 30;
}
And for part of InitializeComponent(), I have
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.CMGame_KeyDown);
(automatically done by windows)
Basically, nothing happens when I press a key (the up arrow or anything else, since I've tried different keys in testing)
I read somewhere that I could try overriding WndProc, but I don't know how I would do this (even after I wrote the override method header and and found the code for KeyDown, 0x0100)
For reference, I also have a button in the form with a click event.
Help would be much appreciated.
|
|
|
|
|
You need to tell the control that's holding the image that is needs to be redrawn either by calling it's invalidated method or calling onpaint directly depending on the situation. You could hook this up to be done automatically by putting it in a locationchanged event which you can fire whenever X or Y is changed
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Right, I actually had that earlier before I started messing with things. Even with this.Invalidate() after it, nothing happens. I think that the event isn't being fired at all, since, for parts of my test, I just told the keydown, keypress, and keyup to change the text of the form. Nothing happened anytime I pressed a button.
|
|
|
|
|
Ok, I found my problem. Once I removed my button, the key input worked again. Can anyone tell me what else would have worked?
modified on Sunday, August 17, 2008 7:36 PM
|
|
|
|
|
Is it a focus problem? Was the button getting the keyboard input? If so, setting the form's KeyPreview property to true might be the solution.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Thanks, setting the KeyPreview to true worked perfectly for me.
|
|
|
|
|
Hola!
Here's a short description of my what I wanna do:
I'll create a program which is running all the time
I've got the computer on and it will listen to my typing,
and if I type a certain word, let's say "calculator" the
program will start the calculator up.
My problem is that I have no idea of how to get started with
the keylistening.. Ofcourse it needs to work while out of focus
too.. I need a push in the right direction - anyone?
/Joplinazz
|
|
|
|
|
search here and google for keyboard hooks
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
|
Hi
i define this interface :
public interface Interface1
{
void myMethod();
}
then create class which inherited from our interface :
public class Class1 : Interface1
{
#region Interface1 Members
private void myMethod()
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
as you can see in second code, i set modifier of myMethod to private, but the following error message has shown me :
'WindowsApplication10.Class1' does not implement interface member 'WindowsApplication10.Interface1.myMethod()'. 'WindowsApplication10.Class1.myMethod()' is either static, not public, or has the wrong return type.
i want to set it's modifier to private to prevent access to this method outside of my class, but how to do ?
thanks
|
|
|
|
|
This doesn't quite make sense. Interfaces are used to quarantee existence of methods, properties, indexers or events that are public. For this reason implementation must be public.
If you don't want the method to be callable outside of your class, don't put it in an interface.
Mika
|
|
|
|
|
Mika is correct, it doesn't make sense to declare interface method implementations as private , that's not what they're for.
You can however use an abstract class like this:
public abstract class Vehicle
{
abstract protected void SomeMethod();
}
public class Car : Vehicle
{
protected override void SomeMethod()
{
throw new NotImplementedException();
}
}
However, the lowest access level is protected in this case.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
If you implement it Explicitly
void Interface1.MyMethod()
{
throw new NotImplementedException();
} then it will remain invisible unless a Class1 instance is explicitly cast to Interface1 like
Class1 class1 = new Class1();
Interface1 interface1 = (Interface1)class1;
interface1.MyMethod();
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
That's true, however then this sort of defeats the purpose of an interface, because often you just do something like
IInterface obj = Factory.GetSomeImplementation();
Usually you don't access (and don't care about) the direct implementation for an instance.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
I totally agree - just trying to give the OP an alternative that may work in his situation.
A better way may be to have a base interface without the method that he could use and a second interface that inherits from the first for when he needs the method
public interface Interface1
{
}
public interface Interface2 : Interface1
{
void myMethod();
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
If you don't want your method to be public or accessible via the interface (explicit impl) then the solution is very simple:
remove the interface from your class since you are not using the class through that interface..
|
|
|
|
|
I am setting the image in a treeview by:
oNode = treeview1.Nodes.Add(sCaption);
oNode.Tag = sTagValue;
oNode.ImageKey = sImageKey;
oNode.Expand();
The images are being set fine. But when I click on a node, the icon changes to the icon set on the root node. When I click a different node, the prior node's image changes back to its correct icon.
Anyone know what I'm doing wrong?
Everything Makes Sense In Someones Mind
|
|
|
|