简体   繁体   中英

How to change user control label.text from a form

Right so I have a user control called "ModbusMaster" and a form with literally a single button on it..

When I click the button I want to change the text of a label on my control..

However nothing happens..

Here is the main form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ModbusMaster_2._0
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    ModbusMaster mb = new ModbusMaster();

    public void button1_Click(object sender, EventArgs e)
    {
      mb.openPort("wooooo");
    }
  }
}

I am calling the method openPort and passing the string "wooo" to it..

here is my control

The text does not get updated :(:(:(

using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace ModbusMaster_2._0
{
  public partial class ModbusMaster : UserControl
  {
    string portName = "COM1"; //default portname
    int timeOut = 300;        //default timeout for response

    SerialPort sp = new SerialPort();

    public ModbusMaster()
    {
      InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      portLabel.Text = portName;
    }

    public void openPort(string port)
    {
      statusLabel.Text = port;
    }

/*
 *  Properties 
*/
    public string SerialPort //Set portname
    {
      get { return portName; }
      set { portName = value;}
    }
    public int TimeOut //Set response timeout
    {
      get { return timeOut; }
      set { timeOut = value; }
    }
  }
}

I think you must have two instances of ModbusMaster .

One of them is the one you can see on the display, and is NOT being updated.

The other one is the one you create in class Form1 with the line of code:

ModbusMaster mb = new ModbusMaster();

That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).

What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");

[EDIT]

Thinking about it - it's possible that you haven't instantiated another user control at all.

Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.

If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.

You are creating your UserControl but not assigning it to your Form's Control Collection. Try something like this in your Constructor.

namespace ModbusMaster_2._0
{
    public partial class Form1 : Form
    {
        ModbusMaster mb = new ModbusMaster();

        public Form1()
        {
            InitializeComponent();
            this.Controls.Add(mb); //Add your usercontrol to your forms control collection
        }

        public void button1_Click(object sender, EventArgs e)
        {
            mb.openPort("wooooo");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM