I have a property grid that will have a few properties referenced. I would like to have one of the items in the property grid to be a button or even have a ellipses button which will act like a button on a normal win form.
Is there a way to do this?
Appreciate your help in advance!
I recommend reading Getting the Most Out of the .NET Framework PropertyGrid Control.
It walks through how to create a custom UI for your property, which could include a button that opens a popup/separate form/etc.
I added collapse all and expand all buttons to the PropertyGrid using extension methods.
PropertyGrid Buttons
namespace MyNameSpace
{
public static class PropertyGridHelper
{
private static PropertyGrid getPropertyGridParent(object sender)
{
PropertyGrid propertyGrid = null;
ToolStripButton toolStripButton = sender as ToolStripButton;
// ToolStripButton -> ToolStrip -> PropertyGrid
if (toolStripButton != null)
{
ToolStrip toolStrip = toolStripButton.GetCurrentParent() as ToolStrip;
if (toolStrip != null)
{
propertyGrid = toolStrip.Parent as PropertyGrid;
if (propertyGrid != null)
{
propertyGrid.CollapseAllGridItems();
}
}
}
return propertyGrid;
}
private static void propertyGridCollapseAllClick(object sender, EventArgs e)
{
PropertyGrid propertyGrid = getPropertyGridParent(sender);
if (propertyGrid != null)
{
propertyGrid.CollapseAllGridItems();
}
}
private static void propertyGridExpandAllClick(object sender, EventArgs e)
{
PropertyGrid propertyGrid = getPropertyGridParent(sender);
if (propertyGrid != null)
{
propertyGrid.ExpandAllGridItems();
}
}
public static void AddCollapseExpandAllButtons(this System.Windows.Forms.PropertyGrid propertyGrid)
{
foreach (Control control in propertyGrid.Controls)
{
ToolStrip toolStrip = control as ToolStrip;
if (toolStrip != null)
{
toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.CollapseAll, propertyGridCollapseAllClick));
toolStrip.Items.Add(new ToolStripButton("", Properties.Resources.ExpandAll, propertyGridExpandAllClick));
}
}
}
}
}
UITypeEditor, using the IWindowsFormsEditorService... thats what it was. Got it! Thanks for the direction!
Related
Today I got a problem in my development.
I have a Windows Form like this :
I need to enable the button "Appliquer" when the content of one of my textbox change.
I know that I can put the KeyPress event on each textbox and enable my button with that. In this window it can be easy to do that because there is only 10 textbox but I have an other window with more of 100 textbox and I think there is a better solution.
I tried to put the Keydown event directly in my windows form but it doesn't work.
So my question is, how can I do this. If someone have an idea ?
Thank you in advance !
Thomas
Since you already have 100+ textboxes in your form. I am assuming performance is not an issue for you.
In your form constructor, call this method. It will attach the event to all the textbox controls present in your form & inside sub controls such as groupbox, panel etc. (if you require)
There could be better ways of iteration..
public Form1()//your constructor
{
InitializeComponent();
AttachEvent(this);
}
void AttachEvent(Control CTrl)
{
foreach (Control c in CTrl.Controls)
{
if (c is TextBox)
{
c.TextChanged += new EventHandler(c_TextChanged);
continue;
}
if (c.HasChildren)
{
AttachEvent(c);
}
}
}
void c_TextChanged(object sender, EventArgs e)
{
//Your Code here btnGo.Enabled = !btnGo.Enabled;
}
What you can do is to extend TextBox make a field ( accessible from the designer ) to bind that TextBox into some other control.
public class MeTextBox
: TextBox
{
public override string Text
{
get { return base.Text; }
set
{
if ( m_DependantControl != null )
{
m_DependantControl.Enabled = !string.IsNullOrWhiteSpace(value);
}
base.Text = value;
}
}
Control m_DependantControl;
[Browsable(true)]
public Control DependantControl
{
get { return m_DependantControl; }
set { m_DependantControl = value; }
}
}
Now you can use MeTextBox as a regular TextBox. And if you want to make it control Enabled flag of some other Control you can just specify DependantControl property which will be accessible in the designer.
Fitting this into your example (code):
// assume you have a Button named btnConfirm
// and want to enable this button only when your `TextBox` has some text
MeTextBox mtb = new MeTextBox();
mtb.DependantControl = btnConfirm;
And if you do not want to make it in the code you can use designer directly.
To make it other way around ( one button dependant on many text boxes ) you can extend Button object :
public class MeButton
: Button
{
List<TextBox> m_DependantOn = new List<Control>();
[Browsable(true)]
public List<TextBox> DependantOn
{
get { return m_DependantOn; }
set { RemoveEvents(); m_DependantOn = value; AssignEvents(); }
}
void RemoveEvents()
{
foreach(TextBox ctrl in m_DependantOn)
ctrl.TextChanged -= WhenTextChanged;
}
void AssignEvents()
{
foreach(TextBox.ctrl in m_DependantOn)
ctrl.TextChanged += WhenTextChanged;
}
void WhenTextChanged(object sender, TextChangedEventArgs e)
{
this.Enabled = true;
}
}
When a button is clicked, I want to change the background image in the mainpage. In the xaml I have ; Style="{StaticResource LayoutGridStyle}for background. How can I achieve this ? Thanks !
It can be done like this:
public static class FrameworkElementExtensions
{
public static object TryFindResource(this FrameworkElement element, object resourceKey)
{
var currentElement = element;
while (currentElement != null)
{
var resource = currentElement.Resources[resourceKey];
if (resource != null)
{
return resource;
}
currentElement = currentElement.Parent as FrameworkElement;
}
return Application.Current.Resources[resourceKey];
}
}
private void PageTitle_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
ApplicationTitle.Style = (Style)ApplicationTitle.TryFindResource("PhoneTextTitle1Style");
}
sample is given here
Include the style or background image (however you want to handle it) as a property in your ViewModel for the mainpage. Then bind to this...
Style="{Binding NameOFViewModelProperty}
The when you change the style through the button clicked code make sure you raise a property changed event...
RaisePropertyChanged("NameOFViewModelProperty");
so the page knows to refresh itself with the changes.
In my WinForms application written in C# there is a Button on one Form which needs to slightly alter the appearance of a second Form (just change the Text on a Button).
I have managed to do this, but the code is horribly long, and I believe there must be a much more concise way of achieving the same thing.
Here is my code for the Button on Form frmConflicts and how it changes the Text on the Button btnAddCase on Form frmAdmin (works, but seems too long) -
private void btnNoConflicts_Click(object sender, EventArgs e)
{
try
{
foreach (Form f in Application.OpenForms)
{
if (f.Name == "frmAdmin")
{
frmAdmin a = (frmAdmin)f;
a.conflictsClear = true;
foreach (Control ctrl in a.Controls)
{
if (ctrl.Name == "panAdmin")
{
foreach (Control ctrl2 in ctrl.Controls)
{
if (ctrl2.Name == "tabControlAdmin")
{
TabControl tab = (TabControl)ctrl2;
foreach(TabPage page in tab.TabPages)
{
if (page.Name == "pageNewCase")
{
foreach (Control ctrl3 in page.Controls)
{
if (ctrl3.Name == "panCaseDetails")
{
foreach (Control ctrl4 in ctrl3.Controls)
{
if (ctrl4.Name == "btnAddCase")
{
ctrl4.Text = "Add Case";
}
}
}
}
}
}
}
}
}
}
}
}
this.Close();
}
catch (Exception eX)
{
MessageBox.Show("frmConflicts: btnNoConflicts()" + Environment.NewLine + eX.Message);
}
Any help to significantly reduce the amount of code would be much appreciated, as I am going to need to do similar interactions between Forms elsewhere in my application.
If your button is added through designer and is not dynamically created the solution is simple: add a method inside your frmAdmin like
public void ChangeCaption(string caption)
{
btnAddCase.Text = "Add case";
}
and then
var frmAdmin = Application.OpenForms.OfType<Form>().FirstOrDefault(x => x.GetType() == typeof(frmAdmin));
if (frmAdmin != null)
{
frmAdmin.ChangeCaption("Add case");
}
I think it`s help to you
foreach (Form f in Application.OpenForms)
{
var controls =this.Controls.Find("btnAddCase", true);
if(controls!=null)
foreach(var control in controls)
{
control.Text="Add case";
}
}
If the the appearance of second from require a change on first from you should solve this in another way.
The best is that your button that require a change should be open for capture the event of form two open and then apply required change.
In the place where you declare your button you should assign to it a listener that will capture the Form2 opening and then apply action.
so in the method private void btnNoConflicts_Click(object sender, EventArgs e) you should trigger event for that button to capture instead off searching it.
You could use LINQ + ControlCollection.Find:
Control btnAddCase = Application.OpenForms.Cast<Form>()
.Where(f => f.Name == "frmAdmin")
.SelectMany(f => f.Controls.Find("btnAddCase", true)) // true means recursive
.FirstOrDefault();
if(btnAddCase != null)
btnAddCase.Text = "Add Case";
You could create a public property and subscribe to a PropertyChanged event from your form, you'll need your class that has the public variable to extend INotifyPropertyChanged.
//Your class
public class ButtonText : INotifyPropertyChanged
{
private string _buttonText;
public string ButtonValue
{
get{ return _buttonText; }
set
{
//Sets the value of _buttonText to the value passed in an argument
_buttonText = value;
RaisePropertyChanged("ButtonValue");
}
}
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
In your form class you'd bind to the property ButtonValue property of the ButtonText class like so:
ButtonText buttonObj = new ButtonText();
//Property field to bind, object to bind, property to bind
btnAddCase.DataBindings.Add("Text", buttonObj,"ButtonValue");
buttonObj.ButtonText = "Your text to bind.";
Because the btnAddCase.Text property is bound to the ButtonValue property of the ButtonText class, your btnAddCase.Text property will reflect the value of your ButtonText.ButtonValue property at all times, it's also a two way binding.
I'm building a custom data type using the user control wrappper method. Within it I am adding the existing TinyMCE data type. The problem is that I need to find a way to dynamically get a hold of the current TabPage on which the data type resides so that I can add the TinyMCE buttons to the menu. This is what I have currently (the TabPage is hardcoded):
Using statements:
using umbraco.cms.businesslogic.datatype;
using umbraco.editorControls.tinyMCE3;
using umbraco.uicontrols;
OnInit method:
private TinyMCE _tinymce = null;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.ID = "crte";
DataTypeDefinition d = DataTypeDefinition.GetDataTypeDefinition(-87);
_tinymce = d.DataType.DataEditor as TinyMCE;
ConditionalRTEControls.Controls.Add(_tinymce);
TabView tabView = Page.FindControl("TabView1", true) as TabView;
TabPage tabPage = tabView.Controls[0] as TabPage;
tabPage.Menu.InsertSplitter();
tabPage.Menu.NewElement("div", "umbTinymceMenu_" + _tinymce.ClientID, "tinymceMenuBar", 0);
}
User control:
<asp:PlaceHolder ID="ConditionalRTEControls" runat="server" />
Note: Page.FindControl is using a custom extension method that recursively finds the control.
I'd love if there was a way to access the TabPage via the Umbraco API, but, after working on this for the past several hours, the only way I could get the tab was by traversing the parent controls until I came to the tab.
Code:
private TinyMCE _tinymce = null;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.ID = "crte";
DataTypeDefinition d = DataTypeDefinition.GetDataTypeDefinition(-87);
_tinymce = d.DataType.DataEditor as TinyMCE;
ConditionalRTEControls.Controls.Add(_tinymce);
}
protected void Page_Load(object sender, EventArgs e)
{
TabView tabView = Page.FindControl("TabView1", true) as TabView;
TabPage tabPage = GetCurrentTab(ConditionalRTEControls, tabView);
tabPage.Menu.NewElement("div", "umbTinymceMenu_" + _tinymce.ClientID, "tinymceMenuBar", 0);
}
private TabPage GetCurrentTab(Control control, TabView tabView)
{
return control.FindAncestor(c => tabView.Controls.Cast<Control>().Any(t => t.ID == c.ID)) as TabPage;
}
Extension Methods:
public static class Extensions
{
public static Control FindControl(this Page page, string id, bool recursive)
{
return ((Control)page).FindControl(id, recursive);
}
public static Control FindControl(this Control control, string id, bool recursive)
{
if (recursive)
{
if (control.ID == id)
return control;
foreach (Control ctl in control.Controls)
{
Control found = ctl.FindControl(id, recursive);
if (found != null)
return found;
}
return null;
}
else
{
return control.FindControl(id);
}
}
public static Control FindAncestor(this Control control, Func<Control, bool> predicate)
{
if (predicate(control))
return control;
if (control.Parent != null)
return control.Parent.FindAncestor(predicate);
return null;
}
}
I have following class that defines attached property to set children's margin:
public class MarginSetter
{
public static Thickness GetMargin(DependencyObject obj)
{
return (Thickness)obj.GetValue(MarginProperty);
}
public static void SetMargin(DependencyObject obj, Thickness value)
{
obj.SetValue(MarginProperty, value);
}
public static readonly DependencyProperty MarginProperty =
DependencyProperty.RegisterAttached("Margin", typeof(Thickness), typeof(MarginSetter), new UIPropertyMetadata(new Thickness(), CreateThicknesForChildren));
public static void CreateThicknesForChildren(object sender, DependencyPropertyChangedEventArgs e)
{
var panel = sender as Panel;
if (panel == null) return;
foreach (var child in panel.Children)
{
var fe = child as FrameworkElement;
if (fe == null) continue;
fe.Margin = MarginSetter.GetMargin(panel);
}
}
}
The problem is that when CreateThicknesForChildren is invoked no child controls are added to parent yet. How to fix this class so that it will correctly set margin on all child controls?
In my project no controls are dynamically added to parent, they all are created in xaml file. By the way, designer works correctly and somehow correctly sets margin for all children elements.
How about registering to the Loaded event of the panel? it won't help if you're adding items dynamically later, but for the basic 95% it would work:
public static void CreateThicknesForChildren(object sender, DependencyPropertyChangedEventArgs e)
{
var panel = sender as Panel;
panel.Loaded += ...
}