Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:17:46+00:00 2026-06-11T14:17:46+00:00

in my main form, i have a timer which update the value in the

  • 0

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
context

Error 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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:17:47+00:00Added an answer on June 11, 2026 at 2:17 pm

    If you move your method outside the Form1 class you’ll need to add at least the control as a parameter to the method, since BeginInvoke and InvokeRequired are called on this. you can just pass the textbox array and call the invokes on it:

    public void writeToTextBox(int j, string value, TextBox[,] textBoxes)
    {
        TextBox textBox = textBoxes[1, j];
        if (textBox.InvokeRequired)
        {
            textBox.BeginInvoke(new StringParameterDelegate(writeToTextBox), new object[] { j, value, textBoxes });
            return;
        }
        // Must be on the UI thread if we've got this far
        textBox.Text = value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a System.Windows.Forms.Timer component on an application's main form which checks every
I need help with the following design. Basically I have a Main form which
I have a public variable testStr in my applications main form. I have a
I have a project which has a main form and some other forms. When
I have JavaScript Function in which I check for value of a stageId box
I have a main thread that creates a form object which creates and sets
I have created one application in which Main Form Calls Sub Form on FormShow
I have a main Form. There is a linklabel. If i click on it,
I have a Main form.I want to launch another form from it and launch
I have a main form, and a floating child form that is non-modal. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.