Powershell Textbox placeholder - winforms

I create my from, define my TextBoxes, and for my placeholder text I'm using the following code:
$AssetText.Add_MouseClick({ $AssetText.text = “” })
$ErrorText.Add_MouseClick({ $ErrorText.text = “” })
$IssueText.Add_MouseClick({ $IssueText = “” })
$TestTagText.Add_MouseClick({ $TestTagText.text = “” })
$TroubleshootText.Add_MouseClick({ $TroubleshootText.text = “” })
$ResolutionText.Add_MouseClick({ $ResolutionText.text = “” })
It works to remove text from the TextBox, but if I type a fair amount of text in any TextBox and then click outside of it, then come back to it, it erases the text I was working on.
Is there another function I can use that would work better than this current method? So that initially I can click the $TextBox to make the text disappear, but when writing my own text in the box wont disappear after clicking in and outside of the $TextBox?

Here is another approach on setting Placeholder text for Textbox. The approach relies on handling WM_PAINT message and has been explained and implemented here.
The difference between this approach and the other approach (sending EM_SETCUEBANNER) is this approach works for multi-line TextBox as well.
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$assemblies = "System.Windows.Forms", "System.Drawing"
$code = #"
using System.Drawing;
using System.Windows.Forms;
public class ExTextBox : TextBox
{
string hint;
public string Hint
{
get { return hint; }
set { hint = value; this.Invalidate(); }
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0xf)
{
if (!this.Focused && string.IsNullOrEmpty(this.Text)
&& !string.IsNullOrEmpty(this.Hint))
{
using (var g = this.CreateGraphics())
{
TextRenderer.DrawText(g, this.Hint, this.Font,
this.ClientRectangle, SystemColors.GrayText , this.BackColor,
TextFormatFlags.Top | TextFormatFlags.Left);
}
}
}
}
}
"#
#Add the SendMessage function as a static method of a class
Add-Type -ReferencedAssemblies $assemblies -TypeDefinition $code -Language CSharp
# Create an instance of MyForm.
$form = [Form] #{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Sample";
}
$form.Controls.AddRange(#(
($textBox1 = [ExTextBox] #{Location = [Point]::new(10,10); Hint = "Start typing!" })
($textBox2 = [ExTextBox] #{Location = [Point]::new(10,40); Hint = "Start typing!";
MultiLine = $true; Height = 50; })
))
$null = $form.ShowDialog()
$form.Dispose()

