473,326 Members | 2,095 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.

Usercontrol Property loses value at runtime - I don't get it?

Hi y'all

I've got a custom control that is inherited from a normal TextBox. To that,
I've added a new property that I call "Units", Which is a collection class
containing members of a "Unit" class.

When I put my Textbox on the form, I can access the "Units" property from
the IDE's property grid, use the collection designer and add new "Unit"
members to the class.

So far, so good, but when I run my project and query the "Units" collection,
it's empty. Afterwards, I can see in the Collection editor that my "Unit"
members are still there.

If I add the "Unit" members programmatically at runtime, there's no problem.

It must be something I'm missing because I'm not good enough at OOP, but
what?

Can somebody give me a clue, please?

TIA,
Johnny J.
Apr 16 '07 #1
9 3155
Probably the Unit collection get initialize by the time it run..

Static attribute for the Units Collection property would workout for you I
guess..

Nirosh.

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
Hi y'all

I've got a custom control that is inherited from a normal TextBox. To
that, I've added a new property that I call "Units", Which is a collection
class containing members of a "Unit" class.

When I put my Textbox on the form, I can access the "Units" property from
the IDE's property grid, use the collection designer and add new "Unit"
members to the class.

So far, so good, but when I run my project and query the "Units"
collection, it's empty. Afterwards, I can see in the Collection editor
that my "Unit" members are still there.

If I add the "Unit" members programmatically at runtime, there's no
problem.

It must be something I'm missing because I'm not good enough at OOP, but
what?

Can somebody give me a clue, please?

TIA,
Johnny J.

Apr 16 '07 #2
Nope, didn't work...

Any other suggestions?

Cheers,
Johnny J.

"L.W.C. Nirosh" <ni****@sabre.comwrote in message
news:%2*****************@TK2MSFTNGP06.phx.gbl...
Probably the Unit collection get initialize by the time it run..

Static attribute for the Units Collection property would workout for you I
guess..

Nirosh.

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
>Hi y'all

I've got a custom control that is inherited from a normal TextBox. To
that, I've added a new property that I call "Units", Which is a
collection class containing members of a "Unit" class.

When I put my Textbox on the form, I can access the "Units" property from
the IDE's property grid, use the collection designer and add new "Unit"
members to the class.

So far, so good, but when I run my project and query the "Units"
collection, it's empty. Afterwards, I can see in the Collection editor
that my "Unit" members are still there.

If I add the "Unit" members programmatically at runtime, there's no
problem.

It must be something I'm missing because I'm not good enough at OOP, but
what?

Can somebody give me a clue, please?

TIA,
Johnny J.


Apr 16 '07 #3
Check where you have initialized the collection.. becos I think you are
initializin gthe collection as the application starts..

Nirosh.

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:OI**************@TK2MSFTNGP03.phx.gbl...
Nope, didn't work...

Any other suggestions?

Cheers,
Johnny J.

"L.W.C. Nirosh" <ni****@sabre.comwrote in message
news:%2*****************@TK2MSFTNGP06.phx.gbl...
>Probably the Unit collection get initialize by the time it run..

Static attribute for the Units Collection property would workout for you
I guess..

Nirosh.

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:ey**************@TK2MSFTNGP02.phx.gbl...
>>Hi y'all

I've got a custom control that is inherited from a normal TextBox. To
that, I've added a new property that I call "Units", Which is a
collection class containing members of a "Unit" class.

When I put my Textbox on the form, I can access the "Units" property
from the IDE's property grid, use the collection designer and add new
"Unit" members to the class.

So far, so good, but when I run my project and query the "Units"
collection, it's empty. Afterwards, I can see in the Collection editor
that my "Unit" members are still there.

If I add the "Unit" members programmatically at runtime, there's no
problem.

It must be something I'm missing because I'm not good enough at OOP, but
what?

Can somebody give me a clue, please?

TIA,
Johnny J.



