|
What does this have to do with C#?? A dropped connection doesn't have anything to do with your code. Once the connection starts it's up to Encoder and the client to keep it going. Your code has nothing to do with it.
|
|
|
|
|
I want to get the caret position with my C# application.but in some cases ,GetGuiThreadInfo does not work creectly.
it works well on
1. notepad
2. ie
3. explorer
4. word
and works bad on (hwndCaret will be 0)
1. Visual Studio
2. Firefox
3. Sublime Text 2
Anyone who can helps me?
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public int cbSize;
public int flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public Rectangle rectCaret;
}
public GUITHREADINFO? GetGuiThreadInfo(IntPtr hwnd)
{
if (hwnd != IntPtr.Zero)
{
uint threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
GUITHREADINFO guiThreadInfo = new GUITHREADINFO();
guiThreadInfo.cbSize = Marshal.SizeOf(guiThreadInfo);
if (GetGUIThreadInfo(threadId, ref guiThreadInfo) == false)
return null;
return guiThreadInfo;
}
return null;
}
public void SendText(string text)
{
IntPtr hwnd = GetForegroundWindow();
if (String.IsNullOrEmpty(text))
return;
GUITHREADINFO? guiInfo = GetGuiThreadInfo(hwnd);
AddMessage("go");
if (guiInfo != null)
{
IntPtr ptr = (IntPtr)guiInfo.Value.hwndCaret;
AddMessage("hwndCaret = " + ptr.ToInt32());
if (ptr != IntPtr.Zero)
{
AddMessage("Left = " + guiInfo.Value.rectCaret.Left);
for (int i = 0; i < text.Length; i++)
{
SendMessage(ptr, 0x102, (IntPtr)(int)text[i], IntPtr.Zero);
}
}
}
}
public Form1()
{
InitializeComponent();
}
private void AddMessage(string message)
{
var i = listBox1.Items.Add(message);
listBox1.SelectedIndex = i;
}
private void button1_Click(object sender, EventArgs e)
{
Thread.Sleep(2000);
SendText("Abcdefg");
AddMessage("------------");
}
}
|
|
|
|
|
hey Friends....
I have a Table with Name (Student_Table)
fields(Stdid, StdRollNo, StuName)
with 5 records
And A windows Form With Name Form1
Now, My question is..
I want Dynamic Buttons equals to students records ie.. 5 buttons,
on That Buttons i want Student Roll No.
please guys help me
Jack_Spero
|
|
|
|
|
your question has been answered in the other forum you asked it in - a friendly bit of advice - please dont cross post - pick the best forum, post the question once !
'g'
|
|
|
|
|
Hello
I have been trying to export a string array from my C# unmanged DLL export to metatrader , but can not do that , please need you expert suggestion ..
[DllExport("_CheckStringArray", CallingConvention = CallingConvention.StdCall)]
public static void _CheckStringArray([In, Out, MarshalAs(UnmanagedType.LPStr)] StringBuilder [] str)
{
try
{
str[0].Append("hello");
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
#import "CyboMySqlRead.dll"
void _CheckStringArray(string s[]);
string array [3] ;
_CheckStringArray(array)
Print(array[0]);
|
|
|
|
|
Is there a shorthand for updating an object? Something like an initializer?
var item = new FooViewModel { id = "test", name = "test" };
Something like that but instead:
Record myRecord = Context.RecordRepository.GetById(1234);
myRecord {
id = 123,
name = "blah"
}
Cheers, --EA
|
|
|
|
|
|
The best you can do is try and using LINQ to fetch and update records in the same line.
However, what you are tyring to do is not directly possible.
|
|
|
|
|
In VB.net its called "WITH"
C# dont have this trick... But, you can use this:
public static void With<T>(this T _object, Action<T> _action)
{
_action(_object);
}
Usage:
LongInstanceOfPersonVariableName.With(x => {
x.AgeIntVar = 21;
x.NameStrVar = "John";
x.NameStrVar += " Smith";
});
from http://stackoverflow.com/a/5964848[^]
I dont think its the best solution... But... It may work.
modified 24-Jul-13 7:36am.
|
|
|
|
|
I have C# MDI Form EXE as a framework. In which I have to deployed my VB 6 OCX, and also calls another VB 6 OCX from displayed OCX events.
Is this possbile? where I have to make changes C# or VB 6.0?
Does anyone has made similar kind of project before?
Any link?
Thanks in Advance
|
|
|
|
|
It's just a COM component. You just add a reference to it and use it like any other class.
|
|
|
|
|
Thanks Dave,
fisrt COM component loaded fine in C#.net exe framework. My problem is to load another COM component into same C#.net exe framework, and this second COM component will be called from first COM component.
Hope you understand my problem.
Any idea?
|
|
|
|
|
Exactly the same as before. You add a reference to it.
You're problem is that your COM componets should not be calling into each other. That would be a hard-code tie where you couldn't use one component without the other. That's just a bad design.
|
|
|
|
|
I need to use a generic collection of keyvalue pair. I need thr folloeing:
1) should be generic because my values are a customized class
2) iterating should keep the order in which I added Items
3) I should be able to find the index of the element
4) I need access via Key
A dictionary is ok for 1,2,4 but not for 3
SortedList and SortedDictionary are ok for 1, 3, 4 but not for 2.
If I create a
List<keyvaluepair> it is ok for 1,2,3 but not for 4 ( I mean not directly)
SortedList<string, double> d = new SortedList<string, double>();
d.Add("z", 1);
d.Add("a", 2);
d.Add("c", 3);
d.Add("b", 4);
foreach (KeyValuePair<string,double> k in d)
{
Console.WriteLine(k.Key + " " + k.Value);
}
KeyValuePair<string, double> i_item = d.ElementAt(2);
Any suggestion?
Thanks for your time
|
|
|
|
|
If I change your code to use a Dictionary it works for me...
If you directly want to access a value you can also do this:
var value = d["c"];
whil returns the value 3 in this case.
What error do you get when using the Dictionary class?
|
|
|
|
|
KeyValuePair<string, double> i_item = d.ElementAt(2);
may be I was not clear I can't get item from index value. For example I need the second element How can I ask the Dictionary the second element?
The dictionary is an unsorted collection
|
|
|
|
|
An unsorted collection is what you're looking for if you want to prevent the order in which the items were added.
And I don't know what error you're getting when calling
KeyValuePair<string, double> i_item = d.ElementAt(2);
as this works just fine for me with a dictionary
|
|
|
|
|
thanks you are right sorry. Basically I need to find the index from the key and viceversa.
How can I find the index related to the key "3", from the dictionary. It should be 2
|
|
|
|
|
Why do you need to be able to get the index of a KeyValuePair if you already got the key?
Maybe take a look at the hint from Keith.
Note: As I just saw after a bit of researching, the Dictionary uses some hashing of the keys to create some order which means it does not necessarily return the items in the order in which they were added
Source: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx[^]
Quote: For purposes of enumeration, each item in the dictionary is treated as a KeyValuePair<tkey, tvalue=""> structure representing a value and its key. The order in which the items are returned is undefined.
|
|
|
|
|
|
But its not Generic
Well one could create a Generic wrapper for it
|
|
|
|
|
I'll get me coat.
|
|
|
|
|
|
I really like (and upvoted) your question; it was fun to see what I could implement quickly, and I'm going to post the code here before I read the other responses on the thread, since I feel I can learn something from knowing whether or not the code I post actually meets your requirements, and, possibly, learning from other people's responses.
I wonder if I have not just duplicated the functionality of a Dictionary<K, V> ? Also, I suspect Linq queries could be used in some way, efficiently here, rather than, as I did, creating two Lists in the class (memory cost ?).
Disclaimer: I've tested only with KeyValuePair<string, double> since that's what you used. Testing with instances of a custom class as K, or V: that's on my list
Here's the revised class:
public class IndexedKVPList<K, V> : List<KeyValuePair<K, V>>
{
private readonly List<K> keyList = new List<K>();
private readonly List<V> valueList = new List<V>();
new public void Add(KeyValuePair<K, V> newKVP)
{
keyList.Add(newKVP.Key);
valueList.Add(newKVP.Value);
base.Add(newKVP);
}
new public void Remove(KeyValuePair<K, V> killKVP)
{
if(! this.Contains(killKVP)) throw new Exception("error in Remove in IndexedKVPPair");
keyList.Remove(killKVP.Key);
valueList.Remove(killKVP.Value);
base.Remove(killKVP);
}
public KeyValuePair<K, V> GetKVPByValue(V theValue)
{
return this[valueList.IndexOf(theValue)];
}
public K GetKeyByValue(V theValue)
{
return this[valueList.IndexOf(theValue)].Key;
}
public int GetIndexByKVP(KeyValuePair<K, V> theKVPPair)
{
return this.IndexOf(theKVPPair);
}
public int GetIndexByValue(V theValue)
{
return (valueList.IndexOf(theValue));
}
public KeyValuePair<K, V> GetKVPByKey(K theKey)
{
return this[keyList.IndexOf(theKey)];
}
public V GetValueByKey(K theKey)
{
return this[keyList.IndexOf(theKey)].Value;
}
public int GetIndexByKey(K theKey)
{
return (keyList.IndexOf(theKey));
}
} Here's the revised test:
private void TestIKVP()
{
var iKVList = new IndexedKVPList<string, double>();
for (int i = 0; i < 10; i++)
{
iKVList.Add(new KeyValuePair<string, double>("Key: " + i, i));
}
var kvp1 = iKVList.GetKVPByKey("Key: 8");
var kvp2 = iKVList.GetKVPByValue(6);
Console.WriteLine(kvp1 + " " + kvp2);
// test Remove function
iKVList.Remove(iKVList[4]);
foreach (var theKVP in iKVList)
{
Console.WriteLine(theKVP.Key + " Value:" + theKVP.Value.GetType());
}
Console.WriteLine(iKVList.GetIndexByKVP(kvp2));
Console.WriteLine(iKVList.GetIndexByKey("Key: 9"));
Console.WriteLine(iKVList.GetIndexByValue(3));
}
~
“This isn't right; this isn't even wrong." Wolfgang Pauli, commenting on a physics paper submitted for a journal
modified 22-Jul-13 7:06am.
|
|
|
|
|
Thanks a lot, I did a workaround using a syncronized List..
Then I found the following solution
Dictionary<string, double> d = new Dictionary<string, double>();
d.Add("z", 1);
d.Add("a", 2);
d.Add("c", 3);
d.Add("b", 4);
foreach (KeyValuePair<string, double> k in d)
{
Console.WriteLine(k.Key + " " + k.Value);
}
KeyValuePair<string, double> i_item = d.ElementAt(2);
int myIndex = Array.IndexOf(d.Keys.ToArray<string>(), "a");
|
|
|
|