How can you push a string to the EditBox control from say a button control. For instance, i would like to click on a button in the addin ribbon that would send a string to the EditBox text property.
Here are two ways to do it. One uses an event which keeps the button and edit box loosely coupled, the other uses a direct reference where the button updates the edit box directly. The loosely coupled is the most flexible but is a bit more work than simply updating the edit box text - and, if that is literally all u need, then its overkill.
public class DateTimeStringChangedEventArgs : EventBase { public string OldDateTimeString { get; private set; } public string NewDateTimeString { get; private set; } public DateTimeStringChangedEventArgs(string oldDateTimeString, string newDateTimeString) { OldDateTimeString = oldDateTimeString; NewDateTimeString = newDateTimeString; } } public class DateTimeStringChangedEvent : CompositePresentationEvent<DateTimeStringChangedEventArgs> { public static SubscriptionToken Subscribe( Action<DateTimeStringChangedEventArgs> action, bool keepSubscriberReferenceAlive = false) { return FrameworkApplication.EventAggregator.GetEvent<DateTimeStringChangedEvent>() .Register(action, keepSubscriberReferenceAlive); } public static void Unsubscribe(Action<DateTimeStringChangedEventArgs> subscriber) { FrameworkApplication.EventAggregator.GetEvent<DateTimeStringChangedEvent>() .Unregister(subscriber); } public static void Unsubscribe(SubscriptionToken token) { FrameworkApplication.EventAggregator.GetEvent<DateTimeStringChangedEvent>() .Unregister(token); } internal static void Publish(DateTimeStringChangedEventArgs payload) { FrameworkApplication.EventAggregator.GetEvent<DateTimeStringChangedEvent>() .Broadcast(payload); } } internal class Module1 : Module { private static Module1 _this = null; private string _dateTimeString = DateTime.Now.ToString("G", CultureInfo.CurrentCulture); public string DateTimeString { get { return _dateTimeString; } set { var args = new DateTimeStringChangedEventArgs( _dateTimeString, value); _dateTimeString = value; //Raise the event DateTimeStringChangedEvent.Publish(args); } } public EditBoxDirectApproach EditBoxDirectApproach { get; set; } = null; ... } internal class Button1 : Button { protected override void OnClick() { var date_string = DateTime.Now.ToString("G", CultureInfo.CurrentCulture); //Indirect, loose-coupling Module1.Current.DateTimeString = date_string; //Direct, direct-coupled Module1.Current.EditBoxDirectApproach.Text = date_string; } } internal class EditBoxIndirectApproach : EditBox { public EditBoxIndirectApproach() { Initialize(); } private bool _ignoreEvents = false; private void Initialize() { //This will invoke OnTextChange which is ok //as we havent subscribed to the event yet this.Text = Module1.Current.DateTimeString; DateTimeStringChangedEvent.Subscribe((args) => { _ignoreEvents = true; try { //Invokes OnTextChange this.Text = args.NewDateTimeString; } finally { _ignoreEvents = false; } }); } protected override void OnTextChange(string text) { //prevent (infinite) recursion if (_ignoreEvents) return; Module1.Current.DateTimeString = text; base.OnTextChange(text); } } internal class EditBoxDirectApproach : EditBox { public EditBoxDirectApproach() { Module1.Current.EditBoxDirectApproach = this; this.Text = DateTime.Now.ToString("G", CultureInfo.CurrentCulture); } }
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.