bwgc Posted March 4, 2004 Posted March 4, 2004 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 Quote
Denaes Posted March 4, 2004 Posted March 4, 2004 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. Quote
bwgc Posted March 4, 2004 Author Posted March 4, 2004 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 Quote
Denaes Posted March 5, 2004 Posted March 5, 2004 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 :) Quote
georgepatotk Posted March 5, 2004 Posted March 5, 2004 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. Quote George C.K. Low
bwgc Posted March 5, 2004 Author Posted March 5, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.