Apr 16 '07 #4
In Visual Basic, you normally have to save all the properties in the PropBag
using PropBag.ReadProperty & PropBag.WriteProperty for persistancy.

I don't know how you do in C#, though?

Cheers,

Johnny J.
Apr 16 '07 #5
Search for all References of your Units Property (search for the
private Units member too) and check for
assignment expressions or something like Clear(); Check the designer
generated code whether the serealized
unitscollection is loaded from the resourcefile.

Apr 16 '07 #6
I Still haven't found the solution...

Surely, somebody out there must have experience in control creation and know
the answer... :-)

/Johnny J.


"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:O5**************@TK2MSFTNGP03.phx.gbl...
In Visual Basic, you normally have to save all the properties in the
PropBag using PropBag.ReadProperty & PropBag.WriteProperty for
persistancy.

I don't know how you do in C#, though?

Cheers,

Johnny J.


Apr 17 '07 #7
Johnny Jörgensen wrote:
I Still haven't found the solution...

Surely, somebody out there must have experience in control creation and know
the answer... :-)

/Johnny J.


"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:O5**************@TK2MSFTNGP03.phx.gbl...
>In Visual Basic, you normally have to save all the properties in the
PropBag using PropBag.ReadProperty & PropBag.WriteProperty for
persistancy.

I don't know how you do in C#, though?

Cheers,

Johnny J.


can you post some code ?
Apr 18 '07 #8
can you post some code ?

Sure. Below is the complete code for my control this far - I'm almost done,
but I have this problem: My control contains a collection called Units that
consists of X number of Unit Members. I can see the collection in the
control's properties in the property explorer, and I can add the unit
elements to the unit property using the property explorers collection
editor. But when I run the project, the Unit members are gone.

