WinForms ListBox Append Selection - c#

I have a ListBox with SelectionMode = MultiExtended. I want the default behavior for the ListBox to be "append". In other words, the behavior you get when holding down the control key should be the default, passive functionality for the ListBox.
How would I do this? Do I need to subscribe to the "Mouse Down" and "Key Down" events manually? Is there a setting I'm missing?
Thanks.

Ugly solution but the best I could do.
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const byte KEYEVENTF_KEYUP = 0x02;
public const int VK_CONTROL = 0x11;
private void listBox1_MouseEnter(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, 0, 0);
}
private void listBox1_MouseLeave(object sender, EventArgs e)
{
keybd_event(VK_CONTROL, (byte)0, KEYEVENTF_KEYUP, 0);
}

Use MultiSimple Mode
http://msdn.microsoft.com/en-us/library/system.windows.forms.selectionmode(v=vs.80).aspx
SelectionMode = SelectionMode.MultiSimple

Related

Sendkeys for Windows Start key

I'm trying to use the Sendkeys to simulate the Windows start key, but none of the options I tried work, does anybody know how can it be done?
CODE
[System.Runtime.InteropServices.DllImport("user32.dll")]
private Thread thrTyping;
private void startThread()
{
ThreadStart ts = new ThreadStart(sendKeys);
thrTyping = new Thread(ts);
thrTyping.Start();
}
private void sendKeys()
{
// TEST 1
Thread.Sleep(5000);
SendKeys.SendWait("(^)"+"{ESC}");
// TEST 2
Thread.Sleep(5000);
SendKeys.SendWait("{LWin}");
}
Use keybd_event instead:
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
private static void PressKey(byte keyCode)
{
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
List of KeyCodes (The one you are looking for is 0x5B - left win key)

Simulate the Backspace on a button press in WPF\C#

I've written a WPF touchscreen application. Within this application I've written a user control which is a touch screen keyboard.
I have all the keys working apart from the backspace. Obviously all the keys are buttons and on the backspace button click I'd like to send the back space key to the focused textbox.
I've tried the following but this just inserts a square character in my textbox:
private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
PresentationSource source = PresentationSource.FromDependencyObject(CurrentControl);
//CurrentControl is my Textbox
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)
{
RoutedEvent = UIElement.KeyDownEvent
};
System.Windows.Input.InputManager.Current.ProcessInput(ke);
}
Any ideas? I'm using Visual Studio 2012 & Windows 8.
I'm not sure if this is a typo or not, but I think your issue is that
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.Back)
Should actually be:
KeyEventArgs ke = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Keys.Back)
I think the Keys enumeration is what you're actually looking for here.
EDIT: The above answer does not work. The below answer should, however, solve the problem.
private void buttonBackSpace_Click(object sender, RoutedEventArgs e)
{
CurrentControl.Focus();
keybd_event(BACK, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(BACK, 0, KEYEVENTF_KEYUP, 0);
}
// Virtual Keypress Function
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
public const int KEYEVENTF_KEYDOWN = 0x0001; //Key down flag
public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag
public const int BACK = 0x08; // Backspace keycode.

How do i Turn OFF the Caps lock key

How do i Turn OFF the Caps lock key in textbox. I am using WPF forms.
When textbox is focused I want to turn off caps lock.
Thanks
Its easy , Firstly add namespace
using System.Runtime.InteropServices;
then declare this in the class
[DllImport("user32.dll")]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags,
UIntPtr dwExtraInfo);
Finally , at textBox_Enter event add this code
private void textBox1_Enter(object sender, EventArgs e)
{
if (Control.IsKeyLocked(Keys.CapsLock)) // Checks Capslock is on
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr)0);
}
}
this code will turn off the Capslock .. I have used it at the enter event you can add it according to your requirement!
Checkout this link here
Use this code for WPF froms.
private void txt_KeyDown(object sender, KeyEventArgs e)
{
if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled) // Checks Capslock is on
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
keybd_event(0x14, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
(UIntPtr)0);
}
}

how to find properties in right click Contextmenu of C# web browser

Hi i have a c# application and an embedded browser in it,its task is to find a link and then right click on it and click on properties!
mouse moves programmatically,so i need to find properties in righ click menu!
can you help me how to do this?
i tried pressing 'r' after right click but it didn't work on some computers!
so i need to do it by moving mouse!
here is my code for finding a link and right clicking:
int x = getXoffset(link);
int y = getYoffset(link);
webBrowser1.Document.Window.ScrollTo(x, y);
Linker.Win32.POINT p2 = new Linker.Win32.POINT();
webBrowser1.Focus();
p2.x = webBrowser1.Left + 10;
p2.y = webBrowser1.Top + 5;
Linker.Win32.ClientToScreen(this.Handle, ref p2);
Linker.Win32.SetCursorPos(p2.x, p2.y);
MouseOperations.GetCursorPosition();
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.RightUp);
Any other idea for reaching properties on right click meny is welcomed
use this code:
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
public static void PressKey(Keys key, bool up)
{
const int KEYEVENTF_EXTENDEDKEY = 0x1;
const int KEYEVENTF_KEYUP = 0x2;
if (up)
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
}
else
{
keybd_event((byte)key, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://google.com");//Your link
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Find your link and right click(automatically by your code)
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown);
}
void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
if (e.MouseButtonsPressed == MouseButtons.Right)
{
Thread.Sleep(1000);
PressKey(Keys.P, true);
PressKey(Keys.P, false);
}
}

Why isn't my simulated mouse click doing anything?

I've got this code:
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
Absolute = 0x8000
}
public void SimMouseEvent(MouseEventFlags e, int x, int y)
{
mouse_event((uint)e, (uint)x, (uint)y, 0, UIntPtr.Zero);
}
public void SimLeftClick(int x, int y)
{
SimMouseEvent(MouseEventFlags.LeftUp | MouseEventFlags.RightUp, x, y);
}
My form looks like this:
When you click "Button" it runs this:
private void button3_Click(object sender, RoutedEventArgs e)
{
SimLeftClick(50, 50);
}
And on my Window I also have this:
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("click");
}
When I click the window it says "click" as expected, but when I click "Button" it doesn't seem to do anything.
Are the coordinates absolute, or relative? What about with multiple monitors? Do they only work on the focused application?
I would expect that 50,50 to either hit my window somewhere and trip the "click" handler or click on some random window because it missed my app completely and focus that instead... why isn't anything happening?
You handle a mouse button down message, but send a mouse button up message. A click needs to be button down followed by the same button up.
The coordinates are ignored, because you didn't pass the Move flag.
Try reading the documentation.
Fixed, with Ben's suggestions.
public void SimLeftClick(int x, int y)
{
var scr = Screen.PrimaryScreen.Bounds;
SimMouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp | MouseEventFlags.Move | MouseEventFlags.Absolute,
(int)(x / (double)scr.Width * 65535),
(int)(y / (double)scr.Height * 65535));
}

Categories

Resources