If you would like to have placeholder in native OS way, you can send EM_SETCUEBANNER to TextBox to set the placeholder text:
using assembly System.Windows.Forms
using namespace System.Windows.Forms
using namespace System.Drawing
$code = #"
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr SendMessage(IntPtr hWnd,
int msg, IntPtr wParam, string lParam);
public const int EM_SETCUEBANER = 0x1501;
"#
$Win32Helpers = Add-Type -MemberDefinition $code -Name "Win32Helpers" -PassThru
$form = [Form] #{
ClientSize = [Point]::new(400,100);
Text = "Placeholder Text";
}
$form.Controls.AddRange(#(
($textBox1 = [TextBox] #{Location = [Point]::new(10,10) })
($textBox2 = [TextBox] #{Location = [Point]::new(10,40) })
))
$textBox1.add_HandleCreated({
$Win32Helpers::SendMessage($textBox1.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
$Win32Helpers::SendMessage($textBox2.Handle,$Win32Helpers::EM_SETCUEBANER`
, [IntPtr]0, "Start typing ...")
})
$null = $form.ShowDialog()
$form.Dispose()

Related

Separating taskbar icon for new form in PowerShell

When you create a windows form in PowerShell, it will group with the host console window, even if you change the icon for the form.
How do I separate the new form's taskbar icon out from the PowerShell console icon?
What happens on left, desired effect on right:
Example code:
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(300,200)
$form.ShowInTaskbar = $true
$form.Icon = New-Object system.drawing.icon 'c:\icon.ico'
$form.Text = 'New taskbar icon plz'
$form.BringToFront()
$form.ShowDialog()
The only thing I saw that was somewhat helpful was a reference that changing the "Application ID" would separate this out, but the references are all C code.
https://msdn.microsoft.com/en-us/magazine/dd942846.aspx
Please give PowerShell answers, or if the answers include C# code or calls to other APIs, please explain how they work in PowerShell.
After cobbling together a lot of P/Invoke through C# magic from the internet, mostly from here and some from here, I ended up with this to add a type with one method you can use like [PSAppID]::SetAppIdForWindow($form.handle, "Your.AppId"):
$signature = #'
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
public class PSAppID
{
// https://emoacht.wordpress.com/2012/11/14/csharp-appusermodelid/
// IPropertyStore Interface
[ComImport,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")]
private interface IPropertyStore
{
uint GetCount([Out] out uint cProps);
uint GetAt([In] uint iProp, out PropertyKey pkey);
uint GetValue([In] ref PropertyKey key, [Out] PropVariant pv);
uint SetValue([In] ref PropertyKey key, [In] PropVariant pv);
uint Commit();
}
// PropertyKey Structure
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct PropertyKey
{
private Guid formatId; // Unique GUID for property
private Int32 propertyId; // Property identifier (PID)
public Guid FormatId
{
get
{
return formatId;
}
}
public Int32 PropertyId
{
get
{
return propertyId;
}
}
public PropertyKey(Guid formatId, Int32 propertyId)
{
this.formatId = formatId;
this.propertyId = propertyId;
}
public PropertyKey(string formatId, Int32 propertyId)
{
this.formatId = new Guid(formatId);
this.propertyId = propertyId;
}
}
// PropVariant Class (only for string value)
[StructLayout(LayoutKind.Explicit)]
public class PropVariant : IDisposable
{
[FieldOffset(0)]
ushort valueType; // Value type
// [FieldOffset(2)]
// ushort wReserved1; // Reserved field
// [FieldOffset(4)]
// ushort wReserved2; // Reserved field
// [FieldOffset(6)]
// ushort wReserved3; // Reserved field
[FieldOffset(8)]
IntPtr ptr; // Value
// Value type (System.Runtime.InteropServices.VarEnum)
public VarEnum VarType
{
get { return (VarEnum)valueType; }
set { valueType = (ushort)value; }
}
public bool IsNullOrEmpty
{
get
{
return (valueType == (ushort)VarEnum.VT_EMPTY ||
valueType == (ushort)VarEnum.VT_NULL);
}
}
// Value (only for string value)
public string Value
{
get
{
return Marshal.PtrToStringUni(ptr);
}
}
public PropVariant()
{ }
public PropVariant(string value)
{
if (value == null)
throw new ArgumentException("Failed to set value.");
valueType = (ushort)VarEnum.VT_LPWSTR;
ptr = Marshal.StringToCoTaskMemUni(value);
}
~PropVariant()
{
Dispose();
}
public void Dispose()
{
PropVariantClear(this);
GC.SuppressFinalize(this);
}
}
[DllImport("Ole32.dll", PreserveSig = false)]
private extern static void PropVariantClear([In, Out] PropVariant pvar);
[DllImport("shell32.dll")]
private static extern int SHGetPropertyStoreForWindow(
IntPtr hwnd,
ref Guid iid /*IID_IPropertyStore*/,
[Out(), MarshalAs(UnmanagedType.Interface)] out IPropertyStore propertyStore);
public static void SetAppIdForWindow(int handle, string AppId)
{
Guid iid = new Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99");
IPropertyStore prop;
int result1 = SHGetPropertyStoreForWindow((IntPtr)handle, ref iid, out prop);
// Name = System.AppUserModel.ID
// ShellPKey = PKEY_AppUserModel_ID
// FormatID = 9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3
// PropID = 5
// Type = String (VT_LPWSTR)
PropertyKey AppUserModelIDKey = new PropertyKey("{9F4C2855-9F79-4B39-A8D0-E1D42DE1D5F3}", 5);
PropVariant pv = new PropVariant(AppId);
uint result2 = prop.SetValue(ref AppUserModelIDKey, pv);
Marshal.ReleaseComObject(prop);
}
}
'#
Add-Type -TypeDefinition $signature
And then with your form:
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(300,200)
$form.ShowInTaskbar = $true
$form.visible = $true
[PSAppID]::SetAppIdForWindow($form.Handle, "YourName.App")
It splits off into its own taskbar entry.
Raymond Chen's blog says the format of the AppID should be "CompanyName.ProductName.SubProduct.VersionInformation where the Sub­Product is optional, and the Version­Information is present only if you want different versions of your app to be treated as distinct." and no longer than 128 characters.
Raymond Chen blog entry 1
Raymond Chen blog entry 2
I think this Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99") is supposed to be a Windows Shell ID, of IPropertyStore, but I don't know how to get it without just writing it in.
Same for the AppUserModelIDKey magic GUID which comes from some Win32 propkey.h maybe?
See also:
https://github.com/microsoft/Windows-classic-samples/blob/7cbd99ac1d2b4a0beffbaba29ea63d024ceff700/Samples/Win7Samples/winui/shell/appshellintegration/AppUserModelIDWindowProperty/AppUserModelIDWindowProperty.cpp#L56
This Microsoft doc which infuriatingly handwaves away as "This is typically IID_IPropertyStore".
AppUserModelID and SystemProperties.System.AppUserModel.ID

Issues with adding custom control to winforms tabcontrol

I am having trouble adding a custom zoom-able an pan-able picture box control to a tabcontrol.tabpage dynamically at runtime. I have tried a lot and was wondering if any of you smart fellas might have some advice for a poor noob like myself... here is some code...
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace TAQTv4
{
public class ZoomPanPicBox : ScrollableControl
{
private Image _image;
//Double buffer the control
public ZoomPanPicBox()
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.AutoScroll = true;
this.Image = null;
this.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
this.Zoom = 1f;
}
//New
[Category("Appearance"), Description("The image to be displayed")]
public Image Image
{
get { return _image; }
set
{
_image = value;
UpdateScaleFactor();
Invalidate();
}
}
private float _zoom = 1f;
[Category("Appearance"), Description("The zoom factor. Less than 1 to reduce. More than 1 to magnify.")]
public float Zoom
{
get { return _zoom; }
set
{
if (value < 0 || value < 1E-05)
{
value = 1E-05f;
}
_zoom = value;
UpdateScaleFactor();
Invalidate();
}
}
private void UpdateScaleFactor()
{
if (_image == null)
{
this.AutoScrollMargin = this.Size;
}
else
{
this.AutoScrollMinSize = new Size(Convert.ToInt32(this._image.Width * _zoom + 0.5f), Convert.ToInt32(this._image.Height * _zoom + 0.5f));
}
}
//UpdateScaleFactor
private InterpolationMode _interpolationMode = InterpolationMode.High;
[Category("Appearance"), Description("The interpolation mode used to smooth the drawing")]
public InterpolationMode InterpolationMode
{
get { return _interpolationMode; }
set { _interpolationMode = value; }
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
}
//OnPaintBackground
protected override void OnPaint(PaintEventArgs e)
{
//if no image, don't bother. I tried check for IsNothing(_image) but this test wasn't detecting a no-image.
if (_image == null)
{
base.OnPaintBackground(e);
return;
}
//Added because the first test sometimes failed
try
{
int H = _image.Height;
//Throws an exception if image is nothing.
}
catch (Exception ex)
{
base.OnPaintBackground(e);
return;
}
//Set up a zoom matrix
Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0);
mx.Translate(this.AutoScrollPosition.X / _zoom, this.AutoScrollPosition.Y / _zoom);
e.Graphics.Transform = mx;
e.Graphics.InterpolationMode = _interpolationMode;
e.Graphics.DrawImage(_image, new Rectangle(0, 0, this._image.Width, this._image.Height), 0, 0, _image.Width, _image.Height, GraphicsUnit.Pixel);
base.OnPaint(e);
}
//OnPaint
}
}
//ZoomPicBox
Now this seems to work fine while using the designer... but when trying to add images and controls at runtime the tabs instantiate fine but the zoomPicBox control does nothing so it would seem... This is how I am using it....
public void loadImagesToTabControl()
{
int i = 0;
foreach (Bitmap bitmap in intDwg.getBitmaps())
{
//ToDo add pic boxes and tabs and bitmaps to tabcontrol1
TAQTv4.ZoomPanPicBox picBox = new TAQTv4.ZoomPanPicBox();
picBox.Image = bitmap;
picBox.Anchor = AnchorStyles.Top;
picBox.Anchor = AnchorStyles.Bottom;
picBox.Anchor = AnchorStyles.Left;
picBox.Anchor = AnchorStyles.Right;
picBox.AutoScroll = true;
picBox.CausesValidation = true;
picBox.Visible = true;
picBox.Zoom = 1;
picBox.BackgroundImageLayout = ImageLayout.Tile;
picBox.Location = new System.Drawing.Point(0, 0);
picBox.TabStop = true;
picBox.Enabled = true;
picBox.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
picBox.CreateControl();
string title = "Pg " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
tabControl1.TabPages[i].Controls.Add(picBox);
i++;
/* Possible pictureBox Implementation...
string title = "Pg " + (tabControl1.TabCount + 1).ToString();
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
PictureBox picbox = new PictureBox();
picbox.Anchor = AnchorStyles.Top;
picbox.Anchor = AnchorStyles.Bottom;
picbox.Anchor = AnchorStyles.Left;
picbox.Anchor = AnchorStyles.Right;
picbox.Image = bitmap;
picbox.Height = 800;
picbox.Width = 1300;
tabControl1.TabPages[i].Controls.Add(picbox);
i++;
*/
}
}
}
And as a last note... the pictureBox implementation worked fine as well so I know I am pulling my images from disk fine in the deserialization method of my intDwg class. Any thoughts would be much appreciated! Thanks in advance!
UPDATE:
I got the control to load pictures by setting backgroundimage to bitmap instead of picBox.Image.... FRUSTRATING .... but it seems that the way I have it set up the image is not anchored correctly ... trying to improve this and work it out now... any tips and tricks would be just awesome! Thanks!
UPDATE:
A Screen shot... as you can see the tab pages load correctly and one for each bitmap in my collection, yet the custom zoomPanPicBox control does not seem to want to display! See Bellow:
ahh .... seems I don't have rep to post pics.... ... alright how about...
https://www.dropbox.com/s/ogj5jlcce831n3p/scrst.png?v=0mcns
...
UPDATE AGAIN GOT IT THANKS All was missing setting the size as you had mentioned using the following: picBox.SetBounds(0, 0, 300, 300);
:D:D:D:D:D:D:)
Also, instead of using a counter:
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
tabControl1.TabPages[i].Controls.Add(picBox);
i++;
Just use your "myTabPage" reference:
TabPage myTabPage = new TabPage(title);
myTabPage.Controls.Add(picBox);
tabControl1.TabPages.Add(myTabPage);

Read text from textbox in Coded UI Tests

There is a bug/limitation in the Coded UI Test WinEdit class: when overriding the OnKeyDown method or subscribing to the KeyDown event in a text box, it is not possible to use the WinEdit.Text property.
That is, when you have this...
private void myTextbox_KeyDown(object sender, KeyEventArgs e)
{
// ...
}
...this won't work:
var edit = new WinEdit(ancestor);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
edit.Text = "New value"; // This doesn't work
I've found a work-around for setting the value here:
var edit = new WinEdit(ancestor);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
Mouse.Click(edit);
System.Windows.Forms.SendKeys.SendWait("New value");
My question: does anyone know a work-around for reading the value?
var edit = new WinEdit(Window);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
string actual = edit.Text; // This doesn't work
I found a work-around myself:
[DllImport("user32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern bool SendMessage(IntPtr hWnd, int msg, int wParam, StringBuilder lParam);
const int WM_GETTEXT = 0x000D;
var edit = new WinEdit(Window);
edit.SearchProperties[WinControl.PropertyNames.ControlName] = "myTextbox";
var sb = new StringBuilder(1024);
SendMessage(edit.WindowHandle, WM_GETTEXT, sb.Capacity, sb);
string actual = sb.ToString();
The Solution is :
Suppose you have one window form having one text box.
//Launch your Application
ApplicationUnderTest mainWindow =
ApplicationUnderTest.Launch(#"D:\Samples\YourApplication.exe");
//Search Text box in your windows Form
var username = new WinWindow(mainWindow);
username.SearchProperties[WinControl.PropertyNames.ControlName] = "txtUserName";
//To Set Text or get, Initialize WinEdit object and asign searched object username to WinEdit object editUsername
WinEdit editUsername = new WinEdit(username) {Text = "Pakistan"};
//get text from textbox username
string text = editUserName.Text;
Thanks,

WPF Page navigation

I use a custom textblock in my WPF Application, when I use it in WPF Windows it worked good but when I use it in a WPF Page it make a problem. When I click on a link in my Custom Control it browse the link and show in browser but the WPF page navigate back to another WPF Page too (first page)
namespace Dtwitter.Controls
{
public class TweetTextBlock : TextBlock
{
public TweetTextBlock()
{
}
#region Dependency properties
public string TweetText
{
get { return (string)GetValue(TweetTextProperty); }
set { SetValue(TweetTextProperty, value); }
}
// Using a DependencyProperty as the backing store for TweetText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TweetTextProperty =
DependencyProperty.Register("TweetText", typeof(string), typeof(TweetTextBlock),
new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(OnTweetTextChanged)));
#endregion
private static void OnTweetTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
string text = args.NewValue as string;
if (!string.IsNullOrEmpty(text))
{
TweetTextBlock textblock = (TweetTextBlock)obj;
textblock.Inlines.Clear();
textblock.Inlines.Add(" ");
string[] words = Regex.Split(text, #"([ \(\)\{\}\[\]])");
string possibleUserName = words[0].ToString();
if ((possibleUserName.Length > 1) && (possibleUserName.Substring(1, 1) == "#"))
{
textblock = FormatName(textblock, possibleUserName);
words.SetValue("", 0);
}
foreach (string word in words)
{
// clickable hyperlinks
if (UrlShorteningService.IsUrl(word))
{
try
{
Hyperlink link = new Hyperlink();
link.NavigateUri = new Uri(word);
link.Inlines.Add(word);
link.Click += new RoutedEventHandler(link_Click);
link.ToolTip = "Open link in the default browser";
textblock.Inlines.Add(link);
}
catch
{
//TODO:What are we catching here? Why? Log it?
textblock.Inlines.Add(word);
}
}
// clickable #name
else if (word.StartsWith("#"))
{
textblock = FormatName(textblock, word);
}
// clickable #hashtag
else if (word.StartsWith("#"))
{
string hashtag = String.Empty;
Match foundHashtag = Regex.Match(word, #"#(\w+)(?<suffix>.*)");
if (foundHashtag.Success)
{
hashtag = foundHashtag.Groups[1].Captures[0].Value;
Hyperlink tag = new Hyperlink();
tag.Inlines.Add(hashtag);
string hashtagUrl = "http://search.twitter.com/search?q=%23{0}";
// The main application has access to the Settings class, where a
// user-defined hashtagUrl can be stored. This hardcoded one that
// is used to set the NavigateUri is just a default behavior that
// will be used if the click event is not handled for some reason.
tag.NavigateUri = new Uri(String.Format(hashtagUrl, hashtag));
tag.ToolTip = "Show statuses that include this hashtag";
tag.Tag = hashtag;
tag.Click += new RoutedEventHandler(hashtag_Click);
textblock.Inlines.Add("#");
textblock.Inlines.Add(tag);
textblock.Inlines.Add(foundHashtag.Groups["suffix"].Captures[0].Value);
}
}
else
{
textblock.Inlines.Add(word);
}
}
textblock.Inlines.Add(" ");
}
}
public static TweetTextBlock FormatName(TweetTextBlock textblock, string word)
{
string userName = String.Empty;
string firstLetter = word.Substring(0, 1);
Match foundUsername = Regex.Match(word, #"#(\w+)(?<suffix>.*)");
if (foundUsername.Success)
{
userName = foundUsername.Groups[1].Captures[0].Value;
Hyperlink name = new Hyperlink();
name.Inlines.Add(userName);
name.NavigateUri = new Uri("http://twitter.com/" + userName);
name.ToolTip = "View #" + userName + "'s recent tweets";
name.Tag = userName;
name.Click += new RoutedEventHandler(name_Click);
if (firstLetter != "#")
textblock.Inlines.Add(firstLetter);
textblock.Inlines.Add("#");
textblock.Inlines.Add(name);
textblock.Inlines.Add(foundUsername.Groups["suffix"].Captures[0].Value);
}
return textblock;
}
static void link_Click(object sender, RoutedEventArgs e)
{
try
{
System.Diagnostics.Process.Start(((Hyperlink)sender).NavigateUri.ToString());
}
catch
{
//TODO: Log specific URL that caused error
MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
}
}
change your link click method to
static void link_click(Object sender, RequestNavigateEventArgs e) {
try {
System.Diagnostics.Process.Start(e.Uri.ToString());
} catch {
//TODO: Log specific URL that caused error
MessageBox.Show("There was a problem launching the specified URL.", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
} finally {
e.Handled = true;
}
}
change your
link.Click+=new RoutedEventHandler(link_Click);
to
link.RequestNavigate+=new RequestNavigateEventHandler(link_Click);
Set e.Handled=true in link_click to mark you've dealt with the link click to prevent the framework from additionally processing your link click further.
Alternatively you may be able to just set the TargetName property of Hyperlink to "_blank" and not need the process start command
The code below should make it work the same way in both cases (Page and Window)....
try this to open the hyperlink in web browser in MouseDown of the Hyperlink object.
Process.Start((e.OriginalSource as Hyperlink).NavigateUri.ToString());
e.Handled = true;
Let me know if this helps.

How can I catch scroll events in windows forms PropertyGrid

I'm trying to synchronize the vertical scrollbars of two property grids. The idea is when a user scrolls one property grid the other property grid scrolls by the same amount.
My first approach was to handle the scroll event but it seems PropertyGrid doesn't generate this kind of event. I looked into the controls contained inside the PropertyGrid and there is a PropertyGridView, that I bet is the control with the scrollbar.
Does anybody know a workaround to achieve what I want?
Thank you.
This one shows the synchronization with the neighboring PropertyGridView. Note you will have to extend it to handle the user clicking on either control. This version updates propertyGrid2 to match propertyGrid1, but not vice versa.
using System;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
Control m_pgv_1 = null;
Control m_pgv_2 = null;
MethodInfo m_method_info;
public Form1 ()
{
InitializeComponent ();
// Set the Property Grid Object to something
propertyGrid1.SelectedObject = dataGridView1;
propertyGrid2.SelectedObject = dataGridView1;
// Loop through sub-controlls and find PropertyGridView
m_pgv_1 = FindControl (propertyGrid1.Controls, "PropertyGridView");
m_pgv_2 = FindControl (propertyGrid2.Controls, "PropertyGridView");
// Reflection trickery to get a private/internal field
// and method, scrollBar and SetScrollOffset in this case
Type type = m_pgv_1.GetType ();
FieldInfo f = FindField (type, "scrollBar");
m_method_info = FindMethod (type, "SetScrollOffset");
// Get the scrollBar for our PropertyGrid and add the event handler
((ScrollBar)f.GetValue (m_pgv_1)).Scroll +=
new ScrollEventHandler (propertyGrid1_Scroll);
}
private void propertyGrid1_Scroll (object sender, ScrollEventArgs e)
{
System.Console.WriteLine ("Scroll");
// Set the new scroll position on the neighboring
// PropertyGridView
object [] parameters = { e.NewValue };
m_method_info.Invoke (m_pgv_2, parameters);
}
private static Control FindControl (
Control.ControlCollection controls, string name)
{
foreach (Control c in controls)
{
if (c.Text == name)
return c;
}
return null;
}
private static MethodInfo FindMethod (Type type, string method)
{
foreach (MethodInfo mi in type.GetMethods ())
{
if (method == mi.Name)
return mi;
}
return null;
}
private static FieldInfo FindField (Type type, string field)
{
FieldInfo f = type.GetField (field,
BindingFlags.Instance | BindingFlags.NonPublic);
return f;
}
}
}
It requires a little trickery, but this should do it:
using System;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsApplication1 {
public partial class Form1 : Form {
Control m_pgv = null;
public Form1 () {
InitializeComponent ();
// Set the Property Grid Object to something
propertyGrid1.SelectedObject = dataGridView1;
// Loop through sub-controls and find PropertyGridView
foreach (Control c in propertyGrid1.Controls) {
if (c.Text == "PropertyGridView")
{
m_pgv = (Control)c;
break;
}
}
// Reflection trickery to get a private field,
// scrollBar in this case
Type t = m_pgv.GetType ();
FieldInfo f = t.GetField("scrollBar",
BindingFlags.Instance | BindingFlags.NonPublic);
// Get the scrollBar for our PropertyGrid and add the event handler
ScrollBar sb = (ScrollBar) f.GetValue(m_pgv);
sb.Scroll += new ScrollEventHandler(propertyGrid1_Scroll);
}
private void propertyGrid1_Scroll (object sender, ScrollEventArgs e) {
System.Console.WriteLine ("Scroll");
}
}
}

Resources