Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Finding the PropertyGrid Delete Key Event in VB.NET
 

Finding the PropertyGrid Delete Key Event in VB.NET

Hi MSDN ,


I am using the Property grid in my application .Lets consider [None] has to appear if i set the property to empty.

Consider VB6.0 application to understand clearly .


Lets consider the Image property of Picture box. I set the image from My Documents. THen at the Image property i pressed the DEL key from keyboard .Directly it has to show the [None] as property text bcoz there is no image for the Picture box.

How to achieve this Functionality in Propertygrid ??


Regards,

VS
SilverPlate  Monday, June 15, 2009 3:07 PM

Hi SilverPlate,

From my experience, the edit control on PropertyGrid is actually a TextBox. You can find it and handle the key event.

This is my code snippet:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

'Bind the picture box.

Me.propertyGrid1.SelectedObject = Me.pictureBox1

'The property editor, which is actually a TextBox

Dim textBox As TextBox = Nothing

'Find the edit control

For Each propertyGridControl As Control In TryCast(Me.propertyGrid1, Control).Controls

If propertyGridControl.[GetType]().Name = "PropertyGridView" Then

For Each gridViewControl As Control In propertyGridControl.Controls

If gridViewControl.[GetType]().Name = "GridViewEdit" Then

textBox = TryCast(gridViewControl, TextBox)

End If

Next

End If

Next

If textBox IsNot Nothing Then

'If found, add some event handler to it.

AddHandler textBox.KeyUp, AddressOf textBox_KeyUp

End If

End Sub

Private Sub textBox_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)

If Me.PropertyGrid1.SelectedGridItem.Label = "Image" And e.KeyData = Keys.Delete Then

'If the selected item is correct, do what you want.

MessageBox.Show("Do what you would like here")

End If

End Sub

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, June 18, 2009 7:18 AM

Hi SilverPlate,

From my experience, the edit control on PropertyGrid is actually a TextBox. You can find it and handle the key event.

This is my code snippet:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load

'Bind the picture box.

Me.propertyGrid1.SelectedObject = Me.pictureBox1

'The property editor, which is actually a TextBox

Dim textBox As TextBox = Nothing

'Find the edit control

For Each propertyGridControl As Control In TryCast(Me.propertyGrid1, Control).Controls

If propertyGridControl.[GetType]().Name = "PropertyGridView" Then

For Each gridViewControl As Control In propertyGridControl.Controls

If gridViewControl.[GetType]().Name = "GridViewEdit" Then

textBox = TryCast(gridViewControl, TextBox)

End If

Next

End If

Next

If textBox IsNot Nothing Then

'If found, add some event handler to it.

AddHandler textBox.KeyUp, AddressOf textBox_KeyUp

End If

End Sub

Private Sub textBox_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)

If Me.PropertyGrid1.SelectedGridItem.Label = "Image" And e.KeyData = Keys.Delete Then

'If the selected item is correct, do what you want.

MessageBox.Show("Do what you would like here")

End If

End Sub

Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, June 18, 2009 7:18 AM

You can use google to search for other answers

Custom Search

More Threads

• layout components when resizing
• UserControl with Panel as constituent control
• CollectionEditor - Code Generation for Property Controls failed. Error was: Object reference not set to an instance of an object.
• Sources of knowledge on Designer Support and Services provided by Net 2.0
• Resize Control
• Incrementing the file version of an EXE created when building a Visual Basic 2005 Express project
• Button Click Event in C++
• New WinForms FAQ repository created
• How to add Sort Icon to Gridview Header?
• How to let application override event handler in User Control?