0
votes

I have a Custom User Control in the page. In this Custom User Control there are labels, DELETE ITEM Button and Quantity Textbox. In my page there is a ListBox which gets items from list, list is generated from local storage.

Now When I click on DELETE ITEM Button, It Goes to ListBox_SelectionChanged and takes the Selectedindex and further goes to DeleteButton_Tap and provides Selectedindex to it and deletes the item.

Now the problem is, if i click inside Quantity Textbox, it does not first goto ListBox_SelectionChanged to get SelectedIndex of item you want to update the quantity. but it directly goes to QuantityBox_TextChanged inside Custom User Control.

How can i send the SelectedIndex of the item of which i am trying to change the quantity?

EDIT-----------------------------CODE of Custom Control//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace XMLParsing
{
    public partial class CustomCartFavControl : UserControl
    {
        public CustomCartFavControl()
        {
            InitializeComponent();
        }
        Singleton singletonInstance = Singleton.mft;


        private const string strConnectionString = @"isostore:/MFTDB9.sdf";


        private void deleteItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {


            //Confirm:
            MessageBoxResult resultDelete = MessageBox.Show("Delete This Item From Cart?", "Cart:", MessageBoxButton.OKCancel);
            if (resultDelete == MessageBoxResult.OK)
            {
                using (MFTDataContext MFTdb = new MFTDataContext(strConnectionString))
                {
                    IQueryable<Database> ListQuery = from Item in MFTdb.MFTCart where Item.ProductUniqueID == singletonInstance.CartItemIDs[singletonInstance.ItemToChange] select Item;
                    Database itemRemove = ListQuery.FirstOrDefault();
                    MFTdb.MFTCart.DeleteOnSubmit(itemRemove);
                    MFTdb.SubmitChanges();

                }
                singletonInstance.somethingDeletedFromCart = true;

            }
            else if (resultDelete == MessageBoxResult.Cancel)
            {
                singletonInstance.somethingDeletedFromCart = false;



            }


           //END 
        }



        private void QuantityBoxLabel_TextChanged_1(object sender, TextChangedEventArgs e)
        {
            if (QuantityBoxLabel.Text != "")
            {
                singletonInstance.QuantityChanged = int.Parse(QuantityBoxLabel.Text);
            }

        }

        private void QuantityBoxLabel_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
           //Update Cart
            if (singletonInstance.QuantityOriginal == singletonInstance.QuantityChanged)
            {
            //Do Nothing! Quantity Not Changed
                singletonInstance.SomethingChanged = false;
            }
            else
            {
                //Work here for Quantity Update etc code:
                singletonInstance.SomethingChanged = true;
                //((Cart)this.Page).UpdateCartMethod();
                if (singletonInstance.QuantityChanged == 0)
                {
                    // new quantity =0
                    MessageBox.Show("You Didn't Enter A Valid Quantity. Quantity Changed Back To: " + singletonInstance.QuantityOriginal);
                    QuantityBoxLabel.Focus();
                }
                else
                {
                    if (singletonInstance.UpdatedSuccessfully == false)
                    {

                        // new quantity !=0
                        //Test on Product ID:519
                        // Chnange to dynamic via SlectionIndex or anything..

                        //DISABLE FOR TESTING:
                        //using (MFTDataContext MFTdb = new MFTDataContext(strConnectionString))
                        //{
                        //    var itemToChange = (from item in MFTdb.MFTCart
                        //                        where item.ProductID == singletonInstance.ItemToChange
                        //                        select item).Single();

                        //    itemToChange.ProductQuantity = singletonInstance.QuantityChanged;
                        //    itemToChange.ProductTotalPrice = singletonInstance.QuantityChanged * itemToChange.ProductPrice;
                        //    MFTdb.SubmitChanges();


                        //}
                        singletonInstance.SomethingChanged = false;
                        singletonInstance.UpdatedSuccessfully = true;
                    }
                    else
                    {
                    }

                    (Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/Cart.xaml?refersh=" + DateTime.Now.ToString(), UriKind.Relative));
                }
            }


        }

        private void QuantityBoxLabel_LostFocus(object sender, RoutedEventArgs e)
        {
            // Do same as QuantityBoxLabel_MouseLeave
            QuantityBoxLabel_MouseLeave(null, null);

        }

        private void QuantityBoxLabel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
           //Cant be 0 . Already Filtered in ThirdPage

            if (QuantityBoxLabel.Text != "")
            {
                singletonInstance.QuantityOriginal = int.Parse(QuantityBoxLabel.Text);

            }
        }

        private void QuantityBoxLabel_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
         //   TESTING;
           // this.QuantityBoxLabel.IsHitTestVisible = false;


            //Cant be 0 . Already Filtered in ThirdPage
            singletonInstance.QuantityOriginal = int.Parse(QuantityBoxLabel.Text);


        }

        private void QuantityBoxLabel_LayoutUpdated(object sender, EventArgs e)
        {
            QuantityBoxLabel.Focus();
        }
    }
}
1
As you said, Quantity Textbox seems to be in CustomControl and tap on Quantity Textbox will only take you to Text_Changed. It should be in ListBox to get into Selection_Changed right? You have that CustomControl inside your ListBox? - Balasubramani M
I have Delete Button and Quantity Textbox inside CustomControl. When I click on utton it first takes me to ListBox_SelectionChanged perfectly. But why doesn't clicking inside quantity textbox take me first to ListBox_SelectionChanged ? - Atìf Shabëër
You can add the number of quantities right? or your retrieving that values from your local storage? - Balasubramani M
Please check the Link is posted. Its my CutomControl being generated in listbox. Also, it shows quantity of choosen item in textbox. i want to change the quantity, and on MouseLeave, LostFocus method/eventhandler I have written the code to update quantity, but it requires the productid or listbox selected index so that it understands which item's quantity we are changing. just in delete button case, which goes to listbox_selectionchanged first, why doesnt clicking on textbox goto listbox_selectionchanged first. how will i forward selected index to update method now :( - Atìf Shabëër

1 Answers

0
votes

Dont worry, We will get you the solution. If Quantity_TextChanged method is raised, just add these lines in your Quantity TextBox.

<TextBox IsHitTestVisible="False" />

Now you will be redirected to listbox_SelectionChanged method. There, you will get the selected index of the listbox. Now raise the keyboard using,

QuantityTextBox.Focus();

Now again get the new value from the user, replace the old value with the new value using the selected index of the listbox and display the value again. Its simple right :)