|
Thanks - I knew it should be easy. I tried a couple of variations except "sender as" so I was at least on the right track.
I have several datagridviews that display the status of daily booking periods for tennis courts. Whenever a user clicks on one of the cells, the same action is taken no matter which datagridview is clicked with one exception - the name of the underlying datatable. I've set the variable part of each of the datatables in the datagridviews tag and hence the need to read that value.
I did of course do it the other way and have individual events call the required code and pass the hardcoded datatable value to it but that means duplication and I am trying to learn as I go. Obviously I'm trying to code efficiently as well as successfully
If there is something else to learn from this then I am all ears
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: I tried a couple of variations except "sender as" so I was at least on the right track.
Yes, you were definately on the right track. While this isn't a design pattern in the proper sense of the word, it is fairly common. The nice thing with this cast is that if the cast fails it doesn't throw an exception...the variable is null so you can test for that condition before doing more work.
Glen Harvy wrote: I've set the variable part of each of the datatables in the datagridviews tag and hence the need to read that value.
Given the scenario you are working with, this is probably the best option. I believe you can also get a reference to the underlying datasource from the EventArgs, so you could probably also look at the DataTable name.
Glen Harvy wrote: Obviously I'm trying to code efficiently as well as successfully
That is always the best way to do it, but it seems they don't always go hand-in-hand. Code Project is probably one of the best places to learn as you get a wide variety of experience and solutions, plus the benefit of the articles. Sometimes we can be a bit grouchy, but that's usually because we haven't had morning coffee (or tea) yet.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Thanks for your help on this - much appreciated.
Regards to the cats
Glen Harvy
|
|
|
|
|
Hello everyone,
I have some Buttons in my Windows Applicaton which I would like to add some sort of information for them. I was wondering if it is possible to have a text to be come visiable next to the courser once the courser is hovering over the button. Similar to the one you can have on the toolstrip items?
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
I'm presuming that you mean you would like a tooltip when you hover over a button or similar control.
Drag a ToolTip control onto the Windows Form you want, then add some code After the InitializeComponent call like this
MyToolTip.SetToolTip(this.mycontrol, "Some Text To Show When Mouse Is Over");
Where MyToolTip is the name given to the tooltip control and mycontrol is the name of the control you want to add a tooltip to.
Then when you hover over the control your tooltip should be shown,
Mark.
|
|
|
|
|
Hello Mark,
Thanx for the info. I have just tried the ToolTip and this is exactly what I was looking for.
Thank you very much and have a great weekend, mate.
Khoramdin
|
|
|
|
|
hi
i want to start an application with hidden window, but how to do ?
|
|
|
|
|
|
I have a c library function that returns a structure. How can i get that structure correctly in c#?
I made an exact copy of the structure in c#, added the attribute [StructLayout(LayoutKind.Sequential)]. What do i then do?
Do I have an IntPtr return type in my dll import function (so i can marshall it) or do I just give the dll import function the structure as return type?
like this
<br />
dllimport...<br />
internal static extern mystruct Function();
or
<br />
dllimport...<br />
internal static extern IntPtr Function(); and then marshall it?
|
|
|
|
|
I think it depends on whether the C function is returning the structure by value or is returning a pointer to a structure. Can you post the C function prototype?
|
|
|
|
|
FSLAPI FSL_EAX_LISTENER_PROPERTIES FSLAPIENTRY fslCreateListenerEnvironment(const char* strData, unsigned int size);
FSL_EAX_LISTENER_PROPERTIES is the structure.
|
|
|
|
|
Ok, well it looks like the structure it returned by value. However, to be on the safe side, if you can find the structure's declaration in its header file, that will tell you for sure. "FSL_EAX_LISTENER_PROPERTIES" looks like a typedef. Sometimes C programmers will typedef a structure pointer. That can disguise whether it is a structure or a pointer to a structure. If you can find its declaration in the header file and post it here, we should be able to nail down for sure whether the function is returning a structure by value or a pointer to a structure.
|
|
|
|
|
<br />
struct FSL_EAX_LISTENER_PROPERTIES<br />
{<br />
long lRoom; <br />
long lRoomHF; <br />
float flRoomRolloffFactor; <br />
float flDecayTime; <br />
float flDecayHFRatio; <br />
long lReflections; <br />
float flReflectionsDelay; <br />
long lReverb; <br />
float flReverbDelay; <br />
unsigned long dwEnvironment; <br />
float flEnvironmentSize; <br />
float flEnvironmentDiffusion; <br />
float flAirAbsorptionHF; <br />
unsigned long dwFlags; <br />
};<br />
Thats the structure which was in the header file.
|
|
|
|
|
Ok, no typedefs to pointers there. Here's what I'd use:
[ DllImport( "Kernel32.dll", CharSet=CharSet.Auto )]
public static extern MyStruct fslCreateListenerEnvironment(StringBuilder stringData, UInt32 size);
Where "MyStruct" is the interopted version of FSL_EAX_LISTENER_PROPERTIES.
Unfortunately, I've not been able to find an example on MSDN for marshalling a function that returns a structure by value. So I can't 100% guarantee that this is the correct approach, but I'm relatively certain.
|
|
|
|
|
|
Hi,
I have to open the outlook application.
olApp = new Outlook.ApplicationClass();
But if the outlook is already running, what should I do?
I have the following code to find out if the outlook applicatin is already running. Once I got the processes of current Outlook, how should I assign to olApp??
bool found=false;
Process [] processes = Process.GetProcessesByName("OUTLOOK");
if (processes.Length >= 1)
{
found=true;
}
Thanks.
|
|
|
|
|
I need a variable whose value is known in all program code.
No it's not a constant, it's a variable whose value changes, but i need to access to its value in other program parts.
If i create a class with a property, everytime i change module i need to create a class reference again, and the property is reset to its default value, i need to keep its value all along the program life, this is simple in all other programing languages, but in C# i didn't manage to do this.
How do i do this, and where to do this?
Thank you,
Joaquim
|
|
|
|
|
have you tried creating a public sealed static class with a public static property to get and set the value for that variable. if the class is sealed and static then you don't have to create a reference, all you need is classname.propertyname ....
-----
|
|
|
|
|
Thank you SABhatti, it worked.
But wowww... alone i wouldn't go there.
I did not see any reference to 'public sealed static classes' (uufff..., i'm tired) anywhere in the documentation.
In Visual FoxPro i would do: 'addproperty(_application,PropertyName)', anywhere in the code, and voilá i would have a property that i could access at any part of the code simply by writing:
_application.Propertyname = "value"
or
this.text1.value = _application.Propertyname
Thank you,
Joaquim
|
|
|
|
|
Global variables are generally an indication of poor design. C# therefore does not support them. You can however simiulate them through other means such as the one you have now discovered.
|
|
|
|
|
No Angus, Global variables, or in this case properties, are a means to an end, bad use of global variables are indication of poor design.
And as we now see: C# support them
Joaquim
|
|
|
|
|
joaquimc wrote: Global variables, or in this case properties, are a means to an end, bad use of global variables are indication of poor design.
I didn't say all uses were bad. I said "generally" they were bad. Every time I have the urge to use a global variable I sit and think if it is really really necessary first. Most of the time I come up with a better solution that doesn't involve global variables. These days I rarely think of global variables at all.
Next, properties do not have values. In C# properties are methods dressed up in syntactic sugar. Any value they appear to have is a result of the value that is returned from the underlying method.
joaquimc wrote: And as we now see: C# support them
It doesn't. It is just sugar dressing. It just appears to support them in a contrived way.
|
|
|
|
|
You can't really say that global variables or result of bad design, instead using them badly is bad. But in some cases you definitely need them depending on the requirement..
-----
|
|
|
|
|
Sure you can. Wether or not it's valid to say that is up for debate.
But seriously, I've never seen a situation that warranted globals. Point is there are better ways to handle situations where you want to use them.
|
|
|
|
|
SABhatti wrote: You can't really say that global variables or [are]result of bad design
Yes. I can say that. 99.99% of the time global variables are the result of bad design.
In any situation that I've used them, I've come back to the code sometime later and think why I made that poor descision. Often, rereading the code I'll come up with a better solution.
SABhatti wrote: But in some cases you definitely need them depending on the requirement..
Such as?
|
|
|
|