473,396 Members | 1,847 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,396 software developers and data experts.

Help! Problems with custom TypeConverter and Persistence...

(PS: Cross post from microsoft.pulic.dotnet.framework.aspnet.webcontrol s)

I've been looking lately for a way to keep the Properties panel for Controls
'clean'...

My goal is to keep similar public properties of a custom Control neatly tied
together -- rather than all over the IDE.

One such set of values that will rarely be changed, so should have little
priority in the IDE Properties panel, and therefore a good candidate for
keeping
in an expandableobject like way, is maybe Captions that are used
through the control.

So assuming that I made the following class

class MyControl : WebControl {
....

public class cCaptions {
public string _Btn_Submit = "Login";
public string _Btn_NewUser = "Register";
public string Btn_Submit {get{return _Btn_Submit;}set{_Btn_Submit=value;}}
public string Btn_NewUser {get{return
_Btn_NewUser;}set{_Btn_NewUser=value;}}
}//Subclass:End
....

private cCaptions _Captions = new cCaptions;
....
[TypeConverter(typeof(TestConverter)),
PersistenceMode(PersistenceMode.Attribute )]
public cCaptions {get {return _Captions;}set {_Captions = value;}
....
}//Control:End
As you can see the public property now requires a custom TypeConverter so
that it can be seen in the IDE -- and deal with persistence as an attribute.

I originally thought/tried using the standard ExpandableObject one -- but I
needed a way to persist it in one string -- just like how style="" attribute
is done, with : and ; as sep chars between key and value...
Well.
The TypeConverter I wrote works atleast for converting between object and
string, and now in the IDE's Property panel I get the expandableObject look
(+/- tree) as well as a means of reading it as one string which looks like

<MyControl CAPTIONS="Btn_Submit:Login;Btn_NewUser:Register"></MyControl>
My problem with this is that it all LOOKS right -- but its not acting
right...
Problems/Observations:
a) When I edit the string directly in the Properties panel -- eg change it
to "Btn_Submit:HELLO;Btn_NewUser:Come on in!" it ---well...doesn't do it.
The moment I hit Save -- it reverts back to to the default values. :-(
b) When I click + to expand the Expandable options, and edit the Btn_Submit
value directly -- it updates the summary string line/title to the new
values -- AND updates the interface's actual label (I have to hit save to
get this to trigger)-- but when I check the Html of the control -- its not
been notified of the changes and still shows default values...
c) If I edit the Html of the Control to another value. Nothing happens.
In other words -- total disconnection between the values of the property --
and what is persisted.

Anybody have an idea?
I attach the TypeConverter that I wrote in case it is the cause -- or if
not, that it may be the starting point for someone else...

using System;
using System.Web.UI.Design;
using System.Web;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Collections;

