Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Column of Buttons

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support
View previous topic :: View next topic  
Author Message
GreyCloud



Joined: 13 Nov 2008
Posts: 19

PostPosted: Mon Sep 13, 2010 4:14 pm    Post subject: Column of Buttons Reply with quote

Hey there,

do you have a sample / any pointers for how to have a column of buttons - effectively i want to add a delete button to each row. Bonus points if i can have more than one button in each column (eg move up /move down)

Many thanks

David
Back to top
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 5027

PostPosted: Mon Sep 13, 2010 10:52 pm    Post subject: Reply with quote

To do this you need to add an Editor (via the Virtual Tree designer) that uses a System.Windows.Forms.Button as the editor control. Set the DisplayMode for the Editor to "Always" then set the Editor property of the column you want it to display in.

The ValueProperty of the Editor will by default be set to the "Text" property of the button. However because you probably won't map any data to this column this would cause the Text of the button to be set to an empty string. To avoid this you can set the ValueProperty to something else that won't affect the button - I'd suggest the "Tag" property.

To handle the button clicks you need to attach event handlers to the button. Because the button you define in the editor is just a template attaching event handlers to it won't work. See the following post for more details on how to do this using InitializeControl:

http://www.infralution.com/phpBB2/viewtopic.php?t=1884

If you want two buttons in a column you could define your own control which has the two buttons in it and then use it as an editor.
_________________
Infralution Support
Back to top
View user's profile Send private message Visit poster's website
GreyCloud



Joined: 13 Nov 2008
Posts: 19

PostPosted: Tue Sep 14, 2010 10:36 am    Post subject: Reply with quote

Thanks for the tips: to those that come this way in the future:
Code:

 public class ButtonColumn : Column{ // uses XElement but can be changed to use your data type

        private readonly Func<XElement, string> _getText;
        private readonly Func<XElement, bool> _btnOnClick;

        #region ctors

        public ButtonColumn (Func<XElement, string> getText, Func<XElement, bool> btnOnClick) {
            this._getText = getText;
            this._btnOnClick = btnOnClick;
        }

        #endregion

        #region Overrides of Column
       
        protected override bool GetData(XElement xElement, CellData cellData) {
            Button button = new Button { Text = this._getText.Invoke(xElement) };
            cellData.Editor = new CellEditor(button) {
                DisplayMode = CellEditorDisplayMode.Always,
                ValueProperty = button.GetType().GetProperty("Tag")
            };
            cellData.Editor.InitializeControl += EditorInitializeControl;
            return true;
        }

        void EditorInitializeControl(object sender, CellEditorInitializeEventArgs e) {
            if (e.NewControl) {
                Button button = (e.Control) as Button;
                XElement element = e.CellWidget.Row.Item as XElement; // or your data type
                if (button != null) {
                    button.Click += (clicksender, clickevents) =>   this._btnOnClick.Invoke(element);                }
            }
        }
        #endregion       
    }
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Virtual Tree Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group