Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Any way to achieve Sticky reference?

Posted on 2004-09-25
6
206 Views
Last Modified: 2010-04-15
Here is an example to hopefully help clarify what I'm trying to do.  

I load a collection with objects (TestClass).  I then bind this collection to a datagrid.  I then also bind text a text box to the current row in the gird like this:
grdList.DataSource = myCollection;
_refClass = (TestClass)myCollection[grdList.CurrentRowIndex];                  

txtRefName.DataBindings.Add("Text",_refClass, "Name");

The obviously the variable _refClass is just acting as a pointer which holds a reference to the item in the collection and then that reference is bound to the textbox.  So if in the future _refClass is set to another item in the collection the TextBox will not reflect that change unless it is rebound.

However, what I'd like to know is, is there a way actually bind the textbox to _refClass so that any time _refClass is pointed to a new item in the collection the TextBox will reflect this without having to be rebound.

The scenerio above isn't a real project I'm working on, I just thought it was the easist way to explain what I am looking for.  I have also coded this into a simple project as an example.  If it will help anyone to understand the issue I can email the project to them.

Thanks so much for your time!
0
Comment
Question by:carpediem
  • 2
6 Comments
 
LVL 10

Expert Comment

by:ptmcomp
ID: 12151123
Try:
txtRefName.DataBindings.Add(new Binding("Text", myCollection, ""));
PS: It's not tested but I'm sure you have to bind to the collection. The CurrencyManager on the form will handle the row changing thing.
0
 
LVL 1

Author Comment

by:carpediem
ID: 12151159
Sorry, my example wasn't very clear (or was maybe to clear and thus hid my real question).

In more generic terms:
Say there is an object X in memory and then object Y just holds a reference to object X.

I then set object Z = object Y.  So now object Z just points to object X.  

What I would like to know is there any way in C# to handle a case where I now point object Y to object Q and I would like object Z to automatically point to object Q as well.

So instead of having object Z directly point to object X when I set object Z = object Y, object Z actually points to object Y so if I point object Y elsewhere object Z will automatically then point to that other object I've updated object Y to point to.

0
 
LVL 10

Accepted Solution

by:
ptmcomp earned 500 total points
ID: 12152333
This is done by the CurrencyManager which is stored in the BindingContext on the Form.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question