0

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; }
    }
  }
}
Shasam
  • 127
  • 2
  • 7

2 Answers2

2

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.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Okay that sounds plausible.. however I am quite new at this.. do you think I should even have 2 instances of ModbusMaster? if I don't do the line ModbusMaster mb = new ModbusMaster(); then I have no way to get access to the openPort method.. however if this is right.. I have no idea how to pass a reference as you say to.. thanks for the help though – Shasam Jun 26 '13 at 15:20
  • You should only have one instance of it. It's going to be a bit hard for me to give you specific advice because I don't know (and won't have time to look at) your entire source code. I'll try to find a sample program though... brb – Matthew Watson Jun 26 '13 at 15:31
  • @lilSebastian Ok, have a look at this example I gave in this answer. You should be able to follow the steps to make a sample program which hopefully will give you some ideas: http://stackoverflow.com/a/15605436/106159 – Matthew Watson Jun 26 '13 at 15:34
  • Great, when I get to work tomorrow I will try and do this.. by the way that is my entire source code.. Thanks again – Shasam Jun 26 '13 at 17:03
  • @lilSebastian Did you want the ModbusMaster form to be totally separate from your main form (like a floating window)? Or did you want it to be part of the form (like the button is)? If so, you should add the User Control directly to the form using the Form Designer. You shouldn't need to manually code that part. If you *do* do that, then remove your existing code for it first. And then you should be able to do more or less what you're doing now (you wouldn't have to do all the steps in that link I gave you, but it's still worth a look ;) – Matthew Watson Jun 26 '13 at 17:16
  • What I want is to have my modbus master control to just be completely self contained and be able to be dragged into any form without additional code really.. It is just going to send a few commands out of the serialport and have like a status led on it.. So you think that if I drag my control onto the form from the designer I shouldn't have to do any additional code.. so how do I then access the methods of my control without doing what I did.. "mb = new ModbusMaster" – Shasam Jun 27 '13 at 07:56
  • 1
    I have figured it out.. You were right.. I didn't change the actual name of the control once I had dropped it onto the form.. it had a name which was modbusMaster1 and I was just assuming it was called ModBusMaster... I renamed it to mb and removed all the instantiating code and it works fine! Thanks a lot! – Shasam Jun 27 '13 at 07:59
1

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");
        }
    }
}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • This added a second modbus master control to my form and the text still didn't update :( – Shasam Jun 27 '13 at 07:55
  • Never mind fixed my problem (above). Thanks for your help though you taught me how to programmatically add controls to a form :) – Shasam Jun 27 '13 at 08:00
  • 1
    @lilSebastian if Mathew Watsons answer worked for you, you should be sure to click the CheckMark to mark it as answered. – Mark Hall Jun 27 '13 at 15:21