设计界面:
编写代码
?
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?www.sxzhongrui.com;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
?
namespace?x
{
????public?partial?class?Form1?: Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
?
????????private?void?button1_Click(object?sender, EventArgs?e)
????????{
????????????try
????????????{
????????????????int?x = Convert.ToInt32(textBox1.Text);
????????????????int?y = 0;
????????????????if?(x >= 0 & x < 10)
????????????????{
????????????????????y = 1 - x * 2;
????????????????}
????????????????else?if?(x >= 10 & x < 20)
????????????????{
????????????????????y = x;
????????????????}
????????????????else?if?(x >= 20 & x < 30)
????????????????{
????????????????????y = 1 + x * 2;
????????????????}
????????????????else
????????????????{
????????????????????www.sxzhongrui.com("请输入0-30之间的数字");
????????????????}
????????????????label2.Text = "y的值为:"?+ Convert.ToString(y);
????????????}
????????????catch?
????????????{
????????????????www.sxzhongrui.com("请输入数字x");
????????????}
????????}
????}
}
?
运行结果:
概要:
有时我在想这个学习LerryLee的Silverlight笔记还要不要写下去。首先我是菜鸟,也是边学边写,内容实在是没什么深度;其次Silverlight3与Silverlight2也就那么一点差别,即使有什么问题大家到网上搜搜基本上都能找到。但我觉得既然当初开始了,那么就把它写完,至少也有始有终么。我就当是写在自己的笔记本上的,给我自己看的。
事件的声明:
在C#中的事件声明有:声明事件,事件处理方法,事件与方法绑定。 silverlight也类似,可以在.maxl中声明事件,也可以在.cs文件中声明事件并绑定。
代码
<
Grid
x:Name
="LayoutRoot"
Background
="#46461F"
>
<
Ellipse
Width
="120"
Height
="120"
Fill
="Orange"
www.sxzhongrui.com
="60"
Canvas.Left
="80"
MouseEnter
="Ellipse_MouseEnter"
MouseLeave
="Ellipse_MouseLeave"
>
Ellipse
>
<
Ellipse
x:Name
="ell"
Width
="50"
Height
="50"
Fill
="Orange"
www.sxzhongrui.com
="60"
Canvas.Left
="280"
Margin
="283,125,67,125"
>
Ellipse
>
Grid
>
代码
public
MainPage() { InitializeComponent(); ell.MouseEnter
+=
new
MouseEventHandler(Ellipse_MouseEnter); ell.MouseLeave
+=
new
MouseEventHandler(Ellipse_MouseLeave); }
private
void
Ellipse_MouseEnter(
object
sender, MouseEventArgs e) { Ellipse el
=
sender
as
Ellipse; el.Fill
=
new
SolidColorBrush(Colors.Yellow); }
private
void
Ellipse_MouseLeave(
object
sender, MouseEventArgs e) { Ellipse ell
=
sender
as
Ellipse; ell.Fill
=
new
SolidColorBrush(www.sxzhongrui.com); }
?
效果也是一样的:
有人问sender
as
Ellipse;中的sender什么意思,显然
object sender即事件的发送者,此例子中的sender即ellipse,因为是它触发的事件。
事件数据:
比如上个例子中的事件处理方法:
void OnMouseLeave(object sender, MouseEventArgs e)sender是事件发送者,那么e就是存储事件的数据。事件数据类从EventArgs继承来的。所以事件的数据要用到e。
代码
<
Grid
x:Name
="LayoutRoot"
Background
="White"
>
<
Rectangle
Height
="120"
Margin
="10,10,0,0"
Width
="200"
Fill
="Orange"
Stroke
="White"
StrokeThickness
="2"
www.sxzhongrui.com
="40"
Canvas.Left
="130"
MouseMove
="Rectangle_MouseMove"
/>
<
TextBlock
Margin
="10,120,0,0"
Name
="txtb"
Text
="TextBlock"
VerticalAlignment
="Top"
Width
="120"
/>
Grid
>
代码
private
void
Rectangle_MouseMove(
object
sender, MouseEventArgs e) { Point p
=
e.GetPosition(e.OriginalSource
as
FrameworkElement); txtb.Text
=
String.Format(
"
坐标位置({0}:{1})
"
, p.X, p.Y); }
注意e.OriginalSource是Silverlight3中的,代替了原e.Source。
?
路由事件:
我开始不知道什么叫路由事件,不过LerryLee也解释了:事件路由,使得我们可以在父节点上接收和处理来自于子节点的事件。估计事件路由就是说在父容器声明的事件,也可以应用到子控件。不知这样对不对?这个例子我试了一下,没什么困难,就不写上来了。
拖放功能:
拖放的这篇跟事件密切相关,我就一起学了。逻辑:1,按下鼠标——MouseLeftButtonDown2,拖动鼠标——MouseMove3,松开鼠标——MouseLeftButtonUp听TerryLee这么一说,思路一下就清晰了。我就试着自己写写看。知道是一会事,做起来是另外一回事。遇到好多问题,不过最后总算高明白了。要在Canvas里,Grid不行。——这个是因为我在Grid里设置不了坐标,谁有办法的可以告诉我一下。button控件不行,但我用Image是可以的。——因为Silverlight3里button点击只响应click事件,不响应MouseLeftButtonDown事件。加上ClickMode="Hover",button就可以拖动了。
代码
<
Canvas
Background
="#46461F"
>
<
Image
Source
="/SilverlightAppDemo6;component/Images/aspxba8205_20081025143826_2.jpg"
Width
="50"
Height
="50"
MouseLeftButtonDown
="OnMouseDown"
MouseMove
="OnMouseMove"
MouseLeftButtonUp
="OnMouseUp"
>
Image
>
<
Button
Canvas.Left
="10"
www.sxzhongrui.com
="10"
Content
="Button"
Height
="23"
Name
="button1"
Width
="75"
MouseLeftButtonDown
="OnMouseDown"
MouseMove
="OnMouseMove"
MouseLeftButtonUp
="OnMouseUp"
/>
Canvas
>
?
代码
bool
trackingMouseMove
=
false
; Point mousePosition;
public
MainPage() { InitializeComponent(); }
void
OnMouseDown(
object
sender, MouseButtonEventArgs e) { FrameworkElement element
=
sender
as
FrameworkElement; mousePosition
=
e.GetPosition(
null
); trackingMouseMove
=
true
;
if
(
null
!=
element) { element.CaptureMouse(); element.Cursor
=
Cursors.Hand; } }
void
OnMouseMove(
object
sender, MouseEventArgs e) { FrameworkElement element
=
sender
as
FrameworkElement;
if
(trackingMouseMove) {
double
deltaV
=
e.GetPosition(
null
).Y
-
mousePosition.Y;
double
deltaH
=
e.GetPosition(
null
).X
-
mousePosition.X;
double
newTop
=
deltaV
+
(
double
)element.GetValue(Canvas.TopProperty);
double
newLeft
=
deltaH
+
(
double
)element.GetValue(Canvas.LeftProperty); element.SetValue(Canvas.TopProperty, newTop); element.SetValue(Canvas.LeftProperty, newLeft); mousePosition
=
e.GetPosition(
null
); } }
void
OnMouseUp(
object
sender, MouseButtonEventArgs e) { FrameworkElement element
=
sender
as
FrameworkElement; trackingMouseMove
=
false
; element.ReleaseMouseCapture(); mousePosition.X
=
mousePosition.Y
=
0
; element.Cursor
=
null
; }
? 这个代码基本上来自TerryLee,我自己写的也可以移动,不过移动时一抖一抖的。我晕了。
总目录
上一篇:
vs2010 学习Silverlight学习笔记(4):界面布局
下一篇:
vs2010 学习Silverlight学习笔记(6):全屏模式
转载于:https://www.sxzhongrui.com/yaoge/archive/2010/05/08/1730620.html
1.关系运算符含义 和 关系表达式示例
运算符 名称 示例 功能 缩写 < 小于 a 大于 a>b a大于b时返回真;否则返回假 GT >= 大于等于 a>=b a大于等于b时返回真;否则返回假 GE == 等于 a==b a等于b时返回真;否则返回假 EQ != 不等于 a!=b a不等于b时返回真;否则返回假 N
参见自: https://www.sxzhongrui.com/item/%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6/352774?fr=aladdin#1
?
2: 例子:输入两个整数判断大小
main()
{
int x,y,z;
//变量说明(声明)
//函数内的变量声明务必要放到函数主体的最顶部,
//否则在一些严格遵守C89的编译器上会报错且无法通过编译!!!(如:VS2010)
int max(int a,int b);
//函数声明(说明)
printf("输入两个整数(用空格分开)判断大小:");
//输出 字符串
scanf("%d %d",&x,&y);
//输入 x和y的值,
//scanf()为内置的输入函数
z=max(x,y);
//先调用max()函数,
//再将max()函数返回的值赋给z
printf("最大数是: %d \n",z);
//输出 字符串
//printf()为内置的输出函数
}
//主函数(main)
int max(int a,int b)
{
if (a>b) return a;else return b;
}
//自定义max函数
运行效果:
3.输入三个整数判断大小
https://www.sxzhongrui.com/u012336596/article/details/113806728#t2
4.例子: 模拟红绿灯 和 模拟 菜单
https://www.sxzhongrui.com/u012336596/article/details/113814572
?
运算符优先级与结合性
https://www.sxzhongrui.com/u012336596/article/details/113837473#t5
关系运算符等级 低于 算数运算符等级