2

Im thinking about creating control - normal multiline textbox in which user will insert phone numbers with commas separating each number.

1111111111,
2222222222,
3333333333,
4444444444,
...

I have got entity User and Entity PhoneNumber.

Now I would like to bind phoneNumber entity to that control. The question is what should I implement in this control so that it will understand what to bind. For example if I bind phoneNumber to combobox .net knows how to show it on this control but how would it be in my case ?

thanks for any help and hint on how to start creating something like this.

bye

2
  • I just spoke to the Oracle at Delphi and presented your question. She shrugged and looked at me in that way that women do when you think you've said the right thing, but might well have caused a fall-out with the in-laws for the next 20 years. Commented Nov 27, 2010 at 22:23
  • Textbox control is not meant to be bound to a collection of objects. Its for one-to-one mapping. You must consider an alternative approach to the problem. Make use to other controls like listbox, gridview etc. Commented Dec 7, 2010 at 6:45

3 Answers 3

1

You can do as it is done in Combobox, Have a property for data source and data members and then in your code loop through your database's phone no. column and append them in the text box with a comma after each entry. i myself have implemented something similar to this.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok but in combobox I have got DataSource and in my control I dont. I only can use dataBindings. I found this article: support.microsoft.com/kb/327413 and I can set in my control property ICollection<String> and then it works but I want it to be ICollection<Number>. In main form I use BindingSource and I set BindingSource.DataSource = typeof(Number); then in code i write BindingSource.DataSource = MyEntityContext.Number and I set binding in my control to bindingSource.DataSourec but I can only choose properties of Number class not Number as it is ... I want to have list of numbers binded
0

maybe converting your data in presentation for user's view and convert back in your domain will solve your problem. Here is a sample code of mine for converting Enumeration presentation to localized resources in my project: although it's not an elegant code but maybe solve your problem. in convert you can split your join your collection of number by a commma and set it to your control and in conver back split the string by comma and build your collection.

[MarkupExtensionReturnType(typeof (IValueConverter))]
public abstract class ConverterMarkupExtension<T> : MarkupExtension where T : class, IValueConverter, new()
{
    private static T _converter;

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return _converter ?? (_converter = new T());
    }
}

public class EnumConverter<T> : ConverterMarkupExtension<EnumConverter<T>>, IValueConverter
    where T : struct
{
    #region Implemented Interfaces

    #region IValueConverter

    /// <summary>
    ///   convert from enum to string
    /// </summary>
    /// <param name = "value"></param>
    /// <param name = "targetType"></param>
    /// <param name = "parameter"></param>
    /// <param name = "culture"></param>
    /// <returns></returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return null;
        }

        string convert = typeof(GlossaryResources).GetProperty(value.ToString()).GetValue(null, null).ToString();

        return new Item { Name = value.ToString(), Value = convert };
    }

    /// <summary>
    ///   convert from string to enum value
    /// </summary>
    /// <param name = "value"></param>
    /// <param name = "targetType"></param>
    /// <param name = "parameter"></param>
    /// <param name = "culture"></param>
    /// <returns></returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
        {
            return null;
        }

        T enumvalue;
        Enum.TryParse(value.Cast<Item>().Name, out enumvalue);
        return enumvalue;
    }

    #endregion

    #endregion
}

public class GenderEnumConverter : EnumConverter<Gender>
{
}

Comments

0

You could create your own custom implementation of a TextBox, that has a property that accepts your collection of object and wraps the 'Lines' property of the Textbox. Something like this (assuming that NumberText is the name of the field that exposes the actual text):

    public class NumberBox : TextBox
    {
        public IList<Number> Numbers 
        {
            get
            {
                List<Number> numbers = new List<Number>();
                foreach (string line in Lines)
                {
                    numbers.Add(new Number()
                    {
                        NumberText=line
                    });
                }
                return numbers;
            }

            set
            {
                List<string> numberStrings = new List<string>();
                foreach (Number n in value)
                {
                    numberStrings.Add(n.NumberText);
                }
                this.Lines = numberStrings.ToArray();
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.