473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

propertygrid - user selectable unit

Lee
Hi,

Using the propertygrid, is it possible to have a user selectable 'unit
of measure' for a field.

For example, I am looking at using a propertygrid for defining inputs
into a calculation. Some of the inputs can be either monetary or
percantage. The ideal scenario is where the user enters the value and
immediately to the right of the value field is a drop down, defaulted
to %,which allows a currency to be selected.

I'm very new to c# so apologies if this is straightforward.

Thanks
Lee

Oct 14 '06 #1
5 2987
Hard to do "all in one", as the PropertyGrid uses the TypeConverter,
which allows for text conversions [inclusive or] list of available
options.

What you can do, however, is to split the two, and have the
TypeConverter be able to parse the entire string, else enter the value
and unit on separate lines in the grid.

See enclosed example (just add a CurrencyAmount property to your class
;-p).

Marc

using System;
using System.ComponentModel;

public enum UnitOfCurrency { GBP, USD, INR, EUR } // etc
public class CurrencyAmountConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
return sourceType == typeof(string) ||
base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
string text = value as string;
if (text == null) return base.ConvertFrom(context, culture,
value);
return CurrencyAmount.Parse(text);
}
public override bool
GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext
context, System.Collections.IDictionary propertyValues)
{
decimal amount = (decimal)propertyValues["Amount"];
UnitOfCurrency unit = (UnitOfCurrency)propertyValues["Unit"];
return new CurrencyAmount(unit, amount);
}
public override bool GetPropertiesSupported(ITypeDescriptorContext
context)
{
return true;
}
public override PropertyDescriptorCollection
GetProperties(ITypeDescriptorContext context, object value, Attribute[]
attributes)
{
return TypeDescriptor.GetProperties(typeof(CurrencyAmount ),
attributes).Sort(new string[] { "Amount", "Unit" });
}
}

[TypeConverter(typeof(CurrencyAmountConverter))]
[ImmutableObject(true)]
public struct CurrencyAmount : IEquatable<CurrencyAmount>, IComparable,
IComparable<CurrencyAmount>
{
public static CurrencyAmount operator *(CurrencyAmount lhs, decimal
rhs)
{
return new CurrencyAmount(lhs.Unit, lhs.Amount * rhs);
}
public int CompareTo(CurrencyAmount other)
{
int result = this.Unit.CompareTo(other.Unit);
if (result == 0) result = this.Amount.CompareTo(other.Amount);
return result;
}
int IComparable.CompareTo(object other)
{
return CompareTo((CurrencyAmount)other);
}
public override int GetHashCode()
{
return _amount.GetHashCode() * 7 + _unit.GetHashCode();
}
public override bool Equals(object obj)
{
return base.Equals((CurrencyAmount)obj);
}
public bool Equals(CurrencyAmount amount)
{
return (amount == this);
}
public static bool operator <(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount < rhs.Amount;
}
public static bool operator >(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount rhs.Amount;
}
public static bool operator <=(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount <= rhs.Amount;
}
public static bool operator >=(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount >= rhs.Amount;
}
public static bool operator ==(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount == rhs.Amount;
}
public static bool operator !=(CurrencyAmount lhs, CurrencyAmount
rhs)
{
return lhs.Unit != rhs.Unit || lhs.Amount != rhs.Amount;
}
private readonly decimal _amount;
private readonly UnitOfCurrency _unit;
public decimal Amount { get { return _amount; } }
public UnitOfCurrency Unit { get { return _unit; } }
public CurrencyAmount(UnitOfCurrency unit, decimal amount)
{
_amount = amount;
_unit = unit;
}
public override string ToString()
{
return _unit.ToString() + ": " + _amount;
}
public static CurrencyAmount Parse(string value)
{
if (value != null) value = value.Trim();
if (string.IsNullOrEmpty(value)) throw new
ArgumentNullException();
string[] parts = value.Split(':');
if (parts.Length != 2) throw new ArgumentException();
return new CurrencyAmount(
(UnitOfCurrency)Enum.Parse(typeof(UnitOfCurrency),
parts[0].Trim(), true),
decimal.Parse(parts[1].Trim()));

}
}

