Properties and Events

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

I'm running a small C# app and I am trying to accomplish the following:

//Presentation Tier
txtMyTextBox.text = MyObject.SomeProperty;



//Business Tier

class MyObject
{
.....
public string SomeProperty
{
....
}

public event System.EventHandler SomeProperty_OnChange;
.....
}

What I want is when anything modifies the SomeProperty property, for the
PresentationTier to be pushed that data.

The class the SomeProperty belongs to runs many loops, and I want the
property text changing each time it hits a new loop to show the new data in
the textbox without the Presentation Tier having to do any of the work.

I did this in VB a long time ago, and totally forget how to do it haha.

Any help would be very appreciated.
 
Ryan,

Instead of naming your event SomeProperty_OnChange, name it
SomePropertyChanged. This will cause data bindings to update when you fire
the event.

Hope this helps.
 
Whenever you set your property you'll get an event.

public class MyObject {
public event EventHandler SomeProperty_Changed;
public string SomeProperty {
get { return this.someProperty; }
set { if ( this.someProperty != value ) { this.someProperty = value;
OnSomePropertyChanged(null); }
}

private void OnSomePropertyChanged(EventArgs e) {
if ( SomeProperty_Changed != null ) { SomePropertyChanged(this, e); }
}
}
 
Ok,

I'm running:
public string ConnectedTo
{
get
{
return p_strServer;
}
set
{
p_strServer = value;
this.ServerChanged(this,null);
}
}

public event System.EventHandler ServerChanged;

I'm getting errors when I hit this.ServerChanged(this, null);

And to fire it, is that going:

p_strServer = "NewServer"
this.ServerChanged;

?



Nicholas Paldino said:
Ryan,

Instead of naming your event SomeProperty_OnChange, name it
SomePropertyChanged. This will cause data bindings to update when you fire
the event.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan Ternier said:
I'm running a small C# app and I am trying to accomplish the following:

//Presentation Tier
txtMyTextBox.text = MyObject.SomeProperty;



//Business Tier

class MyObject
{
....
public string SomeProperty
{
....
}

public event System.EventHandler SomeProperty_OnChange;
....
}

What I want is when anything modifies the SomeProperty property, for the
PresentationTier to be pushed that data.

The class the SomeProperty belongs to runs many loops, and I want the
property text changing each time it hits a new loop to show the new data
in
the textbox without the Presentation Tier having to do any of the work.

I did this in VB a long time ago, and totally forget how to do it haha.

Any help would be very appreciated.
 
Ok I tried this,

In my Class I tried setting the Property by:
This.MyProperty = strURL[2];

MyProperty got the value of the string, but didn't throw back to my original
form.
My form load has the following code:

label1.DataBindings.Add(new Binding("Text",MyClass,"MyProperty"));

It still doesn't change on the form when the property changes in the class.


lblError.DataBindings.Add(new Binding("Text",objReminder,"ConnectedTo"));
 
Ryan,

You want to name your event ConnectedToChanged:

public event System.EventHandler ConnectedToChanged;

Then, you would fire the ConnectedToChanged event when the property
changed, like so:

public string ConnectedTo
{
get
{
return p_strServer;
}
set
{
p_strServer = value;

// You should check for null here, as you might not have any
listeners. You should
// Also pass EventArgs.Empty.
this.ConnectedToChanged(this, null);
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan Ternier said:
Ok,

I'm running:
public string ConnectedTo
{
get
{
return p_strServer;
}
set
{
p_strServer = value;
this.ServerChanged(this,null);
}
}

public event System.EventHandler ServerChanged;

I'm getting errors when I hit this.ServerChanged(this, null);

And to fire it, is that going:

p_strServer = "NewServer"
this.ServerChanged;

?



in
message news:[email protected]...
Ryan,

Instead of naming your event SomeProperty_OnChange, name it
SomePropertyChanged. This will cause data bindings to update when you fire
the event.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan Ternier said:
I'm running a small C# app and I am trying to accomplish the following:

//Presentation Tier
txtMyTextBox.text = MyObject.SomeProperty;



//Business Tier

class MyObject
{
....
public string SomeProperty
{
....
}

public event System.EventHandler SomeProperty_OnChange;
....
}

What I want is when anything modifies the SomeProperty property, for
the
PresentationTier to be pushed that data.

The class the SomeProperty belongs to runs many loops, and I want the
property text changing each time it hits a new loop to show the new
data
in
the textbox without the Presentation Tier having to do any of the work.

I did this in VB a long time ago, and totally forget how to do it haha.

Any help would be very appreciated.
 
Yes that worked!

Thank you very Much.
Nicholas Paldino said:
Ryan,

You want to name your event ConnectedToChanged:

public event System.EventHandler ConnectedToChanged;

Then, you would fire the ConnectedToChanged event when the property
changed, like so:

public string ConnectedTo
{
get
{
return p_strServer;
}
set
{
p_strServer = value;

// You should check for null here, as you might not have any
listeners. You should
// Also pass EventArgs.Empty.
this.ConnectedToChanged(this, null);
}
}


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan Ternier said:
Ok,

I'm running:
public string ConnectedTo
{
get
{
return p_strServer;
}
set
{
p_strServer = value;
this.ServerChanged(this,null);
}
}

public event System.EventHandler ServerChanged;

I'm getting errors when I hit this.ServerChanged(this, null);

And to fire it, is that going:

p_strServer = "NewServer"
this.ServerChanged;

?



in
message news:[email protected]...
Ryan,

Instead of naming your event SomeProperty_OnChange, name it
SomePropertyChanged. This will cause data bindings to update when you fire
the event.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm running a small C# app and I am trying to accomplish the following:

//Presentation Tier
txtMyTextBox.text = MyObject.SomeProperty;



//Business Tier

class MyObject
{
....
public string SomeProperty
{
....
}

public event System.EventHandler SomeProperty_OnChange;
....
}

What I want is when anything modifies the SomeProperty property, for
the
PresentationTier to be pushed that data.

The class the SomeProperty belongs to runs many loops, and I want the
property text changing each time it hits a new loop to show the new
data
in
the textbox without the Presentation Tier having to do any of the work.

I did this in VB a long time ago, and totally forget how to do it haha.

Any help would be very appreciated.
 
Back
Top