|
Are you using ListBox or ListView?
For ListView the default property for HideSelection is true, so when it looses focus, it seems like the selection is gone. If you set that to false, the listview will show selection after focus is lost.
If it's ListBox, it shouldn't set the selected index to -1, unless you're doing it programmatically somewhere in your code.
|
|
|
|
|
I am using Listbox. No, I am not setting the index back to -1...Im just so confused...
I change the binding to a text box and it works just fine...
Regards
G
|
|
|
|
|
OK...Heres the situation:
I set the ValueProperty of the listbox to a int property, whereas the property on the object was string...when the listbox updated the object, it casted int to string, but when the object tried to set the ValueProperty it couldn't due to the type conversion.
Probelm solved.
Thanks anyway
GR
|
|
|
|
|
There's nothing exposed in the class nor available through reflection. RegQueryValueEx is used internally to get the value.
So, you'll have to P/Invoke RegQueryValueEx and get the type for yourself. If you need it; otherwise, try calling GetType from the return value of RegistryKey.Getvalue and make assertions based on that. If you get back System.String , you know it was either REG_SZ , REG_MULTI_SZ , or REG_EXPAND_SZ (in the latter case, search for occurances of '%' to determine exactly, and call Environment.ExpandEnvironmentVariables if you want to expand the string). If you get System.Int32 , you know it was a REG_DWORD . For System.Int64 , assume REG_QWORD . If you bet a System.Array , you can either double-check or assume a byte[] array and know that the registry type was REG_BINARY .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for the answer... but
when I do GetType() I have the type of the object while I debug it (I can see which type it is). But how can I check the type programmatically?
of course these following lines are wrongly written, but how can I do correctly these tests in order to correctly cast the object given by RegistryKey.GetValue()?
[CODE]
if (ob.GetType() == System.String)
{ // proceed string }
else if (ob.GetType() == ...)
{ // proceed other objects }
[/CODE]
How can I write such a test?
Thanks.
|
|
|
|
|
Similar to how you did it. Familiarity with such type-checking code is often necessary:
object value = reg.GetValue("SomeName");
if (value is string)
{
}
else if (value.GetType() == typeof(int))
{
}
else if (value.GetType() == typeof(long))
{
}
else if (value is Array)
{
}
else throw new InvalidOperationException("Unknown registry key type."); I used two different ways to determine type. There's the is keyword which actually translates to the simple, one-line IL instruction isinst , but you can only use a reference type (which Int32 and Int64 aren't) as the second operand. You can also use obj.GetType() == typeof(type) , specifying the type at design-time. typeof is a keyword that gets the type and hardcodes it in the generates instructions. If you want to compare two unknown types at runtime, use objA.GetType() == objB.GetType() or the same with the Equals (instead of == ) method (the override for Type.Equals compares types, which includes the assembly name information - not just the namespace and class/struct/interface/enum name).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
aaah... ok I see... That's it. That's what I was searching for.
Thanks a lot!
so, for testing an System.Array object, I have to use the "is" keyword or the typeof() function? I saw that REG_MULTI_SZ are considered as System.Array in .NET... I guess this is an array of "String"...
|
|
|
|
|
You're right - I wasn't thinking. A REG_MULTI_SZ would be an array. In that case, first do a simple check for the System.Array type, then you could get the first element and check it's type. It'll either be a String or Byte so use typeof since Byte is a value type.
Using is is slightly faster (1 or 2 instructions as opposed to 2 or 4 - you make the call how important that is) than using typeof , but can only be used when the second operand is a reference type. Below is an IL comparison of using each(str is a variable referencing a String ):
ldloc.0
isinst [mscorlib]System.String
ldloc.0
callvirt instance class [mscorlib]System.Type [mscorlib]System.Object::GetType()
ldtoken [mscorlib]System.String
call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(
valuetype [mscorlib]System.RuntimeHandle)
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
You know this inverted shortcut icon on top of each page in VS.NET help; you're supposed to click on it, and it drops down a yellow menu that makes you filter the page by C#, VB or JScript. Well, I have to do this for each page. I want it to stick. Can I do that?
Thanks.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
It is supposed to persist for the class reference, but often does not persist for technical articles where such a feature is enabled. This just has to do with the information stored in the MSHelp metadata in each page and any cookies that are created (or aren't created, which is the problem with not being persisted).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi,
i want to redirect a certain window output into my application???????
thanx
|
|
|
|
|
First off, is this a WinForm or WebForm environment? If it's web, are you using frames within the same "window"? Or, are you wanting 2+ separate windows to talk to each other?
This is a start. Let me know.
Michael Flanakin
Web Log
|
|
|
|
|
He wants to redirect the output of another app that he didn't write, like Word or Excel, to a window in his app. Something like PCAnywhere or Remote Desktop Connection.
RageInTheMachine9532
|
|
|
|
|
yes i want to redirect window like excel or word into my app like PcAnyWhere
|
|
|
|
|
Well, at least your question wasn't as simple as I was thinking. Then again, maybe that would've been better. Unfortunately, I don't have a good answer for you. I could make a few speculations, but without playing around with it, your guess is as good as mine. Sorry.
Michael Flanakin
Web Log
|
|
|
|
|
This has been bugging me for awhile now. Can someone give me an example where you would use Application.Run without parameters?
|
|
|
|
|
All Application.Run does is start the message pump. If you give it a window, it will dispatch messages to the window and will automatically terminate the pump (post messages like WM_QUIT ) when that window is closed. You can use Application.Run() to simply start a message pump without displaying a form. You can later call Application.Exit to terminate the pump (also known as the application message loop).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks for your response Heath. I'm still a little confused though. I'm pretty familiar (although not an expert) with the message pump and the role Application.Run plays in creating it. I guess what I'm confused about is if I call Application.Run without specifying a form then how do I go about getting the window handle for use in the SendMessage or PostMessage api's? In other words, how can I actually use the message pump that Application.Run creates? Does the NativeWindow class somehow factor into all of this?
|
|
|
|
|
Consider the following example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Test : Form
{
static void Main()
{
Test t = new Test();
t.ShowDialog();
Application.Run();
}
TextBox tb;
public Test()
{
tb = new TextBox();
tb.Location = new Point(8, 8);
Controls.Add(tb);
Button b = new Button();
b.Location = new Point(tb.Right + 8, tb.Top);
b.Text = "Click";
b.Click += new EventHandler(b_Click);
Controls.Add(b);
}
private void b_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
Application.Exit();
}
} Now, I'm not sure why you'd want to do this, but you could. It's most likely that this would be handy when you want to have a dialog that doesn't act as the main window handle for an application that receives general application messages posted to the pump. You'll notice (if you compile and run this) that closing the dialog doesn't quit the application. Perhaps someone might have a good reason for using such an approach.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Interesting. I never would have thought that the button click message would have been dispatched correctly in this code. Thanks for the example. The reason I was asking is because I have to be able to use DDE in a service or console application so I was experimenting with different ways of creating the message pump without displaying a form. I have successfully used Application.Run with the form parameter, but it seems like a hack since the form is always hidden. I wonder if I could do something similar with the NativeWindow class? (Just thinking out loud!)
By the way, DDE sucks.
|
|
|
|
|
Nice code. Exactly what i needed for a game .
|
|
|
|
|
Hi,in C# i can list all the users in the domain using active directory services.But i just want to list only active users,i.e.users that have been already login.thanks anyway.
|
|
|
|
|
You could query the Last-Logoff and Last-Logon AD attributes (see DirectoryEntry.Properties ) and see if the latter is later than the former (i.e., they logged on after they logged off).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
In C# I can manipulate known objects in the registry using the Registry and RegistryKey classes. But I would like to know what is the equivalent of the RegQueryValueEx Win32 API in C#? Can anyone tell me please? I would like to know (and therefore handle) the type (REG_SZ, REG_MULTI_SZ...) of a given value in the registry when I browse subkeys and hives.
Thanks for the answers.
|
|
|
|
|
i got two buttons in the form. One is for encrypting the message read from a xml file and then encrypt the xml and save back to another file. The other button is for decryption. Again, this button read the encrypted file back, and then perform decryption.
But i don't know why an exception always thrown when processing this line of code "int actualbytesread = encStream.Read(buf, 0, 100);" ... Can anyone give me some instructions what is happening ?
private void Encrypt_Click(object sender, System.EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Application.StartupPath + "\\Data\\userInfo.xml");
Byte[] data = new UnicodeEncoding().GetBytes(xml.OuterXml);
MemoryStream ms = new MemoryStream(data.Length* 2);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(),
CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
rich.Text += new UnicodeEncoding().GetString(bResult) + "\n\n"; encStream.Close();
FileStream fs = new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
fs.Write(bResult, 0, bResult.Length);
fs.Close();
}
private void Decrypt_Click(object sender, System.EventArgs e)
{
FileStream fs= new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.Open, FileAccess.Read, FileShare.None);
byte[] ByteArray=new byte[fs.Length];
int nBytesRead = fs.Read(ByteArray, 0, ByteArray.Length);
fs.Close();
MemoryStream ms = new MemoryStream(ByteArray);
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plaintextmem = new MemoryStream();
ms.Position = 0;
do
{
//Create a byte array into which we will read the plaintext
//from CryptoStream
Byte[] buf = new Byte[100];
try
{
//read the plaintext from CryptoStream
int actualbytesread = encStream.Read(buf, 0, 100);
//if we have reached the end of stream quit the loop
if (0 == actualbytesread)
break;
//copy the plaintext byte array to MemoryStream
plaintextmem.Write(buf,0,actualbytesread);
}
catch(Exception ab)
{
MessageBox.Show(ab.Message, "Asdf");
}
}while(true);
//don't forget to close the streams
encStream.Close();
ms.Close();
//Extract the plaintext byte stream and close the MemoryStream
Byte[] plaintextbyte = plaintextmem.ToArray();
plaintextmem.Close();
//Encode the plaintext byte into Unicode string
string plaintext = new UnicodeEncoding().GetString(plaintextbyte);
encStream.Close();
}
I would appreciate any response and help from yours ~
|
|
|
|