Wednesday 27 March 2013

How to set maximum length to textarea in html


 Set maximum length to textarea in html
<textarea rows="3" cols="10" onkeypress="if (this.value.length > 10) { return false; }" ></textarea>

How to set maximum length to asp.net multiline textbox?


Set maximum length of a multiline property textbox.

<asp:TextBox ID="txt" runat="server" TextMode="MultiLine" Height="60" onkeypress="if (this.value.length > 10) { return false; }"></asp:TextBox>

Tuesday 26 March 2013

Find the xml node value without knowing the position of an xml node from xml file using SQL query.


To find the xml node value without knowing the position of an xml node from xml file using SQL query.


Given 1 sample xml file which consist MyNode. Here I want to get the value of MyNode which may be placed in different position.
DECLARE @xml XML

SET @xml='<RootNode>
<SubNodes>
      <Node1>Node 1 </Node1>
      <Node2>Node 2 </Node2>
      <Node3>
            <MyNode> My node value </MyNode>
       </Node3>
 </SubNodes>
</RootNode>'

SELECT Nod.value('local-name(.)', 'varchar(50)') AS 'NodeName',
         Nod.value('.', 'varchar(50)') AS 'Node Value'
         FROM @xml.nodes('//*') AS xmlFile(Nod) WHERE Nod.value('local-name(.)', 'varchar(50)')='MyNode'

The output look like



Saturday 23 March 2013

Creating Movable Controls in a WPF Application.


Create Canvas control in WPF window

  <Canvas x:Name="MyCanvas" Background="LightBlue" >          
           
  </Canvas> 


Add controls whatever you wish
I am taking ellipse control the XAML syntax is as follows

<Ellipse Fill="Red" Height="50" Width="100" Canvas.Top="100" Canvas.Left="50" >
            <Ellipse.RenderTransform>
                <TranslateTransform/>
            </Ellipse.RenderTransform>
</Ellipse>


Create MouseLeftButtonDown event for ellipse control
The XAML syntax looks like as below.

<Ellipse Fill="Red" Height="50" Width="100" Canvas.Top="100" Canvas.Left="50" MouseLeftButtonDown="Ellipse_MouseLeftButtonDown">
            <Ellipse.RenderTransform>
                <TranslateTransform/>
            </Ellipse.RenderTransform>
</Ellipse>

Same way you can add another controls. I am adding Rectangle control

<Rectangle Height="100" Width="100" Canvas.Left="243" Canvas.Top="62" Fill="Gray" MouseLeftButtonDown="Rectangle_MouseLeftButtonDown">
            <Rectangle.RenderTransform>
                <TranslateTransform/>
            </Rectangle.RenderTransform>
</Rectangle>


Now create MouseDown, MouseMove, MouseUp events to Canvas Control.

The line looks like

<Canvas x:Name="MyCanvas" Background="LightBlue" MouseDown="Canvas_MouseDown" MouseMove="Canvas_MouseMove" MouseUp="Canvas_MouseUp" >


The design look like



Now going towards to add the code.

Create global variable

double positionX = 0;
double positionY = 0;       
UIElement UIElementHolder = null;

Write the following codes in Code file.
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
               positionX = Mouse.GetPosition((UIElement)sender).X;
               positionY = Mouse.GetPosition((UIElement)sender).Y;          
}

private void Canvas_MouseMove(object sender, MouseEventArgs e)
       {
            // checking draging is holding
            if (e.LeftButton == MouseButtonState.Pressed && UIElementHolder != null)
            {
           
                //Capturing the mouse position
                double newX = Mouse.GetPosition(MyCanvas).X;
                double newY = Mouse.GetPosition(MyCanvas).Y;

                //resetting the holded element position

                //control rendertransfer object created for moving in runtime using translateTransfrom element
                Transform objUIElement = UIElementHolder.RenderTransform;

                //getting offset values of control
                double offsetX = objUIElement.Value.OffsetX;
                double offsetY = objUIElement.Value.OffsetY;

                //changing translateTransfrom
                objUIElement.SetValue(TranslateTransform.XProperty, newX+offsetX-positionX);
                objUIElement.SetValue(TranslateTransform.YProperty, newY+offsetY-positionY);             

                // Update position to global variables
                positionX= newX;
                positionY = newY;
             
            }
        }

        private void Canvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (UIElementHolder != null)
            {
                //Clearing holding UIElement              
                UIElementHolder = null;
            }

        }

        private void Ellipse_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Assigning related UIElement to UIElementHolder Object
            UIElementHolder = (UIElement)sender;
        }

        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //Assigning related UIElement to UIElementHolder Object
            UIElementHolder = (UIElement)sender;
        }


Now run the application move the controls..