-1

I'm fairly new to programming so please excuse my lack of knowledge. What's the easiest way to pass a variable in a class to a form? I am wanting to send sScanNum string to form button. Right now i have the button launching a qr generator and wanting to auto populate the value for the qr code.

foreach (ViewSheet cs in collsheets)
{
    //string jobcontnumb = cs.LookupParameter("JobControlNumber").ToString();

    string sHeetNum = cs.SheetNumber.ToString();

    foreach (AssemblyInstance ai in Spool)
    {

       string jobConNum = ai.LookupParameter("JobControlNumber").AsString();
       ////string sHeetNum = cs.SheetNumber.ToString();
       string sScanNum = "*" + (jobConNum) + "-" + (sHeetNum) + "*";

       TaskDialog.Show(sScanNum, sScanNum);
       break;

    }    
}
KJSR
  • 1,679
  • 6
  • 28
  • 51

2 Answers2

1

For starters you should be creating an instance of the form before you call Show. Then you can create properties in the form class and a constructor that takes the arguments and sets the values etc before you show it.

For instance

public class TaskDialog : Form
{
    public string ScanNumber { get; set; }

    public TaskDialog()
    {
        InitializeComponent();
    }

    public TaskDialog(string scanNumber)
    {
        this.ScanNumber = scanNumber;
        InitializeComponent();
    }
    ...
}

Then you can instantiate the form and show it like so

...
var taskDialog = new TaskDialog(sScanNum);
taskDialog.Show();
hawkstrider
  • 4,141
  • 16
  • 27
  • Having a constructor with parameters breaks things. Better set the property after the initialization with `taskDialog.ScanNumber = sScanNum;` before the `.Show()` call. – John Alexiou Aug 06 '19 at 14:35
  • My bad, I forgot to add InitializeComponent(). That is all you need to prevent issues. Nothing wrong with setting it after as well. – hawkstrider Aug 06 '19 at 14:38
  • The WinForms designer cannot operate on a form that does not have a parameterless constructor (default constructor). – John Alexiou Aug 06 '19 at 15:01
  • Fair enough, a parameterless one can be added as well. – hawkstrider Aug 06 '19 at 15:14
  • I actually just tested and verified that in VS 2017 and there is no issue with a parameterized constructor in a windows form that I can see. No errors and the designer continues to work fine with only a parameterized constructor. Perhaps you can elaborate? – hawkstrider Aug 06 '19 at 15:21
  • Interesting. I did a test myself too. In the past, the designer would produce an error if it could not create an instance of the form. Somebody got smart at MS and managed to roll over this issue. I learned something today. – John Alexiou Aug 06 '19 at 15:37
0

Here is how I transfer data from/to forms. I use the principle of encapsulation and define a single property that contains all the data I want to transfer in a single class

public class ScanInfo
{
    public int JobControl { get; set; } = 0;
    public int Sheet { get; set; } = 0;
    public DateTime Stamp { get; set; } = DateTime.Now;
    public override string ToString() => $"*{JobControl}-{Sheet}*";
}

then fill in the data you need (example below has hardcoded values)

var scan = new ScanInfo()
{
    JobControl = 10302,
    Sheet = 4
};

and set a property on the form with the data

var dlg = new TaskDialog();
dlg.Scan = scan; // now the form has a reference to the data

The property can read and/or write data and update itself if needed at the same time

public partial class TaskDialog : Form
{
    ScanInfo info;

    public TaskDialog()
    {
        InitializeComponent();
    }

    public ScanInfo Scan
    {
        get => info;
        set => SetValues(value);
    }

    void SetValues(ScanInfo scan)
    {
        this.info = scan;
        if (info != null)
        {
            this.jobControlTextBox.Text = info.JobControl.ToString();
            this.sheetTextBox.Text = info.Sheet.ToString();
            this.stampDateTimePicker.Text = info.Stamp.ToString();
        }
        else
        {
            this.jobControlTextBox.Clear();
            this.sheetTextBox.Clear();
            this.stampDateTimePicker.Text = DateTime.Now.ToString();
        }
    }

}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133