Oct 14 '06 #2
I forgot to say; if the drop-down is runtime (not simply an enum) then
you need a type converter for that too ;-p Specifically, you'd look at
the GetStandardValues, ...Supported and ...Exclusive methods. Note that
the context gives you the parent object, so you should be able to do
some clever "likely options" code if you want...

Marc

Oct 14 '06 #3
An alternative answer (sorry; brain not firing 100% this evening ;-p):
look into UITypeEditor implementations; this is basically a way of
providing a drop-down UI.

This would allow you to place a custom control or two on the drop down,
with the appropriate values into the screen. Look at:
http://msdn2.microsoft.com/en-us/lib...ypeeditor.aspx
In particular, think GetEditStyle() returning DropDown, EditValue
showing your control. You can also do "Modal", which displays the
elipsis.

In terms of what this looks like : think "dock", "anchor" etc; it can
look like whatever you want...

Marc

Oct 14 '06 #4
Hello Lee,

Marc already gave you some pointers for workarounds. This is a fact
that you won't be able to have an editable number + its unit in the
same property row with the Microsoft PropertyGrid. So I have no other
solution to give you. However, if representing your data in a single
row is a strong requirement for you and you can consider a commercial
package, you can try Smart PropertyGrid.Net. There is a screenshot of
this value+unit property in action at
http://www.visualhint.com/visualhint...etfeature6.gif. I can tell
you that from the end-user point of view this is very practical. If you
have any question, you can contact me there
(http://www.visualhint.com/helpdesk).

Best regards,

Nicolas Cadilhac @ VisualHint (http://www.visualhint.com)
Home of Smart PropertyGrid for .Net and MFC

Lee wrote:
Hi,

Using the propertygrid, is it possible to have a user selectable 'unit
of measure' for a field.

For example, I am looking at using a propertygrid for defining inputs
into a calculation. Some of the inputs can be either monetary or
percantage. The ideal scenario is where the user enters the value and
immediately to the right of the value field is a drop down, defaulted
to %,which allows a currency to be selected.

I'm very new to c# so apologies if this is straightforward.

Thanks
Lee
Oct 15 '06 #5
Lee
Marc,

Many thanks for the responses. I will investigate the UITypeEditor
class which sounds very promising..

Lee

Oct 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Sab | last post by:
Hi, I have a XML structure where the element name can be chosen freely but the element attributes must be all of the same names. <Node> <AnyName1 Type="Single" Unit="" Comment="">0</AnyName1>...
3
by: Dave Girvitz | last post by:
I have a PropertyGrid (Windows Forms App) based component that uses TypeConverters to generate ranges of acceptable values for properties. The idea was that I could download the key/value pairs...
1
by: margoulin | last post by:
hello, i'm currently using a PropertyGrid to monitor the properties of some numerical objects, mainly composed of ints and floats that must stay in a specific range. The logical choice for...
1
by: SDK | last post by:
I'm using a propertygrid control in my program, but I'm having some troubles implementing the following. I've got two classes, the second class has a property "Parent" which is an object of the...
5
by: Samuel | last post by:
Hi, I am running into a problem of mixing UICulture = auto and allowing users to select culture using a dropdown list. I am detecting a querystring, "setlang", and when found, setting the...
4
by: Bernie Yaeger | last post by:
How can I set the browsableattributes of the control that has been selected (selectedobject) by the propertygrid? Here's what I'm after - I want to open a form with a propertygrid in it. The...
5
by: Ger | last post by:
The propertygrid is a great control, but I would like to show a more descriptive text for the properties in the control. I tried to find a solution within the system.componentmodel but did not...
4
by: phcmi | last post by:
I have a PropertyGrid question. My task is to replace a legacy dialog box presentation with a modern one. The dialog itself allows the user to set configuration settings in our application, so...
0
by: dvsriram | last post by:
hii , I am using Font class as property and showing in the propertygrid . Now i want to show only the font dialog in the propertygrid . The remaining things below the Font ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.