|
when i align the tab control to left or write the text on the tabs doesnt apear at run time ? (the tab text is empty)
|
|
|
|
|
Hi,
I am working on a windows application where the user should be able to move,resize and drag and drop a control (a picture box in my case). I have both MouseDown and Click events for this control. But I cant really use the Click event because everytime I press the mouse button on the control the MouseDown event fires up. Is there a way that i can differentiate between MouseDown and Single Click. Please let me know.
Thanks
smartyosu
|
|
|
|
|
Use a timer. The actual implementation should be placed in the timer event handler (just depends on which Timer class you use; for example, the System.Windows.Forms.Timer.Tick event). The MouseDown event handler starts the timer. If the timer elapses, your original MouseDown implementation is executed. The Click event handler should stop the timer. The timer duration should be a single occurance (non-repeating) and should be at least SystemInformation.DoubleClickTime (an extra 10 milliseconds should be plenty in case of lag).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
How can I make an event handler for every time a certain method "Send" is called?
Or actually event better. An event handler for when a new item is added to a listbox.
/\ |_ E X E GG
|
|
|
|
|
The event pattern in .NET is quite easy. The correct approach is define a delegate (using EventHandler or some EventHandler -like syntax (like delegate void MyEventHandler(object sender, MyEventArgs e) ). Then you declare your event on your class, as well as define an OnEventName method.
The naming convention follows: for an event name "MyEvent", you define a delegate and members like so:
delegate: MyEventHandler
arguments: MyEventArgs
event name: MyEvent
handler: OnMyEvent
So an example class would look like this:
public delegate void SentHandler(object sender, SentArgs e);
public class Example
{
public event SentEventHandler Sent;
protected virtual void OnSent(SentEventArgs e)
{
if (Sent != null)
Sent(this, e);
}
public void Send(string message)
{
OnSent(new SentEventArgs(message));
}
}
public class SentEventArgs : EventArgs
{
string message;
public SentEventArgs(string message)
{
this.message = message;
}
public string Message
{
get { return message; }
}
} You define a protected virtual member named OnEventName so that derivative classes can override it (and they should call base.OnEventName ) without handling the event, which require far more IL statements and doesn't allow for as much handling (like the ability to not have the base handler called, which is actually what raises the event).
This is also documented in the .NET Framework SDK.
What you want to do for a ListBox isn't at all easy. While you can extend ListBox and ListBox.ObjectCollection to override ListBox.CreateItemCollection and return your own ListBox.ObjectCollection derivative, you can't override Add , Remove , etc. While you could hide them using new , your definition won't be called because the ListBox internally refers to the ListBox.ObjectCollection class.
With member hiding, the call is not virtual. This means that in order for your implementation to be called, your class must be the class that's referred to. The following example should demonstrate better:
public class Test
{
static void Main()
{
Test t = new Test();
t.A();
t.B();
t = new Test2();
t.A();
t.B();
((Test2)t).A();
((Test2)t).B();
}
public void A()
{
Console.WriteLine("Test.A");
}
public virtual void B()
{
Console.WriteLine("Test.B");
}
}
public class Test2 : Test
{
public new A()
{
Console.WriteLine("Test2.A");
}
public override void B()
{
Console.WriteLine("Test2.B");
}
} This would print the following to a console:
Test.A
Test.B
Test.A
Test2.B
Test2.A
Test2.B So, as you see, unless it's Test2.A being called instead of Test.A when hiding the member, Test.A is called. This is the difference between the call IL instruction that is executed for A() where the callvirt IL instruction is executed for B() .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I'm looking for a control which is similar to the gradient bar seen when windows starts up. Yes, the one where the colors move from left to right. I'm pretty sure I've seen it somewhere around, but can't find it anymore.
Thanks for any help.
Matthias
If eell I ,nust draw to your atenttion to het fakt that I can splel perfrectly well - i;ts my typeying that sukcs.
(Lounge/David Wulff)
www.emvoid.de
|
|
|
|
|
Dear sir,
i want to navigate between different controls in my windows form using enter key. how it is possible. all the events that i hve see is for specific object like form keydown event whenever focuss chage to another child control this event dose't work.
thnx alot.
farukhalifarooq
|
|
|
|
|
You could override the PreProcessMessage method of either the Control or the Form.
Roughly
public override bool PreProcessMessage(ref System.Windows.Forms.Message msg)
{
Keys keyCode=(Keys)(int)msg.WParam & Keys.KeyCode;
if((msg.Msg==WM_KEYDOWN || msg.Msg==WM_KEYUP) && keyCode==Keys.Enter)
{
msg.WParam=(System.IntPtr)0;
return true;
}
}
Michael
CP Blog [^]
|
|
|
|
|
Or even easier: set Form.KeyPreview to true and override the Form.ProcessDialogKey message:
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
return false;
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello C# Guru´s!
I´ve a serious problem with array´s and reflection.
I have a assembly source something like that:
namespace Test
{
class TestClass
{
public int intTest1;
public float fltTest1;
public int[] arrTest;
// default constructor
TestClass()
{
arrTest = new int[12];
}
}
}
For each instance a default constructor defines the array 'arrTest'.
I read MetaInforation from this dll and instanced a object from this class.
Then I walk through every Field and do something.
But if the field is an array I would do something before, and for that I need the Length of the array.
I tried following lines:
using System;
using System.Reflection;
namespace Test
{
class MainClass
{
[STAThread]
static void Main()
{
Assembly assembly = System.Reflection.Assembly.LoadFrom("Test.dll");
foreach(Type tmpType in assembly.GetTypes())
{
if (tmpType.IsClass)
{
MethodInfo[] m = tmpType.GetMethods();
object myInstance = tmpType.InvokeMember(m[0].Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, tmpType, null);
foreach (FieldInfo tmpField in tmpType.GetFields())
{
if (tmpField.FieldType.IsArray)
{
PropertyInfo p = tmpField.FieldType.GetProperty("Length", typeof(int));
int arrLength = (int) p.GetValue(tmpField,null);
// ************************************* //
}
// do something
}
}
}
}
}
}
Compile and Link is no problem...
But if I debug the lines it stops at // *********
with an Exception: The target object does not match (or something like that)
Why? I can´t help myself anymore, what did I wrong?
Please help me...
With regards
Norman-Timo
|
|
|
|
|
Norman-Timo wrote:
what did I wrong?
How about passing the object instance rather than the type
int arrLength = (int) p.GetValue(myInstance,null);
top secret xacc-ide 0.0.1
|
|
|
|
|
Thank´s for your reply!
But this is the first thing I tried!
I only get an exception that the target object does not match
I tried every object which is thinkable but alway´s the exception.
Maybe there´s another (nicer) way to get the array length? But the Method 'GetValue(obj,null)' does not work!
Maybe somone has another idea of getting the arrayLength via reflection???
Please answer
Norman-Timo
|
|
|
|
|
I'm probably missing something increadibly simple. Bascially, I've been playing with MySQl and want to output my queries in a console.
If I run a console app, that's no problems. But if I create a windows app, it doesn't work. Right now I have it set up so that you click a button a connection to the sql server is made. Then I sorta expected a console to pop up and display some query information. Then I close the connection.
The connections are working. I verified that. But Console.Writeline just doesn't display any output. Is this because I created a windows app and not a console app?
As a next step I'm thinking of creating a text box and displaying the query in there. But I just wanted to ask about my console problems.
|
|
|
|
|
Console class only works on console application... if you want just to test your application and show some message use a message box....
MessageBox.Show("Test Message");
Works as well...
cya
Wender Oliveira
.NET Programmer
|
|
|
|
|
|
If you want console output to be visible when used in a Windows Forms application, you have to build the executable as a console app (with /t:exe instead of /t:winexe to the csc.exe command-line compiler, for example). This will start a console window then start your Windows Forms application. Any time you use any of the Console methods, that initial console window is what is being referred to; otherwise, there is no console for the Windows Forms application. It's a completely different subsystem.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Okay cool. Thanks everyone. I was thinking of using a text box actually and having stuff display in there. I was using MessageBox for some lines butn it didn't work for others.
I just assumed that if you called a console window in c# that a console window would pop up. even if calling from a window app. Oh well.
|
|
|
|
|
I'm having some trouble implementing drag & drop between two treeviews in two different winform applications.
It works just fine between two treeviews in the same application though.
What I do is this:
<br />
private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e)<br />
{<br />
...<br />
if (e.Data.GetDataPresent(System.Type.GetType("MyNameSpace.MyTreeNode2")))<br />
{<br />
data = (MyTreeNode2)e.Data.GetData(System.Type.GetType("MyNameSpace.MyTreeNode2"));<br />
<br />
if (data != null)<br />
MyTreeNode2 newFolder = (MyTreeNode2)data.Clone();<br />
<br />
...<br />
}<br />
(The MyTreeNode2 class is inherited from the standard TreeNode class, adding only some extra properties)
This will work fine when doing it with strings, probably since the format for strings is well understood or
something while the format of my treenode object is not so easily shared between applications. Do I need to
implement some kind of serialization stuff for my object for this to work, and pass the string? I know that there
are a couple of predefined data formats available, but in java with WFC (which is where I've just this method
before and it worked better I used the object type DataFormats.CF_WFCOBJECT and it worked fine. Is there a corresponding
solution implemented in .NET/C#?
(It works for a single child node in my treeview, but not for a folder with child elements...)
Can anybody help me?
|
|
|
|
|
Yes, you'll need to marshal the data for transport across application boundaries. The Marshal class may be of some help. Perhaps define a struct or something easy to marshal, fill that with information from the TreeNode , marshal it as an IntPtr or something (declaring it in global memory, for example), and then pack that in the DataObject . That would probably be the easiest solution.
The way you're doing it now would work within the instance of the application.
Also, the format must be the same. So unless these two applications are sharing the assembly that defines your MyNameSpace.MyTreeNode2 class, then you need to just specify a string (maybe something like "MyNameSpace.MyTreeNode2" instead of Type.GetType("MyNameSpace.MyTreeNode2") , which - defined in different assemblies - would yield a different type string).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Isn't it possible to serialize the raw object data from memory into a binary data format and just transport that?
If I just define the MyTreeNode2 class as serializable with the [Serializable] attribute before the class definition
and then I should be able to Serialize it into a byte array or something right? And that should be no problem to
marshal I reckon...
From what I could conclude there is built-in support for this type of binary serialization and deserialization,
(BinaryFormatter class) which should mean that I should be able to use this method without a problem to transport an object..
So, if my thinking is right, can you tell me what's wrong with this? All I know is it doesn't do what I want it to...
(code handling start drag event)
<br />
IFormatter formatter = new BinaryFormatter();<br />
<br />
MemoryStream ms = new MemoryStream();<br />
<br />
formatter.Serialize(ms, draggedItem);<br />
<br />
DataObject dObj = new DataObject( "MyNamespace.MyTreeNode2", ms.GetBuffer() );<br />
selectedTreeView.DoDragDrop( dObj, DragDropEffects.All );<br />
and here is what I want to use on the receiving side. But it doesn't work, the method actually returns after the last line (data = ...) without even continuing execution of the remaining part of the method, or at least the debug stepper of VS won't continue past this point. (Why does this happen?)
(code for handling drop event)
<br />
if ( e.Data.GetDataPresent("MyNamespace.MyTreeNode2") )<br />
{<br />
MyTreeNode2 data = null;<br />
IFormatter formatter = new BinaryFormatter();<br />
<br />
byte[] serializedBytes = (byte[])e.Data.GetData("MyNamespace.MyTreeNode2", false);<br />
MemoryStream ms = new MemoryStream( serializedBytes );<br />
data = (MyTreeNode2)formatter.Deserialize( ms );<br />
...<br />
|
|
|
|
|
An exception might be thrown that would cause execution to stop.
What I described is a way of "serializing" it. You're persisting the object in global memory. Here you might have a problem depending on how .NET's DataObject handles a buffer. You still can't simply cross application boundaries with reference data (of which an array is). It needs to be either packed into memory (like strings and primitives) or locked in global memory (with COM, there's other ways where you can persist data to storage or stream it, but .NET doesn't expose that which is why I've been working on a library that - among other things - contains such an implementation).
Also keep in mind that simply serializing your TreeNode derivative might not be appropriate. Remember that there are other public properties that the BinaryFormatter will traverse and serialize, like the Parent property (so you're actually serializing almost an entire tree because of the references). You should implement ISerializable and serialize only what you need (Text , Tag , etc.). Don't forget about the constructor with the serialization signature.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
You should be able to do it by looking at the raw data bytes. This is more complicated than GetPixel(), but the System.Color structure definitely only supports 8bpp. The upside is that this method can be a lot faster than calling GetPixel thousands of times.
To get at the raw data for a System.Drawing.Bitmap you first have to lock the bits (so the array doesn't move around in memory while you're doing unsafe pointer stuff), then you can get a pointer to the start of a particular scan line, which will be a pointer to the raw array of bytes for that scanline. All of this requires the "unsafe" keyword, which requires a special compiler flag.
Inside the unsafe { ... } block, first call LockBits() on the Bitmap to get a BitmapData structure. Then you can use the Scan0 property of the BitmapData to get a pointer to the first scanline. In your case you would cast this to a (ushort*) type, since each 16bit value is a ushort. Incrementing the pointer will point to subsequent ushort's on the same scanline.
To get to the next row (scan line) of the image, add the value of the Stride property of the BitmapData to the Scan0 value to get the pointer to the start of the second scan line.
I don't have a 48bpp image to try this on, but here's some code for a 32bpp image which you can use as a starting point:
using (Bitmap bmp = new Bitmap("image.tif"))
{
int[,] raw = new int[bmp.Width, bmp.Height];
Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
unsafe
{
BitmapData data = bmp.LockBits(rect,
ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
int max = int.MinValue;
byte *p = (byte *) data.Scan0;
for (int y = 0; y < bmp.Height; y++)
{
byte *q = p;
for (int x = 0; x < bmp.Width; x++)
{
byte red = *(q++);
byte grn = *(q++);
byte blu = *(q++);
raw[x, y] = blu + 256 * grn + 65536 * red;
}
p += data.Stride;
}
bmp.UnlockBits(data);
}
} In your case you'll want ushort instead of byte everywhere, and you wouldn't be saving the pixels as 32-bit ints of course, but the logic is the same. It's also possible that I have the red/blu values backwards here, but you can figure that out.
|
|
|
|
|
When I process this event, it gets called multiple times per page. Why is this? How can I stop it, or should I use another event?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
Are you trying to detect when the page has loaded? try the body.onload event, but be aware that this will only fire when all of the content has downloaded (images etc.). You could also put a call to your function in tags at the bottom of the page if you want it to fire when the html is downloaded.
Find my .Net blog at <a href="http://hiltong.blogspot.com">http://hiltong.blogspot.com</a>
|
|
|
|