|
HI. I'm just learning c# and need a little help with events.
(the following scenario is just an example...hopefully to simplify)
Lets say I have a custom control called Cars which has three states: drive, park and reverse
Now let's say i have a racetrack control that can contain any number of Cars controls, but does not NEED to know anything about those Cars, like their state or even how many of them there are.
Lets add a button to the racetrack control called Go.
When the user clicks Go, I would like an event to be fired that all the Cars can hear and process accordingly. (The ones in park ignore it, those in reverse start moving backwards, those in drive move forward...)
I would also like the Cars controls to be able to fire their own custom events (like HornHonked...) that can be heard by anything, like other Cars, the racetrack, or other controls (pedestrians, pigeons...).
I don't know how events work in C# .NET, so I'm not sure where to start. I came across Instrumentation, but don't know if this is appropriate for such a task. I don't know if I can use existing events somehow, or inherit from events or just create my own. I don't know how you tell parents and children what to listen for and how to make sure they hear it.
I thought there might be a better solution than:
User hits the Go button. In Go_OnClick(), the racetrack iterates through a collection of Cars controls (updated as Cars are added to, removed from the racetrack) and calls a Cars.Go() method.
Like I said, I'm just learning C# from scratch, so I'm not aware of all the features. Sorry about the dumb Cars stuff, it just seemed like an easy example.
Thnaks,
Tym!
|
|
|
|
|
so typical, as soon as i posted, i made some headway in my search. I think I have some options:
- have my custom child controls subscribe to the OnClick event of the Go button on the parent Racetrack control. So, when the onclick event is fired, all Cars controls who subscribe will receive the mesasge. This, however, ties the parent and child controls together, ie, I couldn't plop the Cars controls into, say, a driveway control, without, at a minimum, having the driveway control send an identical OnClick event...
- have the parent control publish a custom event that the child controls can subscribe to, and fire this event when the go button is clicked, or as necessary. this has the same problem as above, a new container/parent control must publish the same event
- this would be wonderful, but not sure if it's legal. in the namespace of the Cars control, but not in the Cars class itself, publish a custom event:
<br />
public event EventHandler GoEvent;<br />
then, in the parent control, fire the event when necessary, as in the OnClick event of the Go button:
<br />
if( GoEvent != null )
and, in the Cars control Class, subscribe to the event:
<br />
CarsNamespace.GoEvent += new EventHandler(OnGoEvent);<br />
Then when a Cars control receives a GoEvent, it will call OnGoEvent().
So, that is what I'd like, but it seems like just throwing it in the namespace like that wouldn't be enough, or is it?
I feel like I am close, but just barely missing something. Like, would I need to declare the GoEvent in the parent??
ok, sorry if you read this far and want the last 3 minutes of your life back... but I just found a little more out and it seems that the namespace thing is a bust, but the second method looks like the way it really works. And my "fix" for the problem of tying the parent to the child would apparently be to create a kind of CarsManager class that handles adding and removing cars, and publishes the events and the event would be triggered by a CarsManager method call. Sorry to make you suffer through my learning process. but, if i'm still missing something or if i made some bad choices, assumptions, I'll always listen to anything...
Thanks,
tym!
PS thanks to moredip who posted this stuff here.
|
|
|
|
|
I'm having a problem when generating a bitmap in memory. I have an image which I want to cut up and rearrange in memory.
When I cut the image up and .DrawImage to the bitmap in memory, it gets 'smudged'. Say the source image is 20px high, I create a 20px bitmap and .drawImage the bit of the source i'm interested in.
The dest image though is incorrect, the pasted sections are drawn at about 22px, even though I have checked, loads of times, the parameters passed to drawimage are correct (20).
Anybody had similar problems?
|
|
|
|
|
What you are describing is alignment problems, not anti-aliasing AA is applied to vector graphics such as fonts and lines, to get rid of the litle jagged edges.
The obvious thing to look for is whether the images proportions are not changing during the "copy" process. That could be one cause.
<a TITLE="See my user info" href=http:
|
|
|
|
|
The actual target image is still the right size, but the drawn areas are too high anfhence are stretched, even using DrawImageUnscaled
|
|
|
|
|
Hi!
I'd like to create an "id" class, i.e. a class which could hold any positive non-null integer, yet somehow semantically generated ids would be > 0, but 0 and -1 would have special meanings.
Basically something like that:
public class MyId: System.Int32
{
public const int kServer = 0;
public const int kBroadcast = -1;
}
Unfortunately that is not possible since System.Int32 is a struct and thus equivalent to a sealed class: I can't derive it.
Can anyone suggest another approach to this?
Maybe the class could just not derive from Int32 and just contain an Int32, generated to be unique from the constructor.
Thanks in advance, all ideas welcome.
R/
|
|
|
|
|
Containing a int32 is your best bet. If you'd like, you can write implicit conversions to and from int32, so you'd be able to write:
MyID id = 13;
or code like that.
|
|
|
|
|
I understand how to write an implicit conversion to in32, but I don't get how to write an implicit conversion from int32 to my class.
Currently I came up with something like that:
public struct MyId
{
public const int kServer = 0;
public const int kBroadcast = -1;
public override bool Equals(object obj)
{
return mId.Equals(obj);
}
public override string ToString()
{
return mId.ToString();
}
public override int GetHashCode()
{
return mId.GetHashCode();
}
public static implicit operator int(MyId id)
{
return id.mId;
}
private int mId;
}
The C# reference says operator = cannot be overloaded.
What I my missing so far?
Is it useful to overload GetHashCode/ToString/Equal?
TIA
R/
|
|
|
|
|
Funny, I could not find any reference to Stephen Toub in GotDotNet, yet I found a nice team page for Eric and his article on MSDN regarding operator overloading
Anyway, I am in the right direction when I say that:
- it's a C++-programmer reflex of mine to expect to be able to overload operator=()
- in the case above, I actually don't need such an operator=, what I need is an operator from int to MyId, such as:
public static implicit operator MyId(int id)
{
MyId v = new MyId; v.mId = id; return v;
}
R/
|
|
|
|
|
To "override" the = operator, all u need to do is an implicit cast
<a TITLE="See my user info" href=http:
|
|
|
|
|
Here's how you overload the conversion from int to your class:
public static implicit operator MyId(int id)
{
return new MyId(id);
}
|
|
|
|
|
As siad by Eric your best best is to make a continer class. Stephen Toub from MS has written an execellent (as all his stuff) utility for this. Search GotDotNet for stoub (his username).
<a TITLE="See my user info" href=http:
|
|
|
|
|
leppie wrote:
Search GotDotNet for stoub (his username).
I had to look on his blog just to find his username...it's toub...not stoub.
Hawaian shirts and shorts work too in Summer.
People assume you're either a complete nut (in which case not a worthy target) or so damn good you don't need to worry about camouflage...
-Anna-Jayne Metcalfe on Paintballing
|
|
|
|
|
I am having a couple of problems setting form size at run-time.
First problem is when I first show the form, it sets the size of the form to 148x40 when I specify the width less than 148 or hight less than 40, they reset to 148 and 40 respectively.
It happens during handle creation time and I can't figure out where the form gets the 148x40 values. I happens somewhere in Control's constructor.
I tried overriding DefaultSize property but the size still defaults to 148x40 if I specify smaller dimentions.
The workaround I use is to force handle creation and then set the size of the form after.
Second problem is that when I set the height of the form and then show the form, the form adds extra height during handle creation time if I have a main menu specified.
Once again the workaround is to set the size after the handle has been created.
However I would like to know if I am missing something and if there is a better way of fixing these problems. Perhaps it is a bug in the form creation logic.
I wish I could see the source of the library so I knew what it was doing.
|
|
|
|
|
Did you look in the InitializeComponent method? That's where the forms designer does its initialization.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Yes, I did.
148x40 does not come from there.
|
|
|
|
|
Acidis wrote:
I wish I could see the source of the library so I knew what it was doing.
You can always use ILDasm to see the IL or use Anakrino or Reflector to decompile see C#, MC++, or VB.NET versions of the IL.
As to your specific problem I haven't seen that before, just doing a quick test I can resize my forms all the way down to 109w 28h which is the minimum size allowed by Windows. Depending on your Appearance settings, your minimum may differ.
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
I might have missed a few details.
Here is the set up of my form. I call it ToolTipForm.
Properties that are different from default:
FormBorderStyle: None
AutoScale: False
Size: 100, 50
StartPosition: Manual
ShowInTaskbar: False
The form contains only one Lavel control.
Label properties:
BorderStyle: FixedSingle
TextAlign: MiddleCenter
UseMnemonic: False
Dock: Fill
Now, if you create a main form and add some way to display the ToolTipForm. USe the following code to show it:
<br />
ToolTipForm ttf = new ToolTipForm();<br />
ttf.Size = new Size(50, 18);<br />
ttf.Show();<br />
The first time you display this form (when it creates the handle) it will set the size of the form to 148x40 (in my case.)
Given the form set up, it should not do it.
If I set the Size property to 50, 18 after the Show() method, it sets the size to 50, 18 without any problems.
The reason I am using the form is because I need my tool tip to be displayed on top of my main window (form) so it is not clipped. I don't think the tooltip control is capable of doing it. And I was unable to make a control display as a top level window. Controls seem to require a parent.
As to decompiling, I am not sure how to do it. I have the Reflector but it does not allow me to decompile anything in the library. I am probably not understanding how to do it properly. Will have to look at it some more.
|
|
|
|
|
Acidis wrote:
I don't think the tooltip control is capable of doing it.
Using .NET 1.1 the ToolTip control isn't getting clipped by the child main window or by child windows. I don't know if this is something that changed between 1.0 and 1.1 though.
Acidis wrote:
I have the Reflector but it does not allow me to decompile anything in the library. I am probably not understanding how to do it properly. Will have to look at it some more.
A new version was released recently that added decompiling to the list of features. When you find a method you want to see the source to, you can press Enter to see the IL or Space to see the IL->C# or IL->VB.NET conversion.
http://www.aisto.com/roeder/dotnet[^]
James
"I despise the city and much prefer being where a traffic jam means a line-up at McDonald's"
Me when telling a friend why I wouldn't want to live with him
|
|
|
|
|
I red the page for this menu, and I found it very interesting, so I downloaded the code, but I can’t find any sample, and couldn’t figure out how to use it. Has anyone already integrated this program with existing codes? What I’d like to do is for each form, make a XML menu description, and make an object of this class, and call a method by passing the XML file to make a menu for the form. Any help would be greatly appreciated.
|
|
|
|
|
Send me your email address and I'll send you an example, from which you can rape and pillage whatever code you need. In the meantime, I'll check that article's download source.
Marc
Help! I'm an AI running around in someone's f*cked up universe simulator. Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus Every line of code is a liability - Taka Muraoka Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"
|
|
|
|
|
Marc,
Thanks a lot man, I really appreciate, and your code is really great. It's
exactly what I was looking for, and precisely what MS should make in the
future version for the windows application menus. anyway my email address as
you can see is Emmanuel_Soheyli@Hotmail.com
Thanks again and keep up the nice work that you're doing.
Emmanuel
|
|
|
|
|
Hello there,
I'm getting crazy.
Maybe somebody of you guys have some good ideas how to solve this.
Imagine, There is an existing API in C++.
The main idea of this interface is to exchange byte arrays with a server.
In the example below you see the principle.
I create e.g. a object myCommand from the 'Command' struct which again consists of enumns and structs.
The idea is to to send these binary data of the enumns and structs down to the server.
In c++ i create this object and send it like that:
Socket.send(&myCommand, sizeof(blabla));
As you see i just need to give a pointer to my data.
I decided to translate this interface to C#, since writing a wrapper would be about the same work(?!).
Anyway, the biggest problem i have is:
How can i create a byte[] from myCommand object, containg all the numerical enum and struct data to be able to send it to the server???
Sockets in C# are only accepting byte[] as parameter, but how to convert 'myCommand Object' to byte?
There are dozens of examples how to decode strings to byte arrays but I did not found any material abot converting my stuff!
One thing i thought about is to use serializing, but since the future app should run on a PocketPC on the Compact Framwork,
this is not possible since the .NET CF is not supporting serializing......
Any help, ideas tips and tricks would be greatly appreciated!
regards,
stonee
<br />
enum DataType<br />
{<br />
Command = 0,<br />
Error = 1,<br />
Result = 2,<br />
};<br />
<br />
enum Command<br />
{<br />
Exit = 0, <br />
GetValueA = 1, <br />
GetValueB = 2, <br />
GetValueC = 3, <br />
GetValueD = 4, <br />
};<br />
<br />
<br />
struct Header<br />
{<br />
public long lPacketSize;<br />
public DataType type;<br />
}<br />
<br />
<br />
struct Command<br />
{<br />
public Header packetHeader;<br />
public Command command;<br />
}<br />
<br />
<br />
<br />
<br />
Command myCommand = new Command();<br />
<br />
Socket.send(&myCommand, sizeof(blabla));
|
|
|
|
|
If you can't use serialization, your best option is to use unsafe. You can do:
byte[] buffer = new byte[sizeof(Command)];
fixed (Byte* pBuffer = buffer)
{
*((Command*) pBuffer) = command;
}
socket.Send(buffer, buffer.Lenth);
|
|
|
|
|
thanks a lot for this idea, should look like below about?
The problem is the compiler states:
The type or namespace name 'Command' could not be found
(are you missing a using directive or an assembly reference?)
Sorry, can you bring me in the right direction please?
And what means fixed?
thanks again,
stonee
public unsafe override bool Send(void* Command, long lSize) <br />
<br />
{ <br />
System.Console.WriteLine("Send() called!");<br />
<br />
try<br />
<br />
{<br />
byte[] buffer = new byte[lSize];<br />
fixed (Byte* pBuffer = buffer)<br />
{<br />
*((Command*) pBuffer) = Command;<br />
}<br />
m_socket.Send(buffer, buffer.Length,SocketFlags.None);<br />
<br />
return true; <br />
<br />
<br />
}<br />
catch(SocketException se)<br />
{<br />
System.Console.WriteLine(se.Message); <br />
return false; <br />
}
|
|
|
|
|