in my main form, i have a timer which update the value in the text box every few seconds, the code are the following:
public partial class Form1 : Form
{
static int column = 2;
static int row = 100;
System.Windows.Forms.TextBox[,] textbox = new System.Windows.Forms.TextBox[column, row];
delegate void StringParameterDelegate(int j, string value);
DisplayTimer displayTimer = new DisplayTimer();
System.Threading.TimerCallback displayCallback;
System.Threading.Timer displayTimerThread;
public Form1()
{
InitializeComponent();
//set the array for name and update time
for (int k = 0; k < column; k++)
{
for (int j = 0; j < row; j++)
{
textbox[k, j] = new System.Windows.Forms.TextBox();
textbox[k, j].Size = new Size(160, 18);
textbox[k, j].Name = "textbox_" + k + "_" + j;
if (j >= 100)
{
textbox[k, j].Location = new System.Drawing.Point((k * 160) + 20, (j * 18) + 30);
}
textbox[k, j].Visible = true;
Controls.Add(textbox[k, j]);
}
}
displayCallback = new System.Threading.TimerCallback(timeDisplay);
displayTimerThread = new System.Threading.Timer(displayCallback, displayTimer, 3000, 3000);
// more code
}
public void timeDisplay(object timerObject)
{
DisplayTimer t = (DisplayTimer)timerObject;
for (int j = 0; j < t.row; j++)
{
string value = t.outputTime[j].ToString("yyyy/MM/dd HH:mm:ss.fff");
writeToTextBox(j, value);
}
}
public void writeToTextBox(int j, string value)
{
if (InvokeRequired)
{
BeginInvoke(new StringParameterDelegate(writeToTextBox), new object[] { j, value });
return;
}
// Must be on the UI thread if we've got this far
textbox[1, j].Text = value;
}
now the above code runs fine and no error occurs. However i have 3 forms in my program use the same method timeDisplay and writeToTextBox, i have to duplicate the code 3 times in the form. I have tried to refactor the 2 methods and put it into a new class. i got the following errors for my writeToTextBox method.
Error 1 The name ‘InvokeRequired’ does not exist in the current
contextError 2 The name ‘BeginInvoke’ does not exist in the current context
Error 3 The name ‘textbox’ does not exist in the current context
how do i refactor it effectively? some working snippet of code would be very helpful, thanks in advance
EDIT: below is the correct code for the question: it runs fine without any error. Thanks Tudor
class DisplayTimer
{
public int numberOfRow = 0;
public int column = 0;
public DateTime[] outputTime;
public System.Windows.Forms.TextBox[,] textbox;
delegate void StringParameterDelegate(TextBox textbox, string value);
public void timeDisplay(object timerObject)
{
DisplayTimer t = (DisplayTimer)timerObject;
for (int j = 0; j < t.numberOfRow; j++)
{
string value = t.outputTime[j].ToString("yyyy/MM/dd HH:mm:ss.fff");
if (value != "0001/01/01 00:00:00.000")
{
writeToTextBox(textbox[column, j], value);
}
}
}
public void writeToTextBox(TextBox textbox, string value)
{
if (textbox.InvokeRequired)
{
textbox.BeginInvoke(new StringParameterDelegate(writeToTextBox), new object[] { textbox, value });
return;
}
// Must be on the UI thread if we've got this far
textbox.Text = value;
}
}
If you move your method outside the
Form1class you’ll need to add at least the control as a parameter to the method, sinceBeginInvokeandInvokeRequiredare called onthis. you can just pass the textbox array and call the invokes on it: