.
.
Tuesday, September 6, 2011
Stock Ticker Emulation COM Add-In Microsoft Office Excel Plugin Multithreaded - Ethan Castanon - C# - CSharp - C Sharp
namespace Ticker_Simulation
{
partial class Conditions
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Conditions));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.txtTargetCellRow = new System.Windows.Forms.TextBox();
this.txtTargetCellColumn = new System.Windows.Forms.TextBox();
this.txtVolatility = new System.Windows.Forms.TextBox();
this.txtPrice = new System.Windows.Forms.TextBox();
this.btnEnter = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(23, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Cell Row";
//
// label2
//
this.label2.Location = new System.Drawing.Point(129, 12);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 1;
this.label2.Text = "Cell Column";
//
// label3
//
this.label3.Location = new System.Drawing.Point(129, 66);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 3;
this.label3.Text = "Volatility: 1-10";
//
// label4
//
this.label4.Location = new System.Drawing.Point(23, 66);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 23);
this.label4.TabIndex = 2;
this.label4.Text = "Price";
//
// txtTargetCellRow
//
this.txtTargetCellRow.Location = new System.Drawing.Point(26, 39);
this.txtTargetCellRow.Name = "txtTargetCellRow";
this.txtTargetCellRow.Size = new System.Drawing.Size(79, 20);
this.txtTargetCellRow.TabIndex = 1;
this.txtTargetCellRow.TextChanged += new System.EventHandler(this.txtTargetCellRow_TextChanged);
//
// txtTargetCellColumn
//
this.txtTargetCellColumn.Location = new System.Drawing.Point(132, 38);
this.txtTargetCellColumn.Name = "txtTargetCellColumn";
this.txtTargetCellColumn.Size = new System.Drawing.Size(79, 20);
this.txtTargetCellColumn.TabIndex = 2;
this.txtTargetCellColumn.TextChanged += new System.EventHandler(this.txtTargetCellColumn_TextChanged_1);
//
// txtVolatility
//
this.txtVolatility.Location = new System.Drawing.Point(132, 92);
this.txtVolatility.Name = "txtVolatility";
this.txtVolatility.Size = new System.Drawing.Size(79, 20);
this.txtVolatility.TabIndex = 4;
//
// txtPrice
//
this.txtPrice.Location = new System.Drawing.Point(26, 92);
this.txtPrice.Name = "txtPrice";
this.txtPrice.Size = new System.Drawing.Size(79, 20);
this.txtPrice.TabIndex = 3;
//
// btnEnter
//
this.btnEnter.Location = new System.Drawing.Point(26, 132);
this.btnEnter.Name = "btnEnter";
this.btnEnter.Size = new System.Drawing.Size(75, 23);
this.btnEnter.TabIndex = 5;
this.btnEnter.Text = "Enter";
this.btnEnter.UseVisualStyleBackColor = true;
this.btnEnter.Click += new System.EventHandler(this.button1_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(132, 132);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23);
this.btnStop.TabIndex = 6;
this.btnStop.Text = "Stop";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Conditions
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(233, 167);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnEnter);
this.Controls.Add(this.txtPrice);
this.Controls.Add(this.txtVolatility);
this.Controls.Add(this.txtTargetCellColumn);
this.Controls.Add(this.txtTargetCellRow);
this.Controls.Add(this.label3);
this.Controls.Add(this.label4);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Conditions";
this.Text = "Ticker Simulator";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtTargetCellRow;
private System.Windows.Forms.TextBox txtTargetCellColumn;
private System.Windows.Forms.TextBox txtVolatility;
private System.Windows.Forms.TextBox txtPrice;
private System.Windows.Forms.Button btnEnter;
private System.Windows.Forms.Button btnStop;
}
}
============================================================
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 Ticker_Simulation
{
public partial class Conditions : Form
{
int TargetRow;
int TargetColumn;
static volatile bool StartStop;
double intPriceChange;
double intPrice;
double OldPrice;
double intVolatility;
public Conditions()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
btnEnter.Text = "Running";
string price = txtPrice.Text;
intPrice = Convert.ToDouble(price);
string volatility = txtVolatility.Text;
intVolatility = Convert.ToDouble(volatility);
intPriceChange = ((intVolatility / 10) / 100) * intPrice;
StartStop = false;
Thread myThread = new Thread(myThreadedMethod);
myThread.Start();
//this.Hide();
}
public void myThreadedMethod()
{
while (!StartStop)
{
Random firstRandomNumber = new Random();
double x = firstRandomNumber.Next() % 100;
if (x % 2 == 0)
{
if (Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value == null)
{
OldPrice = intPrice;
}
else
{
OldPrice = Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value;
}
try
{
double NewPrice = OldPrice - intPriceChange;
Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value = NewPrice;
}
catch
{
}
}
else
{
if (Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value == null)
{
OldPrice = intPrice;
}
else
{
OldPrice = Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value;
}
double NewPrice = OldPrice + intPriceChange;
Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow, TargetColumn).Value = NewPrice;
}
Thread.Sleep(500);
}
}
private void txtPrice_TextChanged(object sender, EventArgs e)
{
}
private void txtVolatility_TextChanged(object sender, EventArgs e)
{
}
private void txtTargetCellRow_TextChanged(object sender, EventArgs e)
{
try
{
string strTargetRow = txtTargetCellRow.Text;
TargetRow = Convert.ToInt32(strTargetRow);
}
catch
{
txtTargetCellRow.Text = "";
Error theError = new Error();
theError.Show();
}
}
private void btnStop_Click_1(object sender, EventArgs e)
{
//Globals.ThisAddIn.Application.ActiveSheet.Cells(TargetRow +1, TargetColumn+1).Value = StartStop;
}
private void btnStop_Click(object sender, EventArgs e)
{
btnEnter.Text = "Enter";
StartStop = true;
}
private void txtTargetCellColumn_TextChanged_1(object sender, EventArgs e)
{
try
{
string strTargetColumn = txtTargetCellColumn.Text;
TargetColumn = Convert.ToInt32(strTargetColumn);
}
catch
{
txtTargetCellColumn.Text = "";
Error theError = new Error();
theError.Show();
}
}
}
}