Author Topic: Formatting databound control properties  (Read 1670 times)

0 Members and 1 Guest are viewing this topic.

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Formatting databound control properties
« on: December 29, 2010, 12:33:44 PM »
I know how to bind windows form control values to object properties.  That is explained in a million examples.
What I cannot find is a windows forms example where you set the backcolor of some control to red, based on some other controls' value.
It seems web forms have a couple events that help with this, so articles like:
http://msdn.microsoft.com/en-us/library/bb288030.aspx
explain it in depth.

I posted this message in the MS forums:
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/060fd57e-6b1d-4185-8f9a-33386ecdf5ca
The replies are inconclusive.

My goal is to have controls that are completely databound, no "value_changed..." callbacks.
I want control colors to be handled by databinding.  I don't know if this is an advantageous preference though yet.  Maybe it will be a pain after I have dealt with all the little issues.
Maybe the answer is to make an object purely for formatting the control properties like enabled, and backcolor.
Has anyone developed a pattern that seems to be slick?

It occurred to me that a form is its own object that traditionally has the exact code i would place in the formatting object.
So you would think controls would allow binding to the properties of the form itself.
I guess its just as easy to make a separate object dedicated to that form.
So you would end up with a buisiness object that fills in values to the controls, and a formatting object you bind to for things like color and enabled.
Anyone done that?
James Maeding

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Formatting databound control properties
« Reply #1 on: December 29, 2010, 10:52:22 PM »
See if this might help or spawn an idea

I will explain more if needed

Used example from your link

This is windows application with control library

It has a textbox with a property to set what it must start with to be able to leave the textbox

and one to set the color to change to if it does not start with "a"

No code behind form

Here is Code pics and app

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace WindowsFormsControlLibrary1
{
   

    public class TextBoxEntered : TextBox
    {


        public bool Valid { get { return IsValid(); } }
        public string BeginssWith { get; set; }

        private bool IsValid()
        {
            if (this.Text.StartsWith(BeginssWith))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            e.Cancel = (!IsValid());
        }

    }


}



Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
namespace WindowsFormsControlLibrary1
{

    public class RedTextBox : TextBox
    {
        public RedTextBox()
        {
            OriginalColor = this.BackColor;
        }

        public Color ChangeColor { get; set; }
        public Color OriginalColor { get; set; }

        private void ChangeBackColor()
        {
            if (this.Text.StartsWith("a"))
            {
                this.BackColor = OriginalColor;
            }
            else
            {
                this.BackColor = ChangeColor;
            }
        }

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            ChangeBackColor();
            e.Cancel = false;
        }

    }
}