I work with a custom TextBox in a winform project, I added this property in my custom TextBox:
private TextBox _nextControl;
public TextBox NextControl
{
set { _nextControl=value; }
get { return _nextControl; }
}
and I got this result for a form with 2 TextBox(textBox1 and textBox2) in my custom TextBox properties with the property NextControl; I can see all TextBoxes in the form:
In this case the property NextControl will show all TextBox in my form to select my next control.
But when I want to do the same in my new WPF costum TextBox I got this with the same condition(2 TextBoxes, textBox1 and textBox2):
Why I don't have the same result? And how to do this in my WPF project?
Update:
For more explanation, in my winform project I use the property NextControl to select the next control from the UI properties.
protected override void OnKeyDown(KeyEventArgs e)
{
if(e.KeyCode==Keys.Down)
// select the next TextBox chosen in this TextBox option
_nextControl.Select();
}
Because I can already choose the name of the next TextBox in UI, I don't want to do this with extra code.
But this not work in WPF: I can't see the names of my TextBoxes in my window for the property NextControl(automatically in winform if I choose the type of property = TextBox).
p.s.: My custom TextBox inherited from System.Windows.Controls.TextBox.
Update:
I uploded a winform project with the custom TextBox [here] of a sample project for what I want a WPF can behaves the same.
I've updated the link of this sample.
Quick look in your code is telling me that your Windows Form User Control is not compatible with WPF, specially the keyDown event in Windows Form parses parameter "KeyEventArgs e", this is a System.Windows.Form.Key but WPF parses "System.Windows.Input.Key" and WPF doesn't have "Select() method for textbox. Also the WPF binds controls in very different way to the WinForm, therefore your WinFromuserControl Will not work in WPF forms.
If you want to use similar behavior in your WPF form, you have to write one that will be supported in WPF.
From your explanation what I could understand is,
You are using a custom text box class which have NextControl property of Type TextBox
You need the Custom text box to automatically transfer focus to the next text box when you press the down arrow key
You need Visual Studio design support to select which is the next control for all your CustomTextBox instances.
Earlier in Winforms Visual studio supported selection of available matched types from the interface. But now in WPF its not supporting out of the box. (We can achieve this by extending the property grid)
In your case you can make the NextControl as dependency property and achieve the relation to the next control using data binding
For data binding you need to click on the square displayed in property grid at the end of property name but left of property drop down.
Hope you are aware of dependency properties and WPF data binding. The binding you need to use is the the Element name binding.
Based on the fact you are looking for a textbox (a known value) the WinForms application is helping you out filling the propertygrid. Like is true for most of your own controls in WinForms you need to suply the values yourself in WPF this time.
The how part of your question has in basis been answered here and here
Implementing the ICustomTypeDescriptor seems like a hard thing to do but actually most functions can just return the TypeDescriptor's implementation.
The only interesting one is GetProperties where you can return a new PropertyDescriptorCollection that contains the array returned from control.Children.OfType();
Related
i used PropertyGrid control to display properties on gridview.
i have taken an reference of this link http://www.c-sharpcorner.com/article/using-property-grid-in-c-sharp/
which is showing like this
But i need checkbox just before the property name shown in red mark on check/uncheck for any property i need to build expression.
I recommend reading this: How do I change boolean properties with one click in PropertyGrid.
It extends the PropertyGrid control and defines its checkbox controls using UITypeEditor.
As Reza mentioned, your choice of control does not appear optimal. You should probably create a form with TextBox, CheckBox, ComboBox etc. Or make use of DataGridView if your display is catering for multiple records at same time.
If you most definitely want to customize PropertyGrid, here is my another answer which might help you start with.
Linked answer:
You can make use of TrackBar. Note that PropertyGrid by default does
not allow you to add controls like these to it. So, you will need to
do some work here. You will need to create a class that inherits from
System.Drawing.Design.UITypeEditor. Next you will have to set the
editor attribute for the property that has to display track bar as
control. Note that unless you do custom paint, it will be shown as
modal dialog or as dropdown editor.
I am working on a project in which I am using a property grid to display the properties of the selected control.
The Property Grid is fixed to the left edge of the container and in the rest of the space I have the form I am designing.
On clicking a control on the form, the specific control’s property is getting selected.
In the above figure, I have selected the textbox and the textbox’s properties get shown on the propertygrid.
Here if you observe, by default, the Name property is highlighted as well.
Is there some way to unselect this property programmatically?
I have tried some suggestions online but none have helped. I am not able to find find a way to remove all selections from the PropertyGrid, but its behaviour seem to be different form a DataGrid...
Here is why I need this...
On selecting a control, if a property in the property grid is selected, then the property is getting modified.
For example, If i cut the control using Ctrl + X, the selected value in property grid is getting cut which in some cases is forcing user to set the property before modifying anything on the form.
I have tried selecting multiple controls, but in that case alse the selected property seems to be persistent
Since PropertyGrid uses DefaultProperty to select a property in its grid, as an option you can set DefaultProperty attribute at run-time for your object to a non-browsable property, for example:
this.propertyGrid1.SelectedObject = null;
TypeDescriptor.AddAttributes(someControl,
new Attribute[] { new DefaultPropertyAttribute("Site") });
this.propertyGrid1.SelectedObject = someControl;
Well, what you are trying are hacks. It is never a good idea to do such hacks particularly if you are not the only person that use the software.
In your case, the focus should be on the designer while you interact with it. So if the user press Ctrl+X, the designer should respond to the keyboard and it should not have any effect on the property grid (as only one control can have the focus at the same time).
Thus it is up to you to make sure that your designer is focusable, that it has the focus when initially displayed, that it get the focus when you press the TAB key. Pressing the TAB key again should put the focus on the property grid so that user can interact with the grid without using the keyboard.
If you have more than these 2 controls, then obviously TAB should also stop at any appropriate controls. Also, it can be a good idea to have some direct shortcuts like F4 to (show and) activate the properties pane.
If you are not able to make it works, then the best compromise would be to use another windows for the properties grid. By using a distinct Tool windows for the properties, it should not respond to the keyboard when the main windows has the focus.
Here are some links that might help you:
Panel not getting focus
Control.Focus Method() — See Remarks section.
In any case, you should not prevent Ctrl+X to works as expected when the property grid has the focus and a property is selected. Users don't like software that do not follows UI conventions.
As a software developer, you should as much as possible ensure that your application follows standard behaviors. I recommend you that you take one or 2 extra days developing your software properly instead of doing hacks.
Often, compromise to gain a few days will never be fix and will be a pain for many years. Better to do it right from the start. Unselecting an item in the property grid is not an acceptable workaround. Your manager should not allows you to do that.
I am working in C# 4.5 - Winform. My question is specific to C# WinForm.
I want to bind a Control's property to another control's property value on the same form.
I have a GroupBox and a Check Box. When Check box is checked then group box should be enabled and when CheckBox in un-checked then GroupBox should be disabled.
However this task can be fulfilled by implement checkbox "CheckedChanged" event. But i want to accomplish this without writing any code.
I dont know it is possible or not. If possible then please provide solution.
It is possible to do via the designer. For your control -> Properties -> Binding...
But it is a lot of steps to result in a line of code in the designer file which you can add yourself just as easily in the constructor:
this.groupBox.DataBindings.Add( "Enabled", this.myCheckBox, "Checked" );
After using C# a while, I recognized that the user controls of system windows forms don't provide a tooltiptext property like I am knowing it from Visual Basic 6.
I saw that the Form, TextBox, Label, ComboBox and PictureBox don't have such property.
Now I have the curious question:
How would I display a tooltip to the user on mousehover over a control like the above mentioned?
And Yes, I saw the "tooltip" control in the toolbox. It provides a baloon and I do not need a baloon.
It shows the tooltip for one item only once per user session and then never (known problem of the component) and why I would need an extra component, if this should be just a property of the corresponding control, for which I would like to provide a tooltip like TextBoxes, Labels etc.
And Yes, I saw the "Help Provider". I have usually never a help file assigned with my projects,
so I do not use the Help System. What I need is a simple Tooltiptext, not a "F1 component".
Now I am curious, how I would implement a Tooltiptext if the controls don't provide a option for it.
Can it be, that C# has no control tooltips? Even Delphi has! Or have I missed something somewhere?
The solution:
Delete all notifyicon controls from your project to ensure no unwanted info bubbles appearing
in the system tray.
Drag and Drop the Tooltip Component in Toolbox on your form, but ignore all the baloon properties and the baloon timeout properties and baloon timer properties, just Drag and Drop
the component without further action.
A weird new property field "tooltip on tooltip1" shows now up in all TextBoxes and Labels etc. on the form.
Just handle this field as it would be your Tooltip property and don't setup anything else in code
or on the designer. Build and compile. The tooltiptext will now show up as normal tooltip without
baloon functionality.
It sounds like you are using the ToolTip control incorrectly.
1- Drag it from the toolbox to your form.
At this point, each control on the form now gets a property "Tooltip" to set the appropriate text.
2- Set each control's new "ToolTip" property in the way you find most intuitive.
In my wpf window I want to change part of it (make different controls visible) when combo box selection is changed. Exactly like TabControl works, just with combobox. I know I could just make some controls visible while hiding the others in the c# code behind but I want to find out if there are other -better solutions.
You can use two Grid or GroupBox (or other container type) controls and put appropriate set of controls in each of them. This way you can just visibility of panels to hide the whole set of controls instead of hiding each control directly.
It may sometimes be appropriate to create a user control for each set of controls. However, this can depend on a specific case.