Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0.NET4
Hi Guys
 
I created a window phone application in-which I created a Image control from the code behind in my window phone application.. I placed all the automatically generated image control in a stack-panel, i added a hold event to the image control, but the problem is how do i access the properties of the selected control that fired the event.. Thanks
 
private void ProcessPhoto(PhotoResult phtres)
        {
            if (phtres.TaskResult == TaskResult.OK && phtres.ChosenPhoto != null)
            {
                for (int i = 0; i < arrstream.Count(); i++)
                {
                    if (arrstream[i] == null)
                    {
                        arrstream[i] = phtres.ChosenPhoto;
                        picocunt++;
                        WriteableBitmap wb = new WriteableBitmap(PictureDecoder.DecodeJpeg(arrstream[i]));
                        var img = new Image();
                        img.Name = "picbox" + i.ToString();
                        img.Height = 80;
                        img.Width = 80;
                        img.Margin = new Thickness(0, 0, 5, 0);
                        img.Source = wb;
                        Photo_Pan.Children.Add(img);
                        img.Hold += new EventHandler<System.Windows.Input.GestureEventArgs>(img_Hold);
                        break;
                    }
                }
                ArrCount.Text = string.Format("{0} Media Slot Remaining", arrstream.Length - picocunt);
 
                progressBar1.Visibility = Visibility.Collapsed;
 
                
            }
        }
 
//Event to be fired when any of the image control is pressed..
        void img_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            //Image img = new Image();
            //Photo_Pan.Children.IndexOf(img);
            //MessageBox.Show(();
        }
 
Above is my code sample
Posted 4 Jul '12 - 9:22
Edited 4 Jul '12 - 9:43

Comments
Sergey Alexandrovich Kryukov - 4 Jul '12 - 15:36
What is your problem? I wonder, how can it be a problem, taking into account that you already successfully created and added controls to your UI? --SA
IamBlessed - 4 Jul '12 - 15:38
The problem is that i want want a user select any of the programatically created image controls it fires a event, i also want to be able to get the particular control that triggered the event
Wes Aday - 4 Jul '12 - 17:15
The control that fired the event is passed as the sender parameter.

1 solution

This is quite an artificial problem. How come it could be a problem? If you need to access to something, you need to address it by name, right? But it does not have to be direct. As you create and insert controls somewhere in the very beginning, but you need to access them from time to time, those names should be the names of some class/structure instance members, right? Most natural place should be the class of your window or a form which host your stack panel with forms.
 
So, when you first create a control, its constructor always return the reference to it. You already used this reference to insert a control in a panel. Assign this reference to some member of your class, a field or a property; it depends on how you want to use it.
 
It was about direct access. This is not what you always want. Sometime you have many controls of the similar purpose which you want to keep in an array or some other collection. In this way, you should only keep a reference to the array of some collection as an instance member of your class. In this case, the references to individual controls will be accessed by index, or by some key, if this is a key-value based collection. Please see:
http://msdn.microsoft.com/en-us/library/9b9dty7d.aspx[^],
http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx[^].
 
—SA
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 523
1 Mahesh Bailwal 393
2 Maciej Los 205
3 Aarti Meswania 200
4 Tadit Dash 145
0 Sergey Alexandrovich Kryukov 9,607
1 OriginalGriff 7,214
2 CPallini 3,943
3 Rohan Leuva 3,261
4 Maciej Los 2,758


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 4 Jul 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid