i m used ResponsiveGrid to make a Dynamic page And set a SizeChanged event in order to change button's position . but the SizeChanged always being fired make the windows flicker ,and SizeChanged event can not stop,here is my code in .cs
private void Condition_SizeChanged(object sender, EventArgs e)
{
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
}
The event is raised whenever the size is changed which may happen when you set the Width and Height properties of the rows and columns in your code.
You may want to use a variable to "suspend" events from being raised while your code is being executed:
private bool _handle = true;
private void Condition_SizeChanged(object sender, EventArgs e)
{
if (!_handle)
return;
_handle = false;
if (ActualWidth > 1480)
{
Col1.Height = new GridLength(100.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
Row1.Width = new GridLength(5.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 1);
CommonButtonSearch.SetValue(Grid.RowProperty, 0);
CommonButtonSearch.Margin = new Thickness(30, (ActualHeight - 32) / 2, 0, (ActualHeight - 32) / 2);
}
else
{
Row1.Width = new GridLength(100.0, GridUnitType.Star);
Row2.Width = new GridLength(1.0, GridUnitType.Star);
Col1.Height = new GridLength(5.0, GridUnitType.Star);
Col2.Height = new GridLength(1.0, GridUnitType.Star);
CommonButtonSearch.SetValue(Grid.ColumnProperty, 0);
CommonButtonSearch.SetValue(Grid.RowProperty, 1);
CommonButtonSearch.Margin = new Thickness((ActualWidth - 120) / 2, 30, (ActualWidth - 120) / 2, 30);
}
_handle = true;
}
Related
I'm new to Winform (.net 6), can anyone please show me a sample code on how to set up and connect to a database (SqlServer or MySQL) in Winform? Thanks!
Here is the link to a begginers guide to accessing SQL Server through C#.
The example usage to
set up and connect to a database (SqlServer or MySQL) in Winform
I give you here:
Form1.Designer
public partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.textBox_dbAddress = new System.Windows.Forms.TextBox();
this.textBox_dbName = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox_login = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox_password = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(180, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(132, 15);
this.label1.TabIndex = 0;
this.label1.Text = "Database server address";
this.textBox_dbAddress.Location = new System.Drawing.Point(180, 106);
this.textBox_dbAddress.Name = "textBox_dbAddress";
this.textBox_dbAddress.Size = new System.Drawing.Size(188, 23);
this.textBox_dbAddress.TabIndex = 1;
this.textBox_dbName.Location = new System.Drawing.Point(180, 153);
this.textBox_dbName.Name = "textBox_dbName";
this.textBox_dbName.Size = new System.Drawing.Size(188, 23);
this.textBox_dbName.TabIndex = 3;
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(180, 135);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 15);
this.label2.TabIndex = 2;
this.label2.Text = "Database name";
this.textBox_login.Location = new System.Drawing.Point(180, 198);
this.textBox_login.Name = "textBox_login";
this.textBox_login.Size = new System.Drawing.Size(188, 23);
this.textBox_login.TabIndex = 5;
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(180, 180);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(37, 15);
this.label3.TabIndex = 4;
this.label3.Text = "Login";
this.textBox_password.Location = new System.Drawing.Point(180, 245);
this.textBox_password.Name = "textBox_password";
this.textBox_password.PasswordChar = '*';
this.textBox_password.Size = new System.Drawing.Size(188, 23);
this.textBox_password.TabIndex = 7;
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(180, 227);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(57, 15);
this.label4.TabIndex = 6;
this.label4.Text = "Password";
this.button1.Location = new System.Drawing.Point(180, 286);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 8;
this.button1.Text = "Connect";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox_password);
this.Controls.Add(this.label4);
this.Controls.Add(this.textBox_login);
this.Controls.Add(this.label3);
this.Controls.Add(this.textBox_dbName);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox_dbAddress);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Label label1;
private TextBox textBox_dbAddress;
private TextBox textBox_dbName;
private Label label2;
private TextBox textBox_login;
private Label label3;
private TextBox textBox_password;
private Label label4;
private Button button1;
}
Form1
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(
$"server={this.textBox_dbAddress.Text};" +
$"database={this.textBox_dbName.Text}; " +
$"user id={this.textBox_login.Text};" +
$"password={this.textBox_password.Text};" +
$"Trusted_Connection=yes;" +
$"connection timeout=30");
try
{
myConnection.Open();
MessageBox.Show("Connection oppened");
}
catch
{
MessageBox.Show("Connection failed");
}
finally
{
myConnection.Close();
}
}
}
I'm trying to set multiple parallel animations for the same element in code-behind. Well, when im trying to pull "Moving" (RenderTransorm.X + Y) and Opacity together, it runs perfect. But when im trying to add scale animation or Width-Height animation, only the latest animation is running.
What im i doing wrong? - or, is there any elegant way to do this programmatically?
private void StartAnimation(TimeSpan startOffsetTime, TimeSpan duration)
{
if (TweetIsAnimated)
{
Globals.IsAnimating = true;
}
if (_hasAnimated)
{
return;
}
_hasAnimated = true;
StopAnimation();
EnsureTransform();
var storyboard = new Storyboard();
//--------------------------HORIZONTAL ANIMATION-------------------------
if (MoveStory)
{
var horizontalAnimation = new DoubleAnimation(FromHorizontalOffset, 0, duration)
{
EasingFunction = GetFuncByName(EasingFuncType)
//new CubicEase { EasingMode = EasingMode.EaseOut }
};
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(horizontalAnimation);
Storyboard.SetTarget(horizontalAnimation, AssociatedObject);
Storyboard.SetTargetProperty(horizontalAnimation, new PropertyPath("RenderTransform.X"));
storyboard.BeginTime = startOffsetTime;
storyboard.Begin();
}
//------------------------------------------------------------------------
//--------------------------VERTICAL ANIMATION----------------------------
if (MoveStory)
{
var veritcalAnimation = new DoubleAnimation(FromVerticalOffset, 0, duration)
{
EasingFunction = GetFuncByName(EasingFuncType)
};
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(veritcalAnimation);
Storyboard.SetTarget(veritcalAnimation, AssociatedObject);
Storyboard.SetTargetProperty(veritcalAnimation, new PropertyPath("RenderTransform.Y"));
storyboard.BeginTime = startOffsetTime;
storyboard.Begin();
}
//------------------------------------------------------------------------
//--------------------------OPACITY ANIMATION-----------------------------
if (OpacityStory)
{
var opacityAnimation = new DoubleAnimationUsingKeyFrames();
opacityAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(OpacityStart, TimeSpan.Zero));
opacityAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(OpacityStart, startOffsetTime,
GetFuncByName(EasingFuncType)));
opacityAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(1,
duration + startOffsetTime +
TimeSpan.FromMilliseconds(
Duration.TotalMilliseconds/2),
GetFuncByName(EasingFuncType)));
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(opacityAnimation);
Storyboard.SetTarget(opacityAnimation, AssociatedObject);
Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(UIElement.OpacityProperty));
storyboard.Begin();
}
//------------------------------------------------------------------------
//--------------------------SCALE ANIMATION------------------------------
if (ScaleStory)
{
ScaleTransform scaleTransform1 = new ScaleTransform(1, 1, AssociatedObject.ActualWidth, AssociatedObject.ActualHeight);
AssociatedObject.RenderTransformOrigin = new Point(1, ScaleStart);
AssociatedObject.RenderTransform = scaleTransform1;
var EasingFunction1 = new BackEase() { EasingMode = EasingMode.EaseOut, Amplitude = AmplitudeNow };
var scaleAnimation = new DoubleAnimationUsingKeyFrames();
TimeSpan time = duration + startOffsetTime + TimeSpan.FromMilliseconds(Duration.TotalMilliseconds / 2);
scaleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(ScaleStart, TimeSpan.Zero));
scaleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(ScaleStart, startOffsetTime, EasingFunction1));
scaleAnimation.KeyFrames.Add(new EasingDoubleKeyFrame(1, time, EasingFunction1));
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(scaleAnimation);
Storyboard.SetTarget(scaleAnimation, AssociatedObject);
Storyboard.SetTargetProperty(scaleAnimation, new PropertyPath("RenderTransform.ScaleY"));
storyboard.Begin();
}
//------------------------------------------------------------------------
//--------------------------RESIZE ANIMATION------------------------------
if (ResizeStory)
{
DoubleAnimation widthAnimation = new DoubleAnimation
{
From = WidthStart,
To = AssociatedObject.ActualWidth,
Duration = Duration
};
DoubleAnimation heightAnimation = new DoubleAnimation
{
From = HeightStart,
To = AssociatedObject.ActualHeight,
Duration = Duration
};
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(widthAnimation);
Storyboard.SetTarget(widthAnimation, AssociatedObject);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(FrameworkElement.WidthProperty));
storyboard.BeginTime = startOffsetTime;
storyboard.Begin();
storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(heightAnimation, AssociatedObject);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
storyboard.BeginTime = startOffsetTime;
storyboard.Begin();
}
//------------------------------------------------------------------------
}
Do not create new storyboard!
Refactor your code like this:
var storyboard = new Storyboard();
storyboard.Completed += AnimationOnCompleted;
_storyboards.Add(storyboard, true);
if (MoveStory)
{
var horizontalAnimation = new DoubleAnimation(FromHorizontalOffset, 0, duration)
{
EasingFunction = GetFuncByName(EasingFuncType)
//new CubicEase { EasingMode = EasingMode.EaseOut }
};
storyboard.Children.Add(horizontalAnimation);
Storyboard.SetTarget(horizontalAnimation, AssociatedObject);
Storyboard.SetTargetProperty(horizontalAnimation, new PropertyPath("RenderTransform.X"));
}
// ... if else etc...
if (ResizeStory)
{
DoubleAnimation widthAnimation = new DoubleAnimation
{
From = WidthStart,
To = AssociatedObject.ActualWidth,
Duration = Duration
};
DoubleAnimation heightAnimation = new DoubleAnimation
{
From = HeightStart,
To = AssociatedObject.ActualHeight,
Duration = Duration
};
storyboard.Children.Add(widthAnimation);
Storyboard.SetTarget(widthAnimation, AssociatedObject);
Storyboard.SetTargetProperty(widthAnimation, new PropertyPath(FrameworkElement.WidthProperty));
storyboard.Children.Add(heightAnimation);
Storyboard.SetTarget(heightAnimation, AssociatedObject);
Storyboard.SetTargetProperty(heightAnimation, new PropertyPath(FrameworkElement.HeightProperty));
}
storyboard.BeginTime = startOffsetTime;
storyboard.Begin();
I've downloaded a spammer program from YouTube, and decompiled it. Here's the code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace SimpleSpammer
{
public class Form1 : Form
{
private IContainer components;
private TextBox textBox1;
private Button button1;
private Button button2;
private Label label1;
private LinkLabel linkLabel1;
private Label label2;
private LinkLabel linkLabel2;
private Label label3;
private Timer timer1;
private Label label4;
private TextBox textBox2;
private Label label5;
private CheckBox checkBox1;
public Form1()
{
this.components = (IContainer) null;
base.\u002Ector();
this.InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing && this.components != null)
this.components.Dispose();
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.components = (IContainer) new Container();
this.textBox1 = new TextBox();
this.button1 = new Button();
this.button2 = new Button();
this.label1 = new Label();
this.linkLabel1 = new LinkLabel();
this.label2 = new Label();
this.linkLabel2 = new LinkLabel();
this.label3 = new Label();
this.timer1 = new Timer(this.components);
this.label4 = new Label();
this.textBox2 = new TextBox();
this.label5 = new Label();
this.checkBox1 = new CheckBox();
this.SuspendLayout();
this.textBox1.Location = new Point(13, 13);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new Size(259, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "SPAM !";
this.textBox1.TextAlign = HorizontalAlignment.Center;
this.button1.Location = new Point(13, 39);
this.button1.Name = "button1";
this.button1.Size = new Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
// ISSUE: method pointer
this.button1.Click += new EventHandler((object) this, __methodptr(button1_Click));
this.button2.Location = new Point(197, 39);
this.button2.Name = "button2";
this.button2.Size = new Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "Stop";
this.button2.UseVisualStyleBackColor = true;
// ISSUE: method pointer
this.button2.Click += new EventHandler((object) this, __methodptr(button2_Click));
this.label1.AutoSize = true;
this.label1.Font = new Font("Arial", 8.25f, FontStyle.Italic, GraphicsUnit.Point, (byte) 0);
this.label1.ForeColor = SystemColors.WindowFrame;
this.label1.Location = new Point(7, 216);
this.label1.Name = "label1";
this.label1.Size = new Size(275, 14);
this.label1.TabIndex = 3;
this.label1.Text = "Copyright © 2010 TheDarkJoker94. This is a freeware.";
this.linkLabel1.AutoSize = true;
this.linkLabel1.Location = new Point(30, 190);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new Size(162, 13);
this.linkLabel1.TabIndex = 4;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "http://thedarkjoker94.cer33.com";
// ISSUE: method pointer
this.linkLabel1.LinkClicked += new LinkLabelLinkClickedEventHandler((object) this, __methodptr(linkLabel1_LinkClicked));
this.label2.AutoSize = true;
this.label2.Location = new Point(10, 135);
this.label2.Name = "label2";
this.label2.Size = new Size(172, 13);
this.label2.TabIndex = 5;
this.label2.Text = "Check my C# Tutorials on Youtube";
this.linkLabel2.AutoSize = true;
this.linkLabel2.Location = new Point(30, 151);
this.linkLabel2.Name = "linkLabel2";
this.linkLabel2.Size = new Size(242, 13);
this.linkLabel2.TabIndex = 6;
this.linkLabel2.TabStop = true;
this.linkLabel2.Text = "http://www.youtube.com/user/TheDarkJoker094";
// ISSUE: method pointer
this.linkLabel2.LinkClicked += new LinkLabelLinkClickedEventHandler((object) this, __methodptr(linkLabel2_LinkClicked));
this.label3.AutoSize = true;
this.label3.Location = new Point(10, 171);
this.label3.Name = "label3";
this.label3.Size = new Size(91, 13);
this.label3.TabIndex = 7;
this.label3.Text = "More Software on";
this.timer1.Interval = 500;
// ISSUE: method pointer
this.timer1.Tick += new EventHandler((object) this, __methodptr(timer1_Tick));
this.label4.AutoSize = true;
this.label4.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.label4.Location = new Point(13, 73);
this.label4.Name = "label4";
this.label4.Size = new Size(88, 16);
this.label4.TabIndex = 8;
this.label4.Text = "Timer Interval:";
this.textBox2.Location = new Point(102, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new Size(89, 20);
this.textBox2.TabIndex = 9;
this.textBox2.Text = "100";
this.label5.AutoSize = true;
this.label5.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.label5.Location = new Point(197, 73);
this.label5.Name = "label5";
this.label5.Size = new Size(77, 16);
this.label5.TabIndex = 10;
this.label5.Text = "miliseconds";
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = CheckState.Checked;
this.checkBox1.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, (byte) 0);
this.checkBox1.Location = new Point(13, 103);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new Size(151, 20);
this.checkBox1.TabIndex = 12;
this.checkBox1.Text = "Send the ENTER key";
this.checkBox1.UseVisualStyleBackColor = true;
this.AutoScaleDimensions = new SizeF(6f, 13f);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(284, 239);
this.Controls.Add((Control) this.checkBox1);
this.Controls.Add((Control) this.label5);
this.Controls.Add((Control) this.textBox2);
this.Controls.Add((Control) this.label4);
this.Controls.Add((Control) this.label3);
this.Controls.Add((Control) this.linkLabel2);
this.Controls.Add((Control) this.label2);
this.Controls.Add((Control) this.linkLabel1);
this.Controls.Add((Control) this.label1);
this.Controls.Add((Control) this.button2);
this.Controls.Add((Control) this.button1);
this.Controls.Add((Control) this.textBox1);
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.ShowIcon = false;
this.Text = "SimpleSpammer";
this.ResumeLayout(false);
this.PerformLayout();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://thedarkjoker94.cer33.com/");
}
private void button1_Click(object sender, EventArgs e)
{
try
{
this.timer1.Interval = Convert.ToInt32(this.textBox2.Text);
this.timer1.Start();
}
catch (Exception ex)
{
int num = (int) MessageBox.Show("An Exception was thrown!\n" + ex.Message, "ExceptionThrown");
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Focused)
return;
SendKeys.Send(this.textBox1.Text);
if (this.checkBox1.CheckState == CheckState.Checked)
SendKeys.Send("{ENTER}");
}
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Stop();
}
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://www.youtube.com/user/TheDarkJoker094");
}
}
}
Now, as you can see his code contains a delay field, a text field, and "Start"/"Stop" buttons.
So I've tried to make something similar, and here's the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
}
private void send()
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Focused)
{
return;
}
SendKeys.Send(textBox1.Text);
}
}
}
Now, I've posted a similar question yesterday but this one is far more accurate.
In his program, I've entered some text, chose a delay of 10MS and started it. After a few seconds of sending, I've clicked the "Stop" button in his program and it stopped sending keys immediately.
Now... When I ran my program, and started sending keys (with a 10MS delay), I let it run for a few seconds, and then I pressed "Stop". Nothing happened, and "Stop" didn't even click. It just has shown a blue frame around it and I wasn't able to click it.
And of course, because of this, it didn't stop sending keys.
So, why did it work perfectly in his program but it doesn't work at all in my program?
Is it possible to animate the "old school" way, in codebehind, instead of xaml?
I just want an arrow that points to something with a 'bounce effect' which I could easily do in my own for loop. But I do not know how to refresh or do a timer delay, inside the loop. I already placed the image into position in codebehind. All I want to do is this simple animation...
public void validationArrow()
{
var validationArrow = new Image();
validationArrow.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(validationArrow);
validationArrow.Stretch = Stretch.None;
validationArrow.VerticalAlignment = System.Windows.VerticalAlignment.Top;
validationArrow.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
var arrowPosition = new TranslateTransform { X = 0, Y = 0 };
validationArrow.RenderTransform = arrowPosition;
validationArrow.Name = "validationArrow";
for (int i = 150; i >= 0; i--)
{
arrowPosition.X = i;
validationArrow.RenderTransform = arrowPosition;
// how can I refresh screen and do some timing here?
}
}
There's no school like the old school ;)
Here, this should help you on your way. You can play with the millisecond and Y translation values being passed to the BuildEasing method to change the 'bounce' effect's speed and distance.
private void RunStoryboard()
{
var arrowImage = new Image();
arrowImage.RenderTransform = new CompositeTransform();
arrowImage.Source = new BitmapImage(new Uri("/SlProject;component/arrow.png", UriKind.RelativeOrAbsolute));
LayoutRoot.Children.Add(arrowImage);
Storyboard storyboard = new Storyboard();
storyboard.Children.Add(BuildKeyFrame(arrowImage));
storyboard.Begin();
}
private DoubleAnimationUsingKeyFrames BuildKeyFrame(Image target)
{
DoubleAnimationUsingKeyFrames kf = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(kf, target);
Storyboard.SetTargetProperty(kf, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));
kf.KeyFrames.Add(BuildEasing(100, 10));
kf.KeyFrames.Add(BuildEasing(200, 0));
kf.KeyFrames.Add(BuildEasing(300, 10));
kf.KeyFrames.Add(BuildEasing(400, 0));
kf.KeyFrames.Add(BuildEasing(500, 10));
kf.KeyFrames.Add(BuildEasing(600, 0));
return kf;
}
private EasingDoubleKeyFrame BuildEasing(int ms, int value)
{
return new EasingDoubleKeyFrame()
{
KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 0, ms)),
Value = value
};
}
I'm trying to add a Viewport2DVisual3D to a Viewport3D in code, but the visual isn't showing up. Any help understanding why not would be appreciated. The following is the code for the main window.
Is it sufficient to just add the Viewport2DVisual3D to the children of the Viewport3D in order for it to be rendered?
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(temp);
}
public void temp(object sender, RoutedEventArgs e)
{
Viewport2DVisual3D test = new Viewport2DVisual3D();
MeshGeometry3D testGeometry = new MeshGeometry3D();
Vector3D CameraLookDirection = Main_Target_CameraOR20.LookDirection;
// Calculate the Positions based on the Camera
Point3DCollection myPoint3DCollection = new Point3DCollection();
myPoint3DCollection.Add(new Point3D(-1, 1, 0));
myPoint3DCollection.Add(new Point3D(-1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, -1, 0));
myPoint3DCollection.Add(new Point3D(1, 1, 0));
testGeometry.Positions = myPoint3DCollection;
PointCollection myPointCollection = new PointCollection();
myPointCollection.Add(new Point(0, 0));
myPointCollection.Add(new Point(0, 1));
myPointCollection.Add(new Point(1, 1));
myPointCollection.Add(new Point(1, 0));
testGeometry.TextureCoordinates = myPointCollection;
Int32Collection triangleIndicesCollection = new Int32Collection();
triangleIndicesCollection.Add(0);
triangleIndicesCollection.Add(1);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(2);
triangleIndicesCollection.Add(3);
triangleIndicesCollection.Add(0);
testGeometry.TriangleIndices = triangleIndicesCollection;
DiffuseMaterial myDiffuseMaterial = new DiffuseMaterial(Brushes.White);
Viewport2DVisual3D.SetIsVisualHostMaterial(myDiffuseMaterial, true);
Transform3DGroup myTransform3DGroup = new Transform3DGroup();
ScaleTransform3D myScaleTransform3D = new ScaleTransform3D();
myScaleTransform3D.ScaleX = 2;
myScaleTransform3D.ScaleY = 2;
myScaleTransform3D.ScaleZ = 2;
TranslateTransform3D myTranslateTransform3D = new TranslateTransform3D();
myTranslateTransform3D.OffsetX = -27;
myTranslateTransform3D.OffsetY = 13;
myTranslateTransform3D.OffsetZ = 6;
RotateTransform3D rotateTransform = new RotateTransform3D()
{
Rotation = new AxisAngleRotation3D
{
Angle = -50,
Axis = new Vector3D(0, 1, 0)
}
};
myTransform3DGroup.Children.Add(myTranslateTransform3D);
myTransform3DGroup.Children.Add(myScaleTransform3D);
myTransform3DGroup.Children.Add(rotateTransform);
test.Transform = myTransform3DGroup;
Button myButton = new Button();
myButton.Content = "Test Button";
test.Material = myDiffuseMaterial;
test.Geometry = testGeometry;
test.Visual = myButton;
ZAM3DViewport3D.Children.Add(test);
}
}
It turns out that the problem was the Offset value. So, it is sufficient to add the child to the Viewport3D to have it render. Cheers