0

In my main window, I create a thread, which is executing a while() loop. The main tasks have two parts: receive data from socket and show it on GUI.

Now I need to show the data on another window at the same time. So I create it first like below.

ShowForm showForm = new ShowForm();

public MainWindow()
{
    InitializeComponent();

    mainThread();

    showForm.Show();
}

And send the data to the showForm like below: (coordinateValue is generated within main window)

showForm.setter(coordinateValue);

And in the code of ShowForm.Designer.cs:

int xValue;

public void setter(int val)
{
    xValue = val;
}

Now I don't know how to show the xValue on the showForm repeatedly (needs to be updated timely), e.g. a textBox or convert the xValue to coordinate and show it on a pictureBox. And in the meanwhile, the main Window's while() loop should continue to receive data and show it on its GUI.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tom Xue
  • 3,169
  • 7
  • 40
  • 77
  • Maybe this can help you you. http://stackoverflow.com/questions/17811112/transfer-numbers-in-textbox-to-labels-in-another-form-c-sharp-windows-forms – C0d1ngJammer Nov 10 '13 at 14:24

3 Answers3

0

You can create an event in your MainWindow. And subscribe to that event in your ShowForm.

Than whenever your data changes MainWindow should raise that event. Just remember that if you get the data in another thread you can't just pass it to the GUI which runs on the main thread. In that case you will want to use a Dispatcher.

Andrzej Gis
  • 13,706
  • 14
  • 86
  • 130
0

You might want to use the Timer class. It can execute methods (Tick event) in even intervals.

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
0

I have wrote a sample that explains how to transfer data from one form to another using an event. If it runs in another thread you should use Invoke method in your control to prevent errors.

public partial class AdditionalForm : Form
{
    private Label l_dataToShow;

    public Label DataToShow { get { return l_dataToShow; } }

    public AdditionalForm()
    {
        InitializeComponent();

        SuspendLayout();
        l_dataToShow = new Label();
        l_dataToShow.AutoSize = true;
        l_dataToShow.Location = new Point(12, 9);
        l_dataToShow.Size = new Size(40, 13);
        l_dataToShow.TabIndex = 0;
        l_dataToShow.Text = "Data will be shown here";
        Controls.Add(l_dataToShow);
        ResumeLayout();
    }
}

public partial class MainForm : Form
{
    private AdditionalForm af;
    public MainForm()
    {
        InitializeComponent();

        SuspendLayout();
        txtbx_data = new TextBox();
        txtbx_data.Location = new System.Drawing.Point(12, 12);
        txtbx_data.Name = "txtbx_data";
        txtbx_data.Size = new System.Drawing.Size(100, 20);
        txtbx_data.TabIndex = 0;
        Controls.Add(txtbx_data);
        ResumeLayout();

        txtbx_data.TextChanged += new EventHandler(txtbx_data_TextChanged);
        af = new AdditionalForm();
        af.Show();
    }
    /// <summary>
    /// The data that contains textbox will be transfered to another form to a label when you will change text in a textbox. You must make here your own event that will transfer data.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void txtbx_data_TextChanged(object sender, EventArgs e)
    {
        af.DataToShow.Text = txtbx_data.Text;
    }
}
Wallstrider
  • 856
  • 1
  • 7
  • 22