lundi 4 octobre 2010

Javascript Review

JavaScript is what the language is usually called. However, the name JavaScript is owned by Netscape. Microsoft calls its version of the language JScript. The generic name of the language is EcmaScript.

Difference between Javascript and JScript :

http://javascript.about.com/od/reference/a/jscript.htm

Hello World :

<html>
<
body>
<
script type="text/javascript">
document.write("Hello World!");
</script>
</
body>
</
html>

Javascript in the Head section :

<html>
<
head>
<
script type="text/javascript">
function
message()
{
alert("This alert box was called with the onload event");
}
</script>
</
head>

<
body onload="message()">
<
p>We usually use the head section for functions (to be sure that the functions are loaded before they are called).</p>
</
body>
</
html>

For browsers that do not support JavaScript :

Browsers that do not support JavaScript, will display JavaScript as page content. To prevent them from doing this, and as a part of the JavaScript standard, the HTML comment tag should be used to "hide" the JavaScript. Just add an HTML comment tag <!-- before the first JavaScript statement, and a --> (end of comment) after the last JavaScript statement, like this:



<html>
<
body>
<
script type="text/javascript">
<!--
document.write("Hello World!");
//-->
</script>
</
body>
</
html>



Alert Box :

alert("You pressed Cancel!");

Confirm Box :

confirm("Press a button");

Prompt Box :

function Prompt() {
prompt("sometext","defaultvalue");
}

The for Loop :

for (var=startvalue;var<=endvalue;var=var+increment)
{
//code to be executed
}

getElementById() Accesses the first element with the specified id.

<head>
<
script type="text/javascript">
function
getValue() {
var x = document.getElementById("myHeader");
alert(x.innerHTML);
}
</script>
</
head>
<
body>

<
h1 id="myHeader" onclick="getValue()">Click me!</h1>

</
body>
</
html>

getElementByName Accesses all elements with the specified name.

<html>
<
head>
<
script type="text/javascript">
function
getElements() {
var x = document.getElementsByName("x");
alert(x.length);
}
</script>
</
head>
<
body>

<
input name="x" type="text" size="20" /><br />
<
input name="x" type="text" size="20" /><br />
<
input name="x" type="text" size="20" /><br /><br />
<
input type="button" onclick="getElements()" value="How many elements named 'x'?" />

</
body>
</
html>

mardi 28 septembre 2010

Slider : MouseLeftButtonDown event

Constructor :

mediaSlider.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler(mediaSlider_MouseLeftButtonDown), true);

Event handler :

private void mediaSlider_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
txt.Text ="MouseLeftButton DOWN";
}

http://forums.silverlight.net/forums/p/117368/264314.aspx

mercredi 28 avril 2010

Find a control in a WPF/Silverlight by name

Public static T FindVisualChildByName(DependencyObject parent, string name) where T : DependencyObject

{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var child = VisualTreeHelper.GetChild(parent, i);
string controlName = child.GetValue(Control.NameProperty) as string;
if (controlName == name)
{
return child as T;
}
else
{
T result = FindVisualChildByName(child, name);
if (result != null)
return result;
}
}
return null;
}
This works for the Visual elements, not for the current elements of a parent: http://forums.silverlight.net/forums/p/85766/402922.aspx


Ref:
http://flatlinerdoa.spaces.live.com/Blog/cns!17124D03A9A052B0!566.entry
http://blogs.msdn.com/kmahone/archive/2009/03/29/visualtreehelper.aspx
http://enterpriseetc.com/post/VisualTreeHelper-Class-in-Silverlight.aspx

mercredi 21 avril 2010

SL Custom Controls

http://msdn.microsoft.com/en-us/magazine/cc721611.aspx#id0430049

Step 1: Create a New Silverlight Project
Step 2: Derive from Control (or ContentControl)
Step 3: Create a Control Template
Step 4: Create a Default Control Template
Step 5: Add Template Bindings
Step 6: Replace TextBlock with ContentPresenter
Step 7: Add a Click Event
Step 8: Add Visual States
The Completed Control