blah blah blah is here! blah blah » Close

up0down
link

I have this little code I wrote:
using System;
using System.Windows.Forms;
using System.IO.Ports;
namespace serialport
{
public partial class Form1 : Form
{
private SerialPort port = new SerialPort("COM3", 9600, Parity.Odd, 8, StopBits.One);
public Form1()
{
InitializeComponent(); port.Open();
}
private void buttonSS_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled; //on/off event
}

private void timer1_Tick(object sender, EventArgs e)
{
port.DtrEnable = !port.DtrEnable;
if (port.DtrEnable)
{
timer1.Interval = (int)numericUpDown1.Value;//1 val
}
else
{
timer1.Interval = (int)numericUpDown2.Value;//2 val
}
}
Now, What I want is a little complicated because I want to store the values from inside timer1_Tick event handrler into some sort of container. I want to make 10 (or more) containers that can hold different values I choose from those timer.intervals(or numUpDown). Then, to be able to play them in order, one container after another. When first container finish its doings, then the second container with his custom values enter and start to run, then the third,etc.
The program is an application for a led who is connected to serial port(rs232) and it flashes from what values I send from here.
Thanks
~Teodor~

last answered 2 years ago

1 answers

up0down
link

Well, you could use a two dimensional int array to store everything. For example:
private int numContainers = 10;
private int numValues = 8;
private int[,] containers = new int[numContainers, numValues];
private int nextContainer = 0;
private int nextValue = 0;
private void timer1_Tick(object sender, EventArgs e)
{
port.DtrEnable = !port.DtrEnable;
if (port.DtrEnable)
{
timer1.Interval = (int)numericUpDown1.Value;//1 val
}
else
{
timer1.Interval = (int)numericUpDown2.Value;//2 val
}
containers[nextContainer, nextValue] = timer1.Interval;
nextValue++;
if (nextValue == numValues)
{
nextValue = 0;
nextContainer++;
if (nextContainer == numContainers)
{
timer1.Enabled = false;
// call method which sends values to device
}
}
}
Incidentally, I take it you realize that setting the timer1.Interval will change the interval before the Tick eventhandler is next called.

up0down
link

I dont know how to use lists. (or arrays) ... at all.
if you are kind enough to make the link with a listbox for me.
I dont have any method created there, to call. That is all the code I have and is ok but I dont have the listbox attached to it. To the right of that listbox I put an add button and a remove button. These btns must take the values from numUpDown's and store them as a new item into that list.
When I have (let's say 10) items in that list, I hit the buttonSS to play the list. :)
thanx again.

up0down
link

To add values to a listbox until you have 10, you could use this code:
private void btnAdd_Click(object sender, EventArgs e)
{
int value = (int)numericUpDown1.Value;
listBox1.Items.Add(value);
if (listBox1.Items.Count == 10)
{
buttonSS.PerformClick();
}
}

up0down
link

thanks Vulpes.
But I must learn listBox and arrays to really do what I want....hmmm its so hard.... eh I will learn it anyway. :)

This post was imported from csharpfriends, if you have a similiar question please ask it again.

All previous members have been migrated, hope you enjoy the new platform!

Feedback