Return input from Windows Form

Grrr

Member
Joined
Apr 20, 2018
Messages
8
Programming Experience
Beginner
Hello everyone,
I'm new here and a newbie at C#.

So heres the thing - I've created a 'Class Library' project that uses(runs) a Windows Form, and I would like to gather its input and use it in Class1.cs.
The codes are basic (almost no coding done) :

Class1.cs
C#:
using System;using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace TestClassLib
{
    public class Class1
    {
        public static void WinFormTest()
        {
            System.Windows.Forms.Application.Run(new Form1());


        }
    }
}

Form1.cs (the design contains a single textbox and a button)
C#:
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 TestClassLib
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            // return textBox1.Text; // it throws an error obviously
        }
       
    }
}

My goal is upon the button1_Click event to store the textBox1's value and pass that value to Class1.cs, aswell close the WindowsForm dialog.
But obviously I don't know to do any of it!
 
Why exactly are you calling Application.Run? That is not a normal thing for a library to do. Normally you create an instance of a form and call its Show or ShowDialog method. In your case, it sounds like ShowDialog is what you should calling. ShowDialog blocks until the form closes, at which point you can retrieve whatever data you want from it via properties and/or methods. If you want the contents of 'textBox1' on the form in Class1 then Form1 should expose it via a property and Class1 can then get the value of that property. A 'return' statement in Form1 only makes sense if it's in a function that will be called by Class1. A function returning a value is effectively equivalent to a property returning a value.
 
Thanks for your feedback, jmcilhinney!


Why exactly are you calling Application.Run? That is not a normal thing for a library to do.


I'm very new to C#, and It seems that I've followed the wrong tutorial.. (the guy just mentioned how to call a form from the main .cs program - but he didn't elaborate any further).




Normally you create an instance of a form and call its Show or ShowDialog method.
In your case, it sounds like ShowDialog is what you should calling. ShowDialog blocks until the form closes, at which you can retrieve whatever data you from it via properties and/or methods.


Thanks for explaining how its done properly, now I'll know!




If you want the contents of 'textBox1' on the form in Class1 then Form1 should expose it via a property and Class1 can then get the value of that property.


I see - I think I've followed you here (see code) -


Class1.cs:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using System.Windows.Forms;


namespace ClassLibrary1
{
  public class Class1
  {
    public static void WinFormTest()
    {
      // System.Windows.Forms.Application.Run(new Form1()); // This shows up our form (but this is not the standard approach)
      
      // This is the normal Form call:
      var f1 = new Form1();
      var result = f1.ShowDialog(); // store our dialog result
      if (result == DialogResult.OK) // check if the user accepted  his input
      {
        string txt = f1.ReturnValue; // obtain our customly-created property's value
        MessageBox.Show("The input from our textbox is: " + txt); // display our value (just a test)
      }
    }
    
  }
}


Form1.cs:
C#:
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 ClassLibrary1
{
  public partial class Form1 : Form
  {
    
    public string ReturnValue { get; set; } // declare a new string property
    
    public Form1()
    {
      InitializeComponent();
    }
    
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
      // store the value into our property and return OK for the DialogResult
      this.ReturnValue = textBox1.Text;
      this.DialogResult = DialogResult.OK;
      this.Close();
    }
  }
}




A 'return' statement in Form1 only makes sense if it's in a function that will be called by Class1. A function returning a value is effectively equivalent to a property returning a value.


I don't think I'll understand what you are saying, although I tried multiple variations to implement a function with a 'return' statement in Form1:


Class1.cs:
C#:
namespace ClassLibrary1
{
  public class Class1
  {
    public static void WinFormTest()
    {
      var f1 = new Form1();
      var result = f1.ShowDialog(); 
      if (result == DialogResult.OK) 
      {
        string txt = f1.ReturnOurString(); 
        MessageBox.Show("The input from our textbox is: " + txt);
      }
    }
    
  }
}


Form1.cs:
C#:
namespace ClassLibrary1
{
  public partial class Form1 : Form
  {
    
    public static string ReturnOurString()
    {
      RetVal = textBox1.Text;
      return RetVal;
    }
    
    public Form1()
    {
      InitializeComponent();
    }
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
      this.DialogResult = DialogResult.OK;
      this.Close();
    }
  }
}


I'm getting errors everywhere (and I read them), but I don't think I'll get anywhere without knowing the proper approach with some actual code.
 
I'm very new to C#, and It seems that I've followed the wrong tutorial.. (the guy just mentioned how to call a form from the main .cs program - but he didn't elaborate any further).
That's for an application, not a library. I guess the first question is whether you should be creating a library at all. An application project compiles to an EXE that you then run. A library project compiles to a DLL, which is not generally intended to be run directly but rather is to be used by an EXE or another DLL. Is this library project being referenced by another project? If not, you created the wrong type of project to start with. If you want an EXE that you can run a Windows Forms application from then you create a Windows Forms Application project. That will add a Main method with a Form1 and call Application.Run in that Main method by default.
I'm getting errors everywhere (and I read them), but I don't think I'll get anywhere without knowing the proper approach with some actual code.
The code looks generally OK so if you're seeing specific errors then you need to tell us what they are and where they are so we can address them specifically.
 
Back
Top Bottom