namespace XAct.Web.Controls {

public class TestConverter : ExpandableObjectConverter {
System.Type _Type = typeof(XAct.Web.Controls.LoginPanel.cCaptions);

public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
{
if (t == typeof(string)) {return true;}
return base.CanConvertFrom(context, t);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo info,object value) {
if (value is string) {
string[] tParts;
tParts = SplitPlus((string) value, ";");
object o = _Type.Assembly.CreateInstance(_Type.ToString());
string tKey = string.Empty;
string tValue = string.Empty;
int tPos = 0;
foreach (string tPart in tParts){
if (tPart == string.Empty){continue;}
tPos = tPart.IndexOf(':');
if (tPos == -1){
tPos = tPart.Length;
tKey = tPart;
tValue = string.Empty;
}else{
tKey = tPart.Substring(0,tPos);
tValue = tPart.Substring(tPos+1);
System.Reflection.PropertyInfo oPI =
_Type.GetProperty(tKey,System.Reflection.BindingFl ags.SetProperty |
System.Reflection.BindingFlags.Public
|System.Reflection.BindingFlags.IgnoreCase);
if (oPI != null){
oPI.SetValue(o,tValue,null);
}
}
}
return o;
}
return base.ConvertFrom(context, info, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destType) {
object o=null;
try {
o = System.Convert.ChangeType(value,_Type);
}catch{}
if ((o !=null) && (destType == typeof(string))) {
string tResult = string.Empty;
string tDivChar = string.Empty;
PropertyInfo[] oPIs = _Type.GetProperties();
foreach(PropertyInfo oPI in oPIs){
string tKey = oPI.Name;
object oVal = oPI.GetValue(o,null);
string tValue = string.Empty;
if (oVal != null){
tValue = oVal.ToString();
if (tValue != string.Empty){tResult += tDivChar + tKey + ":" + tValue;}
if (tDivChar == string.Empty){tDivChar = ";";}
}
}
return tResult;
}
return base.ConvertTo(context, culture, value, destType);
}

/// <summary>
/// Split function that only splits if not within brackets or quotes.
/// </summary>
/// <param name="qString"></param>
/// <param name="qDivChar"></param>
/// <returns></returns>
public static string[] SplitPlus(string qString, string qDivChar) {
if (qDivChar== String.Empty){qDivChar = ",";}
ArrayList tResults = new ArrayList();
string tChar="";
string tWord = "";
bool tEscaped=false;
string tLastChar = "";
System.Collections.Stack tQuotes=new System.Collections.Stack();
for (int i=0;i<qString.Length;i++) {
tChar = qString[i].ToString();
if (tQuotes.Count == 0) {
//We are outside of quotes, so look for quote beginnings...
if ((tChar == "(") ||
(tChar == "{") ||
(tChar == "[") ||
(tChar == "'") ||
(tChar == "\"")) {
tQuotes.Push(tChar);
tLastChar=tChar;
}
if ((tChar == qDivChar)) {
tResults.Add(tWord);
tWord="";
tChar = "";
}
}
else {
//We are within quotes...need to look for close chars:
if (tEscaped ==false) {
if (tChar == "\\") {tEscaped=true;}
else {
tLastChar =(string)tQuotes.Peek();
if ((tChar == "\"") && (tChar == tLastChar)) {
tQuotes.Pop();
}
else if ((tChar == "\'") && (tChar == tLastChar)) {
tQuotes.Pop();
}
else if ((tChar == "]") && (tLastChar == "[")) {
tQuotes.Pop();
}
else if ((tChar == "}") && (tLastChar == "{")) {
tQuotes.Pop();
}
if ((tChar == ")") && (tLastChar == "(")) {
tQuotes.Pop();
}
}
}
else {
tEscaped = false;
}
}
tWord = tWord+tChar;
}
if (tWord!= String.Empty) {tResults.Add(tWord);}
return (string[])tResults.ToArray(typeof(string));
}
}//Class:End
}
Nov 18 '05 #1
1 1680
Correction to class posted before:

There was a bug in ConvertFrom -- when fixed it solved problem a) mentioned
before.

Secondly, I modified this:
[PersistenceMode(PersistenceMode.Attribute )]

//[DesignerSerializationVisibility(DesignerSerializat ionVisibility.Visible )
]

public cCaptions Captions {get {return _Captions;}set{_Captions = value;}}

and tried every variation I can think of -- the only one that works is when
DesignerSerializationVisiblity is set to Content -- which negates the whole
use of the TypeConverter to string format I was going for....

And the wierd thing is that before, when I had just

[PersistenceMode(PersistenceMode.Attribute )]

it was saved as an attribute -- which is what I wanted. Now...nothing. Can't
get it back . gone. Nada.

If anybody can see what I bolloxed up -- thank you very very much!

Sky





using System;

using System.Web.UI.Design;

using System.Web;

using System.ComponentModel;

using System.Globalization;

using System.Reflection;

using System.Collections;

//http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/
html/vsnetpropbrow.asp

