473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3017
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.Componen tModel;

public enum UnitOfCurrency { GBP, USD, INR, EUR } // etc
public class CurrencyAmountC onverter : TypeConverter
{
public override bool CanConvertFrom( ITypeDescriptor Context context,
Type sourceType)
{
return sourceType == typeof(string) ||
base.CanConvert From(context, sourceType);
}
public override object ConvertFrom(ITy peDescriptorCon text context,
System.Globaliz ation.CultureIn fo culture, object value)
{
string text = value as string;
if (text == null) return base.ConvertFro m(context, culture,
value);
return CurrencyAmount. Parse(text);
}
public override bool
GetCreateInstan ceSupported(ITy peDescriptorCon text context)
{
return true;
}
public override object CreateInstance( ITypeDescriptor Context
context, System.Collecti ons.IDictionary propertyValues)
{
decimal amount = (decimal)proper tyValues["Amount"];
UnitOfCurrency unit = (UnitOfCurrency )propertyValues["Unit"];
return new CurrencyAmount( unit, amount);
}
public override bool GetPropertiesSu pported(ITypeDe scriptorContext
context)
{
return true;
}
public override PropertyDescrip torCollection
GetProperties(I TypeDescriptorC ontext context, object value, Attribute[]
attributes)
{
return TypeDescriptor. GetProperties(t ypeof(CurrencyA mount),
attributes).Sor t(new string[] { "Amount", "Unit" });
}
}

[TypeConverter(t ypeof(CurrencyA mountConverter) )]
[ImmutableObject (true)]
public struct CurrencyAmount : IEquatable<Curr encyAmount>, IComparable,
IComparable<Cur rencyAmount>
{
public static CurrencyAmount operator *(CurrencyAmoun t lhs, decimal
rhs)
{
return new CurrencyAmount( lhs.Unit, lhs.Amount * rhs);
}
public int CompareTo(Curre ncyAmount other)
{
int result = this.Unit.Compa reTo(other.Unit );
if (result == 0) result = this.Amount.Com pareTo(other.Am ount);
return result;
}
int IComparable.Com pareTo(object other)
{
return CompareTo((Curr encyAmount)othe r);
}
public override int GetHashCode()
{
return _amount.GetHash Code() * 7 + _unit.GetHashCo de();
}
public override bool Equals(object obj)
{
return base.Equals((Cu rrencyAmount)ob j);
}
public bool Equals(Currency Amount amount)
{
return (amount == this);
}
public static bool operator <(CurrencyAmoun t lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount < rhs.Amount;
}
public static bool operator >(CurrencyAmoun t lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount rhs.Amount;
}
public static bool operator <=(CurrencyAmou nt lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount <= rhs.Amount;
}
public static bool operator >=(CurrencyAmou nt lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount >= rhs.Amount;
}
public static bool operator ==(CurrencyAmou nt lhs, CurrencyAmount
rhs)
{
return lhs.Unit == rhs.Unit && lhs.Amount == rhs.Amount;
}
public static bool operator !=(CurrencyAmou nt 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.IsNullO rEmpty(value)) throw new
ArgumentNullExc eption();
string[] parts = value.Split(':' );
if (parts.Length != 2) throw new ArgumentExcepti on();
return new CurrencyAmount(
(UnitOfCurrency )Enum.Parse(typ eof(UnitOfCurre ncy),
parts[0].Trim(), true),
decimal.Parse(p arts[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 GetStandardValu es, ...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.Ne t. 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
1397
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> <AnyName2 Type="Single" Unit="" Comment="">0</AnyName2> <AnyName3 Type="Single" Unit="" Comment="">0</AnyName3> <AnyName4 Type="Single" Unit="" Comment="">0</AnyName4> <AnyName5 Type="Single" Unit="" Comment="">0</AnyName5>
3
6047
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 from a database and manipulate the TypeConverters dynamically. The property grid then displays the property with a drop down list. It does work when I open the application for the first time. If, however, I open the form and then open a second...
1
7619
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 editing that is a TrackBar in a DropDown control. I managed to write the code for that and it works. However, I would like the numerical value displayed in the propertygrid to be updated as I slide the track bar (for now the value changes when the...
1
306
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 first class. How can I let my user select one of the 'Class1' objects in a propertygrid? So what I want to implement into my application is similar to the property "AcceptButton" on a form. There you can find all the buttons used on the form in a...
5
5043
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 CurrentUICulture to what's specified in the querystring. Since I want to allow UICulture auto detecting, I add UICulture = "auto" to page directive on each page.
4
3072
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 selectedobject will be the form itself. I want the user to change certain properties, thus changing the form. This works fine, but what I really want is to take the changes and lock them into a config file, such that this pc will then display all...
5
1725
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 succeed. As far as I know I can only show the user the property name I use in my class, like "cubic_inches". I would like the user to see something like "Cylinder cubic inches". Is it in any way possible to associate a text to the name of the...
4
18821
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 it seems to me that a PropertyGrid is a perfect modern replacement. I won't bore you with the details with all the controls on the dialog since I'm only concerned with one issue. The legacy dialog contains a drop down list control that is...
0
1257
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 ------->FontDialog -- TO Show --> Name These all below Font things i want2 hide r Enabled=false --> Size --> Unit
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6016
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.