locked
IEnumerable RRS feed

  • Question

  • How do you use the  IEnumerable in silverlight as the MSDN example is confusing.
    Saturday, June 21, 2008 8:39 AM

Answers

  • dont pass the e.GetPosition for the point only

    but calculate another one there should be 4cases for the HitTest coz there are four lines constructing a TextBox

    You should list the four cases, once there is a line Hit with the ListBox will Show "Hit"

    For the Left Line of the TextBox
    e.GetPosition(null).X - (txt1.Width - e.GetPosition(txt1).X)

    For the Right Line of the TextBox
    e.GetPosition(null).X + (txt1.Width - e.GetPosition(txt1).X)

    For the Upper Line of the TextBox
    e.GetPosition(null).Y - (txt1.Height- e.GetPosition(txt1).Y)

    For the Lower Line of the TextBox
    e.GetPosition(null).Y + (txt1.Height- e.GetPosition(txt1).Y)

    Try them all, and if one of them has the result that is the hit

    Sunday, June 22, 2008 8:28 AM
  • Tuesday, June 24, 2008 5:08 AM

All replies

  • More context would be nice...what are you trying to do?

    Saturday, June 21, 2008 10:53 AM
  •  i am using hittest and it returns this data type which I dont know how to use it, is it like an array or collection?

    Saturday, June 21, 2008 8:25 PM
  • Basically with IEnumerable type, you can perform Linq to do certain search. With regular array or collection Linq won't work.

    Example: You want to find a certain type of control or a particular control in your HitTest on a Grid MouseLeftButtonDown event:

    private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                var u = from element in dataGrid.HitTest(e.GetPosition(null))
                        where element is TextBlock
                        select element;
                           
                if(u.Count() > 0) // You find at least one TextBlock that overlap with your mouse position.
                 {

                 }

     }

     

    Saturday, June 21, 2008 8:44 PM
  •  just say i am testing to see if a textbox control is over a listbox.

    So wehat would be the code as I see you have used a SQL type of command with var 'u'.

    I  havent used linq before i have to say but i have used SQL many times. 

     In mouseup this didnt work but i didnt get an error

      Dim u = From element In lb.HitTest(e.GetPosition(Nothing)) _
            Where element Is txt1 Select element

            If u.Count() > 0 Then
                txt1.Text = "hit"
            Else
                txt1.Text = "miss"
            End If

    Saturday, June 21, 2008 8:54 PM
  •  I don't know VB syntax for Linq.  var is anonymous type in C#.

    What is "txt1"? 

    In my sample code I use "element is TextBox" means I want to find the control that is a TextBox ( "is" is for Type, not "==").

    If you want to find a control that is a TextBox with name = "txt1" you need to do more:

    var u = from element in this.HitTest(e.GetPosition(null))
                         where element is TextBox && (string)element.GetValue(TextBox.NameProperty) == "txt1"
                         select element;

    if(u.Count() > 0)

    {

       TextBox t= u.ElementAt(0) ;  // here t is the Textbox you are looking for

    }

    Or you can do:

     var u = from element in this.HitTest(e.GetPosition(null))
                         where element == this.txt1
                         select element;

     

     

     

     

     

     

    Saturday, June 21, 2008 9:43 PM
  •  i have a textbox called txt1 and a listbox called lb

     I want to drag the textbox and if it is over the listbox anywhere i will display a message.

    Now i cant use the word textbox as i get an error as i cant use that  type  in the expression.

     what do i use exactly or what is the simplest to code?

        Dim u = From element In lb.HitTest(e.GetPosition(Nothing)) _
            Where element Is TextBox Select element 'error

            If u.Count() > 0 Then
                txt1.Text = "hit"
            Else
                txt1.Text = "miss"
            End If

    ---

    this fails as well but i get no error


            Dim u = From element In lb.HitTest(e.GetPosition(Nothing)) _
            Where element Is Me.txt1 _
            Select element

     

    Sunday, June 22, 2008 12:31 AM
  •  I have had a test on it using dragging an ellipse called ball onto the Button (myBtn)

    It seems that using the UIElement.HitTest(point) will return some basic Element instead of the control. Or is there any problem on my code?

    Sunday, June 22, 2008 2:29 AM
  • I have worked out that now

            #region Drag and Drop Starts Here
            private bool isMouseCaptured;
            private Point mousePosition;
            void DragNDrop_MouseMove(object sender, MouseEventArgs e)
            {
                FrameworkElement item = sender as FrameworkElement;
                if (isMouseCaptured)
                {

                    // Calculate the current position of the object.
                    double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                    double deltaH = e.GetPosition(null).X - mousePosition.X;
                    double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
                    double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);

                    // Set new position of object.
                    item.SetValue(Canvas.TopProperty, newTop);
                    item.SetValue(Canvas.LeftProperty, newLeft);

                    // Update position global variables.
                    mousePosition = e.GetPosition(null);
                    var u = from element in this.HitTest(e.GetPosition(null))
                            where (element.GetValue(NameProperty).ToString() == "Ball" || element.GetValue(NameProperty).ToString() == "myBtn")
                            select element;
                    if (u.Count() == 2)
                    {
                        TargetEMail.Text = "Hit";
                    }
                    else TargetEMail.Text = "Unhit";

                }
            }
            void DragNDrop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                FrameworkElement item = sender as FrameworkElement;
                isMouseCaptured = false;
                item.ReleaseMouseCapture();
                mousePosition.X = mousePosition.Y = 0;
                item.Cursor = null;
                item.Opacity *= 2;
            }
            void DragNDrop_MouseEnter(object sender, MouseButtonEventArgs e)
            {

                FrameworkElement item = sender as FrameworkElement;
                mousePosition = e.GetPosition(null);
                isMouseCaptured = true;
                item.CaptureMouse();
                item.Opacity *= 0.5;
            }
            #endregion Drag and Drop Ends Here

    Sunday, June 22, 2008 2:43 AM
  • ok let me look at this and get back to you but thankyou so far for all the effort..

    First glance can you just use the listbox.hit instead of this.HitTest

    and textbox names as is and get the textbxox name after  it has a hit like the original solution?

    Sunday, June 22, 2008 3:15 AM
  • I think there are some problems of using UIElement.HitTest(...)

    So I get to use this.HitTest(...)

    Sunday, June 22, 2008 3:17 AM
  • I am sorry but  this doesnt work at all as i never get a hit

    All I am doing is dragging a textbox and when i drag the textbox onto any part of the listbox then I want to get a hit message displayed.

     

    Looking at other codes we are getting the position of 'nothing'  from the canvas so I cant see how this makes sense. I translated c# into vb.net.

     

      Dim u = From element In Me.HitTest(e.GetPosition(Nothing)) _
             Where element.GetValue(NameProperty).ToString = "txt1" Or _
            element.GetValue(NameProperty).ToString = "lb" Select element

       If u.Count > 0 Then
                txt1.Text = "hit"
            Else
                txt1.Text = "miss"
            End If

     

     

    Sunday, June 22, 2008 4:03 AM
  • That should be u.Count() = 2 coz there should be two item inside the element (one is the lb another is the txt)

    That's true in VB

    If you are handling the event from the root element, passing the root element as relativeTo or passing nullNothingnullptra null reference (Nothing in Visual Basic) are equivalent.

    Sunday, June 22, 2008 4:51 AM
  • And you should have .ToString() <= it should be a method

    Sunday, June 22, 2008 5:00 AM
  •  no it doesnt work at ALL as i said, i dont get ANY value with u.count  apart from 0.

     

    It doesnt make any HIT.

     

     

     

    Sunday, June 22, 2008 5:01 AM
  • Where do you put these lines?

    They should be put inside the event that fires when user started dragging and MouseMove

    So it should be put inside the MouseMove event from the Drag and Drop

    Ps.my example is right here http://59.149.117.93/SteveWeb/ 
    Try to drag the ellpise to the middle button and the TextBox above will show hit or not

    Sunday, June 22, 2008 5:13 AM
  •  they are in mouse move and i tried in mouse up.

     

    I use 2 controls on a canvas as i said and no it still doesnt work.

    There must be a catch somewhere. 

    Sunday, June 22, 2008 5:19 AM
  • Set a break point to see if

    Me.HitTest(e.GetPosition(Nothing)) Returns some values

    And to see if you can get the right position on e.GetPosition?

    Sunday, June 22, 2008 5:30 AM

  • Me.HitTest(e.GetPosition(Nothing)) Returns no values ' and the value of VAR u is nothing so it doesnt get a value!

    ' u  returns nothing

        Dim u = From element In Me.HitTest(e.GetPosition(Nothing)) _
             Where element.GetValue(NameProperty).ToString Is "txt1" _
            Or element.GetValue(NameProperty).ToString Is "lb" Select element

     

    how do i test for this  as i can only test for u

    e.GetPosition does get a value ok

     

    Sunday, June 22, 2008 5:57 AM
  • try this instead of Me I dont know if this is the case in VB

    Sunday, June 22, 2008 6:05 AM
  • no 'this' is undefined as in vb.net we use Me.

    I tried  using  a converter but i really cant see how i can debug parts of the linq example and surely the UI.hitTest must work? 

     

    The problem could be using 2 controls which your example didnt pick up on ?

    Sunday, June 22, 2008 6:15 AM
  • So, that should be the problem on the line HitTest

    Sunday, June 22, 2008 6:49 AM
  •  i have no idea what to do because i never used linq before or hittest.

    what can i do? 

    Sunday, June 22, 2008 6:56 AM
  •  if i do this i will get a hit if i drag the textbox over the listbox BUT the textbox is not one of the items that appears in the  result VAR from the for each.

    let us forget about LINQ please for now.

    this will only work if the mouse pointer in the textbox is also in the listbox so i need amore accurate way to get anypart over the lisbox.



         Dim result = lb.HitTest(e.GetPosition(Nothing))

            Dim ui As UIElement

            For Each ui In result

                ' If (ui Is txt1) Then
                txt1.Text = txt1.Text & " " & ui.ToString  'textbox does not appear but the hitest will detect a hit  with other UI objects
                '  End If
            Next

    Sunday, June 22, 2008 7:47 AM
  • dont pass the e.GetPosition for the point only

    but calculate another one there should be 4cases for the HitTest coz there are four lines constructing a TextBox

    You should list the four cases, once there is a line Hit with the ListBox will Show "Hit"

    For the Left Line of the TextBox
    e.GetPosition(null).X - (txt1.Width - e.GetPosition(txt1).X)

    For the Right Line of the TextBox
    e.GetPosition(null).X + (txt1.Width - e.GetPosition(txt1).X)

    For the Upper Line of the TextBox
    e.GetPosition(null).Y - (txt1.Height- e.GetPosition(txt1).Y)

    For the Lower Line of the TextBox
    e.GetPosition(null).Y + (txt1.Height- e.GetPosition(txt1).Y)

    Try them all, and if one of them has the result that is the hit

    Sunday, June 22, 2008 8:28 AM
  • q1) ok i can get it to work but why doesnt the textbox come up in the enumable values when i do listbox hittest, as this confuses me?

     


     

     

    Sunday, June 22, 2008 6:43 PM
  •  anyone?

    Monday, June 23, 2008 6:08 AM
  • Tuesday, June 24, 2008 5:08 AM