namespace XAct.Web.Controls {

public class TestConverter : ExpandableObjectConverter {

System.Type _Type = typeof(XAct.Web.Controls.LoginPanel.cCaptions);

public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
{

if (t == typeof(string)) {return true;}

return base.CanConvertFrom(context, t);

}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo info,object value) {

if (value is string) {

try {

string[] tParts;

tParts = SplitPlus((string) value, ";");

XAct.Web.Controls.LoginPanel.cCaptions o = new
XAct.Web.Controls.LoginPanel.cCaptions();//_Type.Assembly.CreateInstance(_Ty
pe.ToString());

string tKey = string.Empty;

string tValue = string.Empty;

int tPos = 0;

foreach (string tPart in tParts){

if (tPart == string.Empty){continue;}

tPos = tPart.IndexOf(':');

if (tPos == -1){

tPos = tPart.Length;

tKey = tPart;

tValue = string.Empty;

}else{

tKey = tPart.Substring(0,tPos);

tValue = tPart.Substring(tPos+1);

System.Reflection.PropertyInfo oPI =
_Type.GetProperty(tKey,BindingFlags.Instance | BindingFlags.Public |
System.Reflection.BindingFlags.IgnoreCase);

if (oPI != null){

oPI.SetValue(o,tValue,null);

}

}

}

o._NewUser_Email="MERDE EMAIL";

return o;

}

catch {throw new Exception("MERDE!");}

}

return base.ConvertFrom(context, info, value);

}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo
culture, object value, Type destType) {

object o=null;

try {

o = System.Convert.ChangeType(value,_Type);

}catch{}

if ((o !=null) && (destType == typeof(string))) {

try {

string tResult = string.Empty;

string tDivChar = string.Empty;

PropertyInfo[] oPIs = _Type.GetProperties();
foreach(PropertyInfo oPI in oPIs){

string tKey = oPI.Name;

object oVal = oPI.GetValue(o,null);

string tValue = string.Empty;

if (oVal != null){

tValue = oVal.ToString();

if (tValue != string.Empty){tResult += tDivChar + tKey + ":" + tValue;}

if (tDivChar == string.Empty){tDivChar = ";";}

}

}

return tResult;

}

catch {}

}

return base.ConvertTo(context, culture, value, destType);

}

// public override bool
GetStandardValuesSupported(System.ComponentModel.I TypeDescriptorContext
context){

// return false;

// }


/// <summary>

/// Split function that only splits if not within brackets or quotes.

/// </summary>

/// <param name="qString"></param>

/// <param name="qDivChar"></param>

/// <returns></returns>

public static string[] SplitPlus(string qString, string qDivChar) {

if (qDivChar== String.Empty){qDivChar = ",";}

ArrayList tResults = new ArrayList();

string tChar="";

string tWord = "";

bool tEscaped=false;

string tLastChar = "";

System.Collections.Stack tQuotes=new System.Collections.Stack();

for (int i=0;i<qString.Length;i++) {

tChar = qString[i].ToString();

if (tQuotes.Count == 0) {

//We are outside of quotes, so look for quote beginnings...

if ((tChar == "(") ||

(tChar == "{") ||

(tChar == "[") ||

(tChar == "'") ||

(tChar == "\"")) {

tQuotes.Push(tChar);

tLastChar=tChar;

}

if ((tChar == qDivChar)) {

tResults.Add(tWord);

tWord="";

tChar = "";

}

}

else {

//We are within quotes...need to look for close chars:

if (tEscaped ==false) {

if (tChar == "\\") {tEscaped=true;}

else {

tLastChar =(string)tQuotes.Peek();

if ((tChar == "\"") && (tChar == tLastChar)) {

tQuotes.Pop();

}

else if ((tChar == "\'") && (tChar == tLastChar)) {

tQuotes.Pop();

}

else if ((tChar == "]") && (tLastChar == "[")) {

tQuotes.Pop();

}

else if ((tChar == "}") && (tLastChar == "{")) {

tQuotes.Pop();

}

if ((tChar == ")") && (tLastChar == "(")) {

tQuotes.Pop();

}

}

}

else {

tEscaped = false;

}

}

tWord = tWord+tChar;

}

if (tWord!= String.Empty) {tResults.Add(tWord);}

return (string[])tResults.ToArray(typeof(string));

}

}//Class:End

}
Nov 18 '05 #2

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

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
3
by: Colin J. Williams | last post by:
Python advertises some basic service: C:\Python24>python Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> With...
7
by: Corepaul | last post by:
Missing Help Files When I enter "recordset" as the keyword and search the Visual Basic Help index, I get many topics of interest in the resulting list. But there isn't any information available...
5
by: Steve | last post by:
I have written a help file (chm) for a DLL and referenced it using Help.ShowHelp My expectation is that a developer using my DLL would be able to access this help file during his development time...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.