Professional Documents
Culture Documents
EN RICHTEXTBOX
private void imprimir_en_richtexbox(RichTextBox r, SolidColorBrush c, string s)
{
TextRange tr = new TextRange(r.Document.ContentEnd, r.Document.ContentEnd);
tr.Text = s;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, c);
r.AppendText(Environment.NewLine);
r.ScrollToEnd();
}
string mensaje = DateTime.Now.ToString("dd:MM:yyyy hh:mm:ss ") + "El sensor " +
posicion + " ha alcanzado una temperatura de " + t + " grados";
imprimir_en_richtexbox(registro_de_eventos, Brushes.Red, mensaje);
VALORES ALEATORIOS
Random r = new Random();
int fila = r.Next(0, 3); De 0 a 3
int temperatura = r.Next(100); Hasta 100
CREAR MATRIZ
int[,] matriz_temperaturas;
matriz_temperaturas = new int[4, 4];
matriz_temperaturas = new int[4, int.Parse(textBox1.Text)];
int[,] matriz_temperaturas = new int[4, 4];
int[,] matriz_temperaturas = new int[,] {{1, 2}, {3, 4}, {5, 6} {7, 8}};
int[,] matriz_temperaturas = new int[4, 2] {{1, 2}, {3, 4}, {5, 6} {7, 8}};
CREAR VECTOR
int[] vector;
vector = new int[4];
int[] vector = new int[4];
int[] vector = new int[] {1, 2, 3, 4};
CREAR TIMER
DispatcherTimer temperatura_timer = new DispatcherTimer();
temperatura_timer.Interval = new TimeSpan(0, 0, 0, 3, 0);
temperatura_timer.Tick += new EventHandler(ejecutar_cada_tick);
temperatura_timer.Start();
temperatura_timer.Stop();
CREAR BACKGROUNDWORKER
1. En el nivel de la clase se crea el worker:
BackgroundWorker worker = new BackgroundWorker();
4. Se aade funcin que se ejecuta cada 0.5 segundos (no en contacto con la interfaz):
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
}
else
{
textlabel.Content = "Done!";
if (worker.IsBusy != true)
{
worker.RunWorkerAsync();
}
else
{
worker.CancelAsync();
}
}
}
7. Comienzo y parada:
private void Start_Click(object sender, RoutedEventArgs e)
{
if (worker.IsBusy != true)
{
worker.RunWorkerAsync();
}
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
if (worker.WorkerSupportsCancellation == true)
{
worker.CancelAsync();
}
}
AADIR EVENTO A UNA IMAGEN (MISMO EVENTO DISTINTAS IMAGENES)
1. Asignar el mismo MouseDown a todas las imgenes:
private void a00_MouseDown(object sender, MouseButtonEventArgs e){}