I'm sure the answer lies in serialization, but I don't know how to do it. In
VB6, you used to use an object called a Property Bag to store properties
between design and runtime. But that's changed in .NET. I found the
following article on MSDN, but I haven't had a chance to look closer at it
(and apply it to C#) yet:

http://msdn2.microsoft.com/en-us/lib...=printer).aspx

Cheers,
Johnny J.


******** CODE BEGINS HERE:

using System;

using System.ComponentModel;

using System.Collections;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

using System.Runtime.Serialization;

namespace UserControls

{

public delegate void UnitErrorDelegate(object sender, UnitErrorEventArgs e);
public partial class FoBNumericUnitTextBox : TextBox

{

public event UnitErrorDelegate ErrorEncountered;
private string m_FancyText = "";

private string m_SimpleText = "";
private bool m_Focused = false;

private bool m_NonNumberEntered = false;

static private Units m_Units = null;
public Units Units

{

get

{

if (m_Units == null)

{

m_Units = new Units();

}

return m_Units;

}

set

{

m_Units = value;

}

}

public string FancyText

{

get { return m_FancyText; }

set {

m_FancyText = value;

ConvertToSimpleText(m_FancyText);

if (m_Focused)

{ base.Text = m_SimpleText; }

else

{ base.Text = m_FancyText; }

}

}
public override string Text

{

get

{

return m_SimpleText;

}

set

{

m_SimpleText = value;

ConvertToFancyText(m_SimpleText);

if (m_Focused)

{ base.Text = m_SimpleText; }

else

{ base.Text = m_FancyText; }

}

}

public FoBNumericUnitTextBox()

{

InitializeComponent();

}

public FoBNumericUnitTextBox(IContainer container)

{

container.Add(this);

InitializeComponent();

}

private void OnFoBNumericUnitTextBox_Enter(object sender, EventArgs e)

{

m_Focused = true;

base.Text = m_SimpleText;

}

private void OnFoBNumericUnitTextBox_Leave(object sender, EventArgs e)

{

m_Focused = false;

ConvertToFancyText(base.Text);

base.Text = m_FancyText;

}
private void ConvertToFancyText(string simpleText)

{

// TODO: Convert

}

private void ConvertToSimpleText(string fancyText)

{

// TODO: Convert

}

private void OnFoBNumericUnitTextBox_KeyDown(object sender, KeyEventArgs e)

{

// Initialize the flag to false.

m_NonNumberEntered = false;

// Determine whether the keystroke is a number from the top of the keyboard.

if (e.KeyCode < Keys.D0 || e.KeyCode Keys.D9)

{

// Determine whether the keystroke is a number from the keypad.

if (e.KeyCode < Keys.NumPad0 || e.KeyCode Keys.NumPad9)

{

// Determine whether the keystroke is a backspace.

if (e.KeyCode != Keys.Back)

{

// TODO: Check för separatorchar
// A non-numerical keystroke was pressed.

// Set the flag to true and evaluate in KeyPress event.

m_NonNumberEntered = true;

}

}

}

}

private void OnFoBNumericUnitTextBox_KeyPress(object sender,
KeyPressEventArgs e)

{

// Check for the flag being set in the KeyDown event.

if (m_NonNumberEntered == true)

{

// Stop the character from being entered into the control since it is
non-numerical.

e.Handled = true;

}

}

}
public class Units : CollectionBase

{

private string m_DefaultSeparatorChar = ":";

public string DefaultSeparatorChar

{

get { return m_DefaultSeparatorChar; }

set { m_DefaultSeparatorChar = value; }

}
public Unit Add()

{

Unit newUnit = new Unit();

newUnit.SeparatorChar = m_DefaultSeparatorChar;

newUnit.UnitText= "Unit" + Convert.ToString(List.Count + 1);

List.Add(newUnit);

return newUnit;

}
new public void Clear()

{

List.Clear();

}
public void Remove(Unit oldUnit)

{

List.Remove(oldUnit);

}
public Units()

{

}
new public Int32 Count()

{

return List.Count;

}
public Unit this[int unitIndex]

{

get

{

try

{

return (Unit)List[unitIndex];

}

catch

{

//Ignore

return null;

}

}

set

{

List[unitIndex] = value;

}

}

}

public class UnitErrorEventArgs

{

private Int32 m_ErrorIndex;

private string m_ErrorText;

private Unit m_ErrorUnit;
public Int32 ErrorIndex

{

get { return m_ErrorIndex; }

set { m_ErrorIndex = value; }

}
public string ErrorText

{

get { return m_ErrorText; }

set { m_ErrorText = value; }

}
public Unit ErrorUnit

{

get { return m_ErrorUnit; }

set { m_ErrorUnit = value; }

}

}
public class Unit

{

private string m_UnitText = "UnitText";

private string m_SeparatorChar = ":";

private Int32 m_MaxValue = 0;

private Int32 m_MinValue = 0;

private bool m_CheckValueRange = false;

public bool CheckValueRange

{

get { return m_CheckValueRange; }

set { m_CheckValueRange = value; }

}
public string SeparatorChar

{

get { return m_SeparatorChar; }

set { m_SeparatorChar = value; }

}

public string UnitText

{

get { return m_UnitText; }

set { m_UnitText = value; }

}

public Int32 MinValue

{

get { return m_MinValue; }

set { m_MinValue = value; }

}
public Int32 MaxValue

{

get { return m_MaxValue; }

set { m_MaxValue = value; }

}

}
}
Apr 18 '07 #9
As mentioned, I'm sure the answer lies in serialization. I've read up on
some articles on that, but there is a problem:

In VB6, I could save and retrieve control settings from within the control
using the PropBag object. According to MSDN, the PropBag is replaced by
serialization, but all the articles I've read shows how you save an instance
of your object from the application itself or from another class - not from
the object itself.

And I want to keep all code inside my control object and not have to do
anything from the outside.

How the heck does it work?

/Johnny J.

"Johnny Jörgensen" <jo**@altcom.sewrote in message
news:uW**************@TK2MSFTNGP02.phx.gbl...
>can you post some code ?

Sure. Below is the complete code for my control this far - I'm almost
done, but I have this problem: My control contains a collection called
Units that consists of X number of Unit Members. I can see the collection
in the control's properties in the property explorer, and I can add the
unit elements to the unit property using the property explorers collection
editor. But when I run the project, the Unit members are gone.

I'm sure the answer lies in serialization, but I don't know how to do it.
In VB6, you used to use an object called a Property Bag to store
properties between design and runtime. But that's changed in .NET. I found
the following article on MSDN, but I haven't had a chance to look closer
at it (and apply it to C#) yet:

http://msdn2.microsoft.com/en-us/lib...=printer).aspx

Cheers,
Johnny J.


******** CODE BEGINS HERE:

using System;

using System.ComponentModel;

using System.Collections;

using System.Collections.Generic;

using System.Diagnostics;

using System.Text;

using System.Windows.Forms;

using System.Runtime.Serialization;

namespace UserControls

{

public delegate void UnitErrorDelegate(object sender, UnitErrorEventArgs
e);
public partial class FoBNumericUnitTextBox : TextBox

{

public event UnitErrorDelegate ErrorEncountered;
private string m_FancyText = "";

private string m_SimpleText = "";
private bool m_Focused = false;

private bool m_NonNumberEntered = false;

static private Units m_Units = null;
public Units Units

{

get

{

if (m_Units == null)

{

m_Units = new Units();

}

return m_Units;

}

set

{

m_Units = value;

}

}

public string FancyText

{

get { return m_FancyText; }

set {

m_FancyText = value;

ConvertToSimpleText(m_FancyText);

if (m_Focused)

{ base.Text = m_SimpleText; }

else

{ base.Text = m_FancyText; }

}

}
public override string Text

{

get

{

return m_SimpleText;

}

set

{

m_SimpleText = value;

ConvertToFancyText(m_SimpleText);

if (m_Focused)

{ base.Text = m_SimpleText; }

else

{ base.Text = m_FancyText; }

}

}

public FoBNumericUnitTextBox()

{

InitializeComponent();

}

public FoBNumericUnitTextBox(IContainer container)

{

container.Add(this);

InitializeComponent();

}

private void OnFoBNumericUnitTextBox_Enter(object sender, EventArgs e)

{

m_Focused = true;

base.Text = m_SimpleText;

}

private void OnFoBNumericUnitTextBox_Leave(object sender, EventArgs e)

{

m_Focused = false;

ConvertToFancyText(base.Text);

base.Text = m_FancyText;

}
private void ConvertToFancyText(string simpleText)

{

// TODO: Convert

}

private void ConvertToSimpleText(string fancyText)

{

// TODO: Convert

}

private void OnFoBNumericUnitTextBox_KeyDown(object sender, KeyEventArgs
e)

{

// Initialize the flag to false.

m_NonNumberEntered = false;

// Determine whether the keystroke is a number from the top of the
keyboard.

if (e.KeyCode < Keys.D0 || e.KeyCode Keys.D9)

{

// Determine whether the keystroke is a number from the keypad.

if (e.KeyCode < Keys.NumPad0 || e.KeyCode Keys.NumPad9)

{

// Determine whether the keystroke is a backspace.

if (e.KeyCode != Keys.Back)

{

// TODO: Check för separatorchar
// A non-numerical keystroke was pressed.

// Set the flag to true and evaluate in KeyPress event.

m_NonNumberEntered = true;

}

}

}

}

private void OnFoBNumericUnitTextBox_KeyPress(object sender,
KeyPressEventArgs e)

{

// Check for the flag being set in the KeyDown event.

if (m_NonNumberEntered == true)

{

// Stop the character from being entered into the control since it is
non-numerical.

e.Handled = true;

}

}

}
public class Units : CollectionBase

{

private string m_DefaultSeparatorChar = ":";

public string DefaultSeparatorChar

{

get { return m_DefaultSeparatorChar; }

set { m_DefaultSeparatorChar = value; }

}
public Unit Add()

{

Unit newUnit = new Unit();

newUnit.SeparatorChar = m_DefaultSeparatorChar;

newUnit.UnitText= "Unit" + Convert.ToString(List.Count + 1);

List.Add(newUnit);

return newUnit;

}
new public void Clear()

{

List.Clear();

}
public void Remove(Unit oldUnit)

{

List.Remove(oldUnit);

}
public Units()

{

}
new public Int32 Count()

{

return List.Count;

}
public Unit this[int unitIndex]

{

get

{

try

{

return (Unit)List[unitIndex];

}

catch

{

//Ignore

return null;

}

}

set

{

List[unitIndex] = value;

}

}

}

public class UnitErrorEventArgs

{

private Int32 m_ErrorIndex;

private string m_ErrorText;

private Unit m_ErrorUnit;
public Int32 ErrorIndex

{

get { return m_ErrorIndex; }

set { m_ErrorIndex = value; }

}
public string ErrorText

{

get { return m_ErrorText; }

set { m_ErrorText = value; }

}
public Unit ErrorUnit

{

get { return m_ErrorUnit; }

set { m_ErrorUnit = value; }

}

}
public class Unit

{

private string m_UnitText = "UnitText";

private string m_SeparatorChar = ":";

private Int32 m_MaxValue = 0;

private Int32 m_MinValue = 0;

private bool m_CheckValueRange = false;

public bool CheckValueRange

{

get { return m_CheckValueRange; }

set { m_CheckValueRange = value; }

}
public string SeparatorChar

{

get { return m_SeparatorChar; }

set { m_SeparatorChar = value; }

}

public string UnitText

{

get { return m_UnitText; }

set { m_UnitText = value; }

}

public Int32 MinValue

{

get { return m_MinValue; }

set { m_MinValue = value; }

}
public Int32 MaxValue

{

get { return m_MaxValue; }

set { m_MaxValue = value; }

}

}
}


Apr 19 '07 #10

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

Similar topics

4
by: DraguVaso | last post by:
Hi, I'm having some weird behaviour, and I can't find any solution to this. I made a UserControl (uclCommands) which contains a Toolbar (ToolBar1) with Modifiers = Public. I put this...
2
by: bill yeager | last post by:
When trying to run my web project, I get the following error: Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the...
4
by: Joe Molloy | last post by:
Hi, I have a usercontrol containing mutually exclusive radiobuttons and I am trying to use a public function to set a property of the usercontrol based on which one is checked. However when I...
1
by: Will Gillen | last post by:
I know this has probably been asked before, but I can't seem to find a solid answer in any of the archives. First, before my question, please forgive my limited knowledge of the event lifecycle...
3
by: Don | last post by:
I've created a custom UserControlwithin which I have placed a Panel. I've changed the Panel's "Modifier" property to Public so that it appears in the Properties Window of the UserControl. This...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 This the ObjectDataSource in my UserControl, <asp:ObjectDataSource ID="odsMessage" runat="server" SelectMethod="ExecuteMessage" TypeName="AH.MyNetwork.BLL.Network.Message">...
5
by: Blue | last post by:
We have a custom word processing type editor built with C# and .NET 2.0 and we need to support typing in languages other than English. I want to be able to use the Windows IME to enter in text...
6
by: MeowCow | last post by:
I have created a UserControl that encapsulates a third party data grid. My goal was to create my own DataSource and DataMember properties that forward the binding to the third party grid, then use...
1
by: RvGrah | last post by:
I have a usercontrol written in C# that exposes a public property of type double. This control is used in a program written in VB.net. If I set the value to 66.6667, it stays fine, I can break...
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
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: 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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.