|
This code does not work because object does not have a tag property. First cast it to Panel ( which is what it will be ). Of course, you should replace your delegate with a proper OnClick handler ( I'm sure that's not the right syntax, don't you need an eventargs, and you sure have the syntax for adding an event handler all wrong. ).
'The above example does not work' is not very helpful - in future try telling us why. I doubt this would compile, so report the compiler errors. If it crashes, report which line crashes and what the exception is. Working that stuff out might actually help you to fix the problem yourself ( not that we mind helping, but surely you want to learn how to do it yourself ? )
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Sorry, i've only been using C# for about a week and i've still got a lot to teach myself. However your comments helped and it now makes a lot more sense to me
Thanks for your help!
|
|
|
|
|
<br />
textBox1.Text = Convert.ToString((int)((Panel)sender.Tag));<br />
|
|
|
|
|
Let see this case:
I need to show the aform, and write code
aform f = new aform(someparameter);
f.Show();
and in the form's code. I use "someparameter" to determine if I should still show the form or Close it.
(this is done automatically, in FormLoad, constructor... etc; without anymore interaction from user (like event))
How this can be done?
|
|
|
|
|
this.Close();
It's the wrong model though - you should work out if you want to show it before you do. Anyone looking at this code is right to assume the form will be shown.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
If at all, you want to do -
Override the constructor with parameters public Form3(string args)
{ if args=="dontshow!" this.Close() // and dispose the object and come out }
However, the best and easier way to handle is to do it before hand since you have the parameters in hand before!
|
|
|
|
|
Why do people keep answering questions by posting under someone elses answer ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
Christian Graus wrote:
Why do people keep answering questions by posting under someone elses answer ?
I don't know why, but apparently you seems to be getting a couple of them.
<font=arial>Weiye Chen
Life is hard, yet we are made of flesh...
|
|
|
|
|
Thats because we had answered at the same time! believe from various locations. Take care, sorry for the trouble guys for this thread!
|
|
|
|
|
No, it's not. You clicked on Reply while looking at Christian's answer! Your post is 20 minutes later this his.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello,
I am trying to call c++ dll function from C#.
Could anybody help me?
Here is the C++ function declaration.
long WINAPI GetPredictValues (double& m_vo2Max, double& m_vo2_kg_max, double& m_o2PulseMax)
And this is my declaration in C#.
[DllImport("c:\\ProgramFiles\\VacuMed\\turboFit\\tf32mmf.dll",SetLastError=true)]
public static extern int GetPredictValues(out double vo2Max,out double vo2_kg_Max,out double o2PulseMax);
And I get an error on the line below.
double vo2max,vo2kgmax,o2Pulsemax;
GetPredictValues( out vo2max, out vo2kgmax,out o2Pulsemax);An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object
Thank you for any help.
Sincerely,
|
|
|
|
|
On what line does the exception occur? I don't think the exception message came up on the line that you posted...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I'm sorry I had forgotten to write the line that the error message occur.
The message occur on the line below:
double vo2max,vo2kgmax,o2Pulsemax;
GetPredictValues( out vo2max, out vo2kgmax,out o2Pulsemax);
Thank you for your help
Hai
|
|
|
|
|
anyone know of a tool that can convert c/c++ header to c#, i need to
convert a massive struct which is about 150 lines that has nested
structs and unions.
heres a sample of the struct.
*note* chopped off bits and pieces in the middile.
struct R_OMNI_LINK_MESSAGE {
unsigned char MessageLength;
union {
unsigned char Data[255];
struct {
unsigned char MessageType;
union {
struct {
unsigned char ItemType8;
unsigned char ItemName8[16];
};
struct {
unsigned char TimeDateValidFlag;
unsigned char AreaSecurityMode[8]; // index 0-7
struct {
unsigned char BatteryReading;
} ExpansionEnclosure[8]; // index 0-7
};
struct /* olmMESSAGE_STATUS */ {
unsigned char Data;
} MessageStatus[33];
struct {
unsigned char AreaNumber; // (1-8)
};
struct {
unsigned char UserCodeNumber;
};
}; // union
}; // struct
}; // union
};
Kids don't try this at home!
|
|
|
|
|
There are no unions in C#.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
ok, how do i convert that to c#?
Kids don't try this at home!
|
|
|
|
|
I'm not sure that you can, in a way I'd consider production safe. Why do you need to ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
doesnt matter if its safe or not...i just need it to work with c#, i have it partially converted but its giving me errors about [FieldOffset()] collisions and marshalling errors.
i need to convert this to c# so i can communicate with a piece of hardware, since i dont know enough c++, c# is the only way to go ;(
Kids don't try this at home!
|
|
|
|
|
savage_ wrote:
[FieldOffset()] collisions and marshalling errors.
Then you've found the methods I could suggest, I dunno how else you could do it. Excepting that perhaps you could store an object for each union, and then create objects that represent the possibilities that go inside. Honestly, I reckon it would be easier to learn a little C++.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
There is no tool that will do this for you. You'll have to code the thing by hand.
Since you haven't posted your C# code, it's impossible to tell you what you did wrong to generate that error. But, I'll take a guess at it.
Heath posted an example of how to simulate unions sometime last year. I grabbed this little piece of code from my archives:
[StructLayout(LayoutKind.Explicit)]
public struct INPUT{
[FieldOffset(0), MarshalAs(UnmanagedType.U4)] public int type;
[FieldOffset(4)] public MOUSEINPUT mi;
[FieldOffset(4)] public KEYBOARDINPUT ki;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSEINPUT{
public int dx;
public int dy;
[MarshalAs(UnmanagedType.U4)] public int mouseData;
[MarshalAs(UnmanagedType.U4)] public int dwFlags;
[MarshalAs(UnmanagedType.U4)] public int time;
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBOARDINPUT{
public short wVk;
public short wScan;
[MarshalAs(UnmanagedType.U4)] public int dwFlags;
[MarshalAs(UnmanagedType.U4)] public int time;
[MarshalAs(UnmanagedType.SysUInt)] public IntPtr dwExtraInfo;
}
The thing to remember is that your inital struct must be LayoutKind.Explicit . Also note the FieldOffsets! If you use LayoutKind.Sequential and try using overlapping FieldOffsets, you'll get the collision exception.
Also, you're going to have to learn a little C++ to do this anyway. Since C++ types and .NET types of the same names don't exactly match in size or function.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
what i dont understand is the offsets. heres where im getting stuck:
struct R_OMNI_LINK_MESSAGE {
unsigned char messagelength // offset 0
unionX {
unsigned char data[255] // offset 4?
struct { // this struct has no name?
unsigned char messagetype // since its inside union this ones offset is 4?
unionY {
struct { // NAME_DATA
unsigned char itemtype // offset ??
} // struct name_data
} // unionY
} // struct
} // unionX
} // r_omni_link_message
this is my c# code for the above bit of code
[StructLayout(LayoutKind.Explicit)]
public struct R_OMNI_LINK_MESSAGE
{
[FieldOffset(0)] public int MessageLength;
[FieldOffset(4)][MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)] public string Data;
[FieldOffset(??)] public int MessageType;
//olmNAME_DATA (8 bit)
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr)] public string ItemType8;
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr)] public string ItemNumber8;
[FieldOffset(??)][MarshalAs(UnmanagedType.LPStr, SizeConst=16)] public string ItemName8;
?? i dont know the offsets
Kids don't try this at home!
|
|
|
|
|
The field offsets are just how many bytes into the structure that field starts. What these values are depends on the original structure. Usually, the field offsets put the field immediately after the last one, accounting for the size (in bytes) of the previous fields.
In your example, the first field is an unsigned char, called messagelength, at, obviously, offset 0 from the start of the structure. Since the C++ char type is usually a single byte, the next field would probably be at offset 1, accounting for the size of the fields before it.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
i am enhancing a image button.... i found this web page but dun reali understand.... can someone teach me how to use it?
http://www.codeproject.com/script/comments/forums.asp?forumid=1649
|
|
|
|
|
You've given a link back to this forum. What are you trying to do ?
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
http://www.codeproject.com/cs/miscctrl/XCtrls.asp
|
|
|
|