|
Thanks Heath for you earlier reply. I could get what I wanted done.
But I have another problem and thought maybe you could shed some light.
The grid has 2 columns and 5 rows out of which one column is editable. I'm implementing an observer pattern where the grid is one of the views. When the user changes the text in the grid column I need to update the model. For this I register for the LostFocus event of the TextBox hosted in the DataGridTextBoxColumn. This is done in my derived class.
Everything is allright if the user enters text and changes focus using the mouse. But using the arrow keys, the focus does change to the next grid cell, but there is no LostFocus event generated.
« Superman »
|
|
|
|
|
When deriving your own classes, you can fire all the events you want. The Commit method is still called on the derived class so add an event to your class and fire it inside the Commit implementation. It's your class - you have control of what happens.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
I am using drag and drop in my app. I have this code for the DragEnter event:
<br />
private void tabPage1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)<br />
{<br />
if( e.Data.GetDataPresent(DataFormats.FileDrop, false) == true )<br />
e.Effect = DragDropEffects.All;<br />
else<br />
e.Effect = DragDropEffects.None;<br />
}<br />
Now, how can I tell if a file or folder was dropped? What I am using now in DragDrop is:
<br />
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);<br />
<br />
foreach( string file in files )<br />
{<br />
DirectoryInfo di = new DirectoryInfo(file);<br />
if (di.Exists)<br />
MessageBox.Show("DIR: "+file);<br />
else<br />
MessageBox.Show("FILE: "+file);<br />
}<br />
Is this the proper way to do the check?
Thanx for the help,
-Flack
|
|
|
|
|
You handle the DragDrop event and use the DragEventArgs.Data.GetDate to get a string[] array (in your case). Then just copy the files or whatever.
In your DragEnter event handler, don't use DragDropEffects.All unless you intend to support all effects (some of which aren't often relevant). Just set the ones you actually support, like Move or Copy , which are the two most common ones.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanx for the reply.
After I do:
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
in DragDrop, how can I differentiate between strings that represent file names and strings that represent folder names? Is the way I did it in my original post the correct way?
Thanx
-Flack
|
|
|
|
|
Flack wrote:
Is the way I did it in my original post the correct way?
Yes, that is one way of doing it. It works, doesn't it?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I find it much easier if a class is going to have an empty constructor to make the class's properties and methods static. But I haven't read or seen anything on the "ramifications" of using static a bunch. One one hand it seems as though it would help the system's performance since static methods and properties are loaded on system startup - but does this dramatically affect the system's "startup" time; and memory usage - or enough to worry about it anyway? And is calling static methods etc. offer any significant performance increase over intializing a class with an empty constructor and then utilizing its methods etc.? Has anyone discovered some good "rules of thumb" for determining when to make a method or property static? I typically create static sql connection strings, color properties, etc... based on the above questions, is this a good idea or not?
|
|
|
|
|
If a method does not rely on state within the class ( i.e. member variables of a specific instance of a class ), then it should be static, or possibly part of a fascade class.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
As Christian implied, you should use instance members when the class requires state for each instantiation. Statics are also executed synchronously within a single AppDomain so forget multi-threaded applications.
Finally, this makes for a terrible object-oriented design and throws polymorphism out the window since you can neither mark a static member as abstract nor override an inheritted static member. You're basically turning the .NET Framework into a procedural construct. If that's what you're after, you don't want object-oriented runtimes like the .NET CLR or Java Runtime Environment.
Static members are best reserved for class factories, non-instance operations (like binary operators, non-class specific methods like Control.MousePosition , and the like). Everything else should be an instance member. In a good OO design, you'll end up using mostly instance members.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello,
I have a form that is shaped with a GraphicsPath object. After setting the form's region, my form's border looks bad (jagged edges). I would like a nice solid border around the form. Is there something I can do?
I have tried using the TransparencyKey property to fill my form with a color. This works fine, except it doesn't work at 32 bits (a bug in TransparencyKey) and I can't use AntiAliasing for my border.
Is there any way I can get smooth, soft edges on my custom shaped form using a GraphicsPath?
Thanx for the help,
-Flack
|
|
|
|
|
use a bitmap as a region, graphics software can draw stuff better than we can do in code usually
|
|
|
|
|
What's your target platform? If it's Windows 2000 and later, you can use layered windows (of which TransparencyKey partly encapsulates, and is - as you've experienced/read - buggy). You can perform the layered windows yourself, though, and get a nice smooth edge like you see with Windows Media Player 9 or the splash screens for newer Adobe applications.
See the SetLayeredWindowAttributes native API documentation in the Platform SDK. You can P/Invoke this. Also see the "Using Layered Windows" link at the bottom. Also, override CreateParams , get base.CreateParams , and OR the WS_EX_LAYERED (0x00080000) extended style, returning the modified CreateParams from your override. You can achieve all this through managed code, but there's a bug in there somewhere and I honestly can't remember where. I do know that doing the work mentioned above has worked for me in .NET applications previously.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've got an object that i am writing a re-sizing method for. The resize begins upon mouse down when over a certain area of a component. This works fine, i can create an increasing side, but only on a per click basis.
The element that activates the re-size is part of the component, and not tied to the parent form. What I need to do is detect the movement of the mouse as an event, either inwards or out of the component, and use the difference in the X coordinates for horizontal re-sizing, and the difference in the Y coordinates for vertical re-sizing.
I thought i had this figured, but guess I was wrong.
Cata
|
|
|
|
|
Without any code example, we can only guess at what you doing wrong, but...
Are you doing your resize in the MouseMove handler or MouseDown. Use MouseMove and check for the position of the mouse, then if your in the resize area, check the status of the MouseButtons in the MouseEventsArgs, then you can use that to determine if you should resize or not.
RageInTheMachine9532
|
|
|
|
|
...or like I said originally today, set a flat in the MouseDown handler and reset it in the MouseUp handler. Dave's method would work, but you end up having to check the same condition (which makes several calls) with each execution of the handler, which is slightly less efficient than using a simple flag.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi.
How could I implement a pointer to an array of bytes, (bytes[]) in C#?
Thank you.
|
|
|
|
|
You can use something like
unsafe
{
byte* bytes = stackalloc byte[50];
}
The stackalloc keyword puts the bytes on the stack as opposed to the heap, and therefore not subject to garbage collection. If you don't use stackalloc, be sure to use the fixed keyword to pin the bytes to the heap (so that the GC won't clean them up).
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
Yeah.
But how can asign to this pointer an array like byte[].
Simply byte= myarray[0]; ?
Thank.
|
|
|
|
|
I believe you have to assign it using the fixed keyword.
byte[] managedByteArr = new byte[50];
managedByteArr[0] = 200;
fixed (byte* pBytes = managedByteArr)
{
Console.WriteLine(pBytes[0]);
}
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
But when the pointer bytes is a member variable of the class throw a compiler error: "Identifier expected" here:
fixed(pBytes=arraymanaged) <-here
{
.....
}
The pointer must be a member variable, that's what I need, so, I can get data out from a thread that call some function that return an array of bytes, (byte[]).
Do you understand what I need?
Thank you.
|
|
|
|
|
This is the code that I wrote:
using System;
unsafe class Class1
{
public byte* pBytes;
unsafe static void Main()
{
byte[] mArr= new byte[]{6,9,11,25};
fixed (pBytes = mArr)
{
for(int i= 0; i<4;i++)
Console.WriteLine(pBytes[i]);
}
}
}
|
|
|
|
|
Not that your example is any indication of what you are actually doing, but keep in mind that C# does turn off bounds checking for cases like
for(int i=0; i
|
|
|
|
|
Why do you need to use an unsafe context for this? mArr is your "pointer" to the array of bytes, except that in the managed code world it's called references, which can be moved around by the garbage collector (GC) in the heap whenever it needs to optimize memory (which is why you must use the fixed keyword, or the GCHandle class to lock the object on the heap for that context). Arrays are also reference types, so you don't need to use ref or out with them except in some uncommon cases when you need to declare an undimensioned array and have a method (not "function") return it.
The code can simply be:
byte[] mArr = new byte[] {6, 9, 11, 25};
for (int i=0; i<mArr.Length; i++)
Console.WriteLine("0x{0:x2}", mArr[i]); Much easier, and correct (since unsafe contexts cause security problems in non-local deployments and are not completely managed by the CLR).
Even if this is a field in your class, it doesn't matter:
public class MyClass
{
public byte[] bytes;
public MyClass()
{
bytes = new byte[] {6, 9, 11, 25};
}
} Your declaration of the byte[] array doesn't dimension it; it only declares an array of a certain type. In unmanaged code, the same is true, since a char* points to the first element (the pointer to the array itself) of a char[] , for example.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, that's ok.
But I still having troubles to fill a member field, (array of bytes), with the data returned by a function called from a thread.
Example:
<br />
class myClass<br />
{<br />
public Array m_arr;
<br />
public void MyThreadMethod()<br />
{<br />
object ret; <br />
ret=AFunction(Aparam);
Array ar = (Array) ret;
m_arr=ar;
}
I can't fill my member variable with data of the thread.
Regards.
|
|
|
|
|
You typically don't declare an Array , but an array of something like byte[] , which I mentioned before. This does not have dimension.
Also, why not return the actual array from AFunction ? All this casting is completely unnecessary. So long as this is all .NET code, you can pass and return anything, but you must understand the difference between value and reference types (you're dealing with reference types here). If AFunction is a P/Invoke method, then things are a bit different. Is this the case?
If it's all coded for .NET (no P/Invoke or COM interop), you could simply use m_arr = AFunction(Aparam); it the method was declared appropriately. Return an object or taking an object as a param is really only a good idea when you don't know what to expect (or in some polymorphic designs when derivative classes will accept different types).
The only times when assign data from a thread is a problem is when 1) you don't lock your resource and multiple threads write to it/assign at the same time, or 2) when you try to update the UI (like setting the Text property of a control or adding a ListViewItem to a ListView ). For this latter case, make use of the Control.InvokeRequired property (to determine if the following method should be used) and the Control.Invoke method.
In any case, this should definitely work if its purely managed code. I do it all the time. Please provide more information (and accurate to your actual problem, instead of an example that might not be close enough to your real problem to solve it) about how this method is threaded and what AFunction is (or whatever it is in your code).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|