|
Hi All,
I assigned a function key (F9) to a button (btnShowMessage) in a form. There are a couple of buttons and listbox inside the form as well. When I load the form and press F9, there is no response, until I set the focus on the button (bthShowMessage).
Now, the problem is that I have 10 buttons to be assigned as F1 to F10 in the form. I do not think I could set focus for every single button. So, how should I go about?
What I would like to have is, when I load a form and press a function key (F1 - F10), a related method should be able to called.
Thank you in advance.
|
|
|
|
|
Hi,
here are some facts about Windows Forms:
1. a Control only listens to keyboard and mouse when it has focus, which only one can have at a time;
2. the easiest way to get special keys or key combinations to do something is by adding a main menu, and menu items with their shortcut set to the keys of interest; then the same event handler can be used for the menu item as well as for other things, such as a Button;
3. a Form can "preview" keyboard input, i.e. get key events although one of its children has focus; so barring the menu idea, you would need a KeyDown and/or KeyPress handler for the Form.
4. none of the above will work if the Form isn't active.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
I got it. Thank you. I set the keyUp event for the button, but not the form. Thank you very much for the advice.
|
|
|
|
|
I guess we need to see your code to understand what's going on. If you're assigning a key in the settings, then I have no idea how that would work, but if you catch a keypress event on the form, that should always work.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
this.btnShowMessage.KeyUp+= new System.Windows.Forms.KeyEventHandler(this.formClose);
private void formClose(object sender, KeyEventArgs e) //Keyup Event
{
if (e.KeyCode == Keys.F9 )
{
this.btnShowMessage_Click(this, EventArgs.Empty);
}
else
MessageBox.Show("No Function");
}
|
|
|
|
|
Why is your event called formClose ?
Well, your code does exactly what you told it to. You're handling events on the button, therefore only if the button has focus, will it fire. Add this event to the form instead.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Thank you too, Graus.
|
|
|
|
|
I have a struct that is pinned for use with a few unmanaged functions. One of the fields in the structure is a byte[]. Should I also pin this, or is the pinning of the structure enough?
(Luc - help! I'll get my head around this PInvoke stuff eventually )
Dave
"My code works, but I don't understand why!" - DaveyM69 (Me) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
You rang milord?
what is the other side? a win32 function, your C/C++ function, someone's C/C++ function?
is it using a native struct containing a fixed-size array? if so, you need [MarshalAs(UnmanagedType.ByValArray, SizeConst=7777)] , which means the array is smashed into the struct, hence there is only one object that should not move. Now be careful with padding; either side may decide to inject dummy bytes for better alignment. Managed side can avoid that by using both [StructLayout(LayoutKind.Sequential)] overall and [FieldOffset(8888)] for each member. However you then are responsible for providing correct offsets; a technique I tend to use is add dummy bytes myself (both ends), rather than rely on attributes/directives. Basic rule is keep every member "naturally aligned", i.e. if size=N, then offset best is a multiple of N.
If it really is a byte pointer, you have two objects that shouldn't move. And the struct then contains an IntPtr which takes 4B or 8B (Win32/Win64). For a small array, Marshal.Copy may help out.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Yes Jeeves!
Sorry, my mistake - this stuff fries my few remaining brain cells. It's actually a pointer to a byte array so obviously I'll have to pin it or something similar .
It could be up to a max of 64KB. The byte array is actually a translation of a struct with a variable length array that I have already padded as the API says it must be word aligned. For an array of this size would I be better pinning or copying in your opinion?
Dave
"My code works, but I don't understand why!" - DaveyM69 (Me) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Copying is against my religion, I always pin (unless it is an array smashed in a struct), with GCHandle most often. I would never copy 64KB if I don't have to, it takes cycles and bytes, and it trashes the caches.
DaveyM69 wrote: I have already padded as the API says it must be word aligned
I would find it very hard to get an object that isn't word aligned. AFAIK every value type (with a size of at least 4B) and every object (an array is an object!) is aligned to at least 4B, probably 8 or more. Same is true on all C systems I've encountered (e.g. using malloc). And the only way you could accidentally loose alignment is by prefixing something smaller inside a struct.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Pinning it is then - cheers again. If ever I'm in Belgium I must buy you several s
I meant the byte array inside the struct (has to be padded to ensure everything else in there is aligned) that I'm then converting to a byte array, who's pointer I need to pass to the main structure which is pinned - you can see why this melted my brain!
Dave
"My code works, but I don't understand why!" - DaveyM69 (Me) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
As it smells like a MIDI app, I can only hope CP will find a way to let us enjoy your compositions someday...
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Luc Pattyn wrote: As it smells like a MIDI app
Good guess. I had to suspend work on what I was doing a few months ago due to other pressures. I've recently picked it up again and trying to clean up some of the parts I wasn't totally happy with before. The wrapper is pretty much complete and tested - next step will be to write a few simple demos to show it off. Article pending ... I'll include a few samples of my musical work in there too
Dave
"My code works, but I don't understand why!" - DaveyM69 (Me) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
DaveyM69 wrote: Article pending ...
will "My code works, but I don't understand why!" apply?
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
I don't know if you caught that thread or not - it's here[^] if not.
It is part of this same project. It was actually a bit of a repost with a solution I discovered accidentally to a problem that I never fully resolved before. If you can shed any light on this and/or would like to see the real code let me know. I'm still not 100% sure why it works amd I may be relying on undocumented behaviour by the GC which worries me as it may change and break this safety net. On the other hand, if it works that way by design then cool
Dave
"My code works, but I don't understand why!" - DaveyM69 (Me) BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi Dave,
yes I had seen part of that thread at that time. I'll reply to it now.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
|
|
|
|
|
Hey there,
I have a c# project and I'm working in VS 2005 and .NET V2 SP2. I also have a web service built in Java that runs on jBoss 4.2.3. In this web service is a method that originally took a number of parameters that kept growing in size over time. Some of these parameters were of type long[].
The web service method was recently changed so that instead of having several parameters it had a single parameter which was an object that contains all parameters. This made it so that whenever we added new functionality to the method it's stub would not change.
Now when I update my reference in the c# project I see the single object but contained within that the long[] parameters are now long?[], which are nullable of course. I don't want this as it could break the existing code if a comsuming app were to pass in an array with null elements.
Can anyone please explain why it is doing this and if there is way of preventing this?
Thanks!
Brian
|
|
|
|
|
I'm working on a solution that pulls in components from different projects. I have my Data Access class, some custom controls I wrote, my App Security class, and the WinForm components. All of these access data, and so far, all of them use their own connections.
I was thinking about creating a ref to my data access class in each project that needs data. I would also include a Connection property that, if populated with an open connection, would be used so that in most cases, I'd only ever have one connection to the data source. If the connection property has not been populated, then the class would establish it's own connection using the data access class.
Any thoughts on this?
Everything makes sense in someone's mind
|
|
|
|
|
The connections should only be created in the data access object(s). There shouldn't be any reason for the other components to create a connection and pass it to the data access class. Doing so runs the risk having the connection opened far longer than necessary, or having it closed by the time the data access needs it, plus other design and maintenance concerns.
only two letters away from being an asset
|
|
|
|
|
Yes, but wouldn't that result in multiple connections open? I'v always heard that an app should only have one, maybe two, open connections.
Everything makes sense in someone's mind
|
|
|
|
|
Kevin Marois wrote: but wouldn't that result in multiple connections open?
Only if you allow it to. You could make the data access component a singleton. However, the chance of having multiple connections open is minimal if you fallow the rule that the connection should be opened as late as possible and closed as soon as possible.
only two letters away from being an asset
|
|
|
|
|
Hi,
I want to use a DetailsView to insert a record into an empty list.
I have a DetailsView in my page source (DefaultMode set to insert), and the datasource in set in the codebehind.
myObject = new CustomObject();
if (!IsPostBack)
{
myDetailsView.DataSource = new List<customobject>() {myObject};
myDetailsView.DataBind();
}
...Then I run the website and type some text into one of the bound fields,
then click a button on the form...
protected void Button_Click(object sender, EventArgs e)
{
string s = myObject.myProperty;
}
But the value of the object is NULL.
Even myDetailsView.DataSource returns an empty list
what am I doing wrong??? I just want to retrieve the values types into the DetailsView
Thanks
James
|
|
|
|
|
jehazlam wrote: what am I doing wrong???
The first thing is not asking this in the ASP.NET forum.
jehazlam wrote: But the value of the object is NULL.
Your object is null because you have not persisted it in any way.
jehazlam wrote:
Even myDetailsView.DataSource returns an empty list
if (!IsPostBack)
{
myDetailsView.DataSource = new List() {myObject};
myDetailsView.DataBind();
}
Of course its empty, your're not re-binding it during your postback
Mastering ASP.NET DataBinding[^]
only two letters away from being an asset
|
|
|
|
|
I am running a SQL statement in C#, where I am using the WHERE clause to limit a query to a date.
Here's the SQL:
DbCommand.CommandText = "SELECT SUM-DATE, SUM-STORE, SUM(SUM-RETURNS) FROM SUMMARY WHERE SUM-DATE = '" + dtProcDate + "' GROUP BY SUM-STORE";
The dtProcDate is assigned here:
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
{
dtProcDate = DateTime.Now.AddDays(-2);
}
else
{
dtProcDate = DateTime.Now.AddDays(-1);
}
When I query just the date, I get the date in the MM/DD/YYYY HH:MM:SS AM/PM format, which is what dtProcDate shows as it's format also.
Here is the Error:
ERROR [HY000] Data Type Mismatch in WHERE clause, Value '10/24/2009 2:22:39 PM'.
What am I missing here?
Jude
|
|
|
|