Results 1 to 7 of 7

Thread: How to get an index value from array and object

  1. #1
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default How to get an index value from array and object

    I'm trying to do a routine to update a control's property (the object is in an array) with an event, but I need the index value of that control in an array of identical controls (TextBoxes).

    The routine is an EventHandler that was bound to the TextChanged method. I now need a way to get the index value so I can update the right index in an array of strings.

    Using C# in Visual Studio.
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  2. #2
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    The routine is an EventHandler that was bound to the TextChanged method.
    Is it for the TextChanged method for all the text boxes

    TextChanged implies

    EventHandler(
    object sender,
    EventArgs e
    );

    Maybe you can 'for loop' through the array of control textboes until 'sender' equals and element in the array which can givr you index as well.

    ie
    for(int i=0; i < all_textboxes.count; i++)
    {
    if (all_textboxes[i].Equals(sender))
    break;
    }

    Equals is usually a 'reference' check on objects to see if the same object (instance of a class) is present
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  3. #3
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    Quote Originally Posted by crosswire View Post
    Is it for the TextChanged method for all the text boxes

    TextChanged implies

    EventHandler(
    object sender,
    EventArgs e
    );

    Maybe you can 'for loop' through the array of control textboes until 'sender' equals and element in the array which can givr you index as well.

    ie
    for(int i=0; i < all_textboxes.count; i++)
    {
    if (all_textboxes[i].Equals(sender))
    break;
    }

    Equals is usually a 'reference' check on objects to see if the same object (instance of a class) is present
    Thanks. Had set the TabIndex and then Name property to the index value when assigning, then read it in the routine. Thought that this method would be more inefficient as the amount of boxes grew but I think I'll change it.

    I'd first made a foreach loop to update everything when the control was closed, but that got sixed when the only event I could find was Disposed and the control data was destroyed by the time the method was called. Isn't there an event triggered when a control is going to be closed/disposed/destroyed?
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  4. #4
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Thanks. Had set the TabIndex and then Name property to the index value when assigning, then read it in the routine.
    Very nice idea. Perhaps mine is less effiient, I'm not sure.

    when the control was closed,
    how is done? Is there any clicking on the control to close it then maybe there is an alternative event you could use.

    Isn't there an event triggered when a control is going to be closed/disposed/destroyed?
    I'l try to check it out
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #5
    Join Date
    Oct 2005
    Posts
    745
    Rep Power
    0

    Default

    Quote Originally Posted by Skele Drew View Post
    Isn't there an event triggered when a control is going to be closed/disposed/destroyed?
    Are you not seeing a Disposing event?
    3.14159265358979323846264338327950288
    4197169399375105820974944592307816406
    28620899862803482534211706798 pi 101

  6. #6
    Join Date
    Jan 2009
    Posts
    2,404
    Rep Power
    0

    Default

    Quote Originally Posted by crosswire View Post
    how is done? Is there any clicking on the control to close it then maybe there is an alternative event you could use.
    Its a user control, basically composed of a TableLayoutPanel with a number of labels and textboxes added at runtime. I was seeking a way to call a routine when the control is about to be destroyed (so I'd grab and store the textbox .Text data at this time), but I can't find any events that facilitate this.

    Quote Originally Posted by recursion View Post
    Are you not seeing a Disposing event?
    Can't use the Disposed (and there's no Disposing) event; when it finally calls the routine, the textbox objects in the array have already been destroyed.
    Last edited by Skele Drew; May 5, 2009 at 10:36 PM.
    Rooted OnePlus 2 64GB Ed, Android 5.1.1 OxygenOS ; on teifin' AT&T's network; Rooted ASUS Transformer TF101 w/ dock, Android 5.1 KatKiss; Laptop: ASUS X550C, 2.0GHzx2, 8GB, 512GB SSD, Kubuntu 15.10;
    Facebook page: Skeleville Technology Solutions

  7. #7
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    im assuming is windows forms, there is a closing event on forms. or you can do the update when the user leaves the individual input control.

    the inputs are in a container, there is a leave event on this, put the button/control that triggers the 'commit' outside this container. this will effectively allow you to update just before the inputs are disposed.

    this sort of problem is easily defeated with data binding.
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •