Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

As opposed to binding to a datasource, how do you bind a textbox.text to a private string variable and get it to display the proper text when the value of the string variable changes?

 

I'm doing:

string v1="value";

textbox1.Binding.Add("Text",v1,"");

//This sets the initial value, just fine.

v1 = "value changed";

// does nothing

 

Thanks - Geoff

Posted

When you bind you a datasource, the index (currency counter?) changes, which triggers an event.

 

As far as I know, there isn't anything to trigger an event whenever a variable is changed.

 

The best bet would be to have another textbox which isn't visible and use that as a variable to hold text. Then on the 'variable' textboxs' changetext event, set the text to your first textbox.

 

Or if you want the variable to ALWAYS be the EXACT same as the textbox, just use the textbox.text instead of a string variable.

Posted

I suppose you could also create a class with a "value changed" event, and do it that way. I just thought maybe I was missing an obvious simple solution.

 

Thanks - Geoff

Posted

No, a class with custom events or a custom control seem to be the way to go.

 

If you do a class on that, could you post it? I'm a bit sketchy on custom events and would love to see how you do it :)

Posted
To create a class for this solution would be a better choice since it is re-usable. Anyhow, if you are just using it for one time, then forget about the class. Do as proposed by Denaes. Please share with me as well if you really create a class for it.

George C.K. Low

Posted

As you requested, I am posting an example. I am by no means an expert at this - there may be many things wrong with it, but it seems to work.

 

In this example, we're trying to get textBox3.Text to refresh automatically as the value of a variable changes.

 

=====================================================

//OK, here's the class...

public class BoundText

{

public delegate void TextUpdatedEventHandler(object sender, EventArgs e);

public event TextUpdatedEventHandler TextUpdatedEvent;

private string _strVal;

public BoundText()

{

StrVal="";

}

public BoundText(string p_StrVal)

{

StrVal = p_StrVal;

}

public string StrVal

{

get{return _strVal;}

set

{

_strVal = value;

OnBoundTextChanged();

}

}

protected virtual void OnBoundTextChanged()

{

if(TextUpdatedEvent!= null)

TextUpdatedEvent(this, EventArgs.Empty);

}

 

}

 

//In your code where you instantiate the new object

boundText1 = new BoundText();

boundText1.TextUpdatedEvent +=new BoundText.TextUpdatedEventHandler(boundText1_TextUpdatedEvent);

 

 

//then somewhere define your event

private void boundText1_TextUpdatedEvent(object sender, System.EventArgs e)

{

textBox3.Text = ((BoundText)sender).StrVal;

}

=============================================================

 

Thanks for you feedback - Geoff

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...