Resolved Using a form that's in a class library

LupusRex

Member
Joined
Oct 30, 2019
Messages
16
Programming Experience
Beginner
Hi
I'm new to C# and oop (though I have coded in Autoit for more that a decade, so I do have a basic understanding of coding) , but have read and watched tuts, and otherwise used google to get answers, but this question I haven't been able to find an answer to.

My problem/question.
I have created a simple form with 2 text boxes and a button. The form is in a class library, course I want to use it in several programs. In the same library I have another class file, that I want to handle all calls to my lib.

My problem is that I don't know how to "pop" the form, and how to use the button to get the input data from the text boxes, and then handle that data within my class file.

I want to use the library to handle the main programs license.

My idea is:

User launches the main program.
The program calls my license lib -> the lib checks if a license for the program exists in the database, if not "pop" the form to allow the user to enter User Name and License.
When the user clicks the form button, the lib calls a license server to check if the license is valid, if the lic is valid it writes it to the database, and shows a License is Valid message to the user.
Else if the license exist, continue the main program and drop the lic lib.

I hope that it clear what I try to achieve, and that some one can help point me in the right direction :)


Cheers
/LR
 
You just simply add a reference to that class library in your program. Then in the program you would call your "class file". The term is "show" a form, not "pop" a form. To do that you would call ShowDialog on an instance of the form that you want to show.

What do you mean by "drop the lic lib"?
 
My problem is that I don't know how to "pop" the form
C#:
            var x = new Form1();
            /** x.ShowDialog(); **/
            x.Show();
x is a new instance of Form1.

If using the ShowDialog(), know that this will block from clicking back on the previous form. I suppose this is alright though since you will likely only be using one window at a time.

Show() does not block you from clicking between windows.
how to use the button to get the input data from the text boxes
You simply access the instance you created and apply it to the property where you want to get/set the value.
C#:
x.textBox1.Text = "Foo";
Screenshot_30.jpg

If you are a beginner, then I would recommend getting some lessons, or studying a recommended book on the subjects of working with forms and transferring data between forms. If you are working with another library, you will likely end up in a situation where crossthreading will become a problem for you as you share values between forms. Not something I'd advice a beginner to start meddling in if you're still learning. In terms of interacting with your server, what have you proposed or thought of for that?
 
Hi Skydiver
I have in my test code created a ref to my dll file, and I could show my gui, but my problem is how to get the data from my text boxes, when the user clicks the activate button.

Hi Sheepings

I have a LicenseHandling library, where I have the form code
and a LicenseHandle.cs class file

I have the following code in my form:
C#:
namespace LRLibary.License
{

    public partial class LicenseGUI : Form
    {

        internal LicenseGUI()
        {
            InitializeComponent();
        }

        private void btnActivate_Click(object sender, EventArgs e)
        {
            string userName = inpUserName.Text;
            string licenseKey = inpLicKey.Text;

            // Handle the inputs
            this.Close();
        }

    }

}

And in my licenseHandling.cs

C#:
public class LicenseHandling
    {
        public string UserName { get; set; }
        public string LicenseKey { get; set; }
        private readonly string regKey = @"HKEY_CURRENT_USER\Software\LR\License";

        public LicenseHandling(string progGuid)
        {
            // 1. we need to check if a license exist in the registry.

           bool licenseExists = CheckLicenseExist(progGuid);
            if (!licenseExists)
            {
                // Show the License GUI and get User Name and License Key
                var myForm = new LicenseGUI();
                myForm.ShowDialog();
               
            }
            // The license exists, so now we get the license key
            string licenseFromReg = GetLicenseDataFromRegistry(progGuid);
        }

I tried myForm.inpUserName (where inpUserName is the user name text box), but inpUserName don't show up when I try to enter it :(

Yes I'm brand new to C#, but I have read and read, and watched tuts. But non of them did help me with using forms from the class lib.
Getting lessons is difficult, corse there is no place within 100km of my home where they teach programming at evening classes, and getting back to school is a bit to late for me (I'm 40+)

I already have a license handling working, written in php and i use it for my Autoit programs, the main reason I'm trying to learn/use C# is mostly due to the laziness of the majority of AV Vendors corse in the last ~1-2 years a lot of my autoit programs has been flagged as virus, though they are not - and it's a pain in the *** to submit them to the AVV each time I do an update to one of them.

I also have a test code for license handling with server in C#, that works :)

Cheers
/LR
 
Last edited:
The problem you are having has nothing to do with using forms from a class library. You would have the same issue if the form was in your main program.

Anyway, the standard approach from the 90's and early 2000's is to have your LicenseGUI expose two properties: UserName and LicenseKey the same way your LicenseHandling class is exposing those two properties. When the user presses the OK button in the dialog, you fill in the properties.

The modern approach in the 2010's and onwards is to use data transfer object. This data transfer object would the Model of a Model-View-Controller (MVC), Model-View-Presenter (MVP), or Model-View-View Model (MVVM) pattern.

Here's a tutorial from another site that I frequent:
The Right Way To Get Values From Form1 To Form2
 
Last edited:
and getting back to school is a bit to late for me (I'm 40+)
It is never too late. My wife and I are in our late 40's. She started her Master's Degree in Nursing two years ago and finished it a couple of months ago. And that's juggling a fulltime job, two toddlers, a kid in college, and a husband with crazy work hours. I'm so proud of her!
 
The problem you are having has nothing to do with using forms from a class library. You would have the same issue if the form was in your main program.

Anyway, the standard approach from the 90's and early 2000's is to have your LicenseGUI expose two properties: UserName and LicenseKey the same way your LicenseHandling class is exposing those two properties. When the user presses the OK button in the dialog, you fill in the properties.

The modern approach in the 2010's and onwards is to use data transfer object. This data transfer object would the Model of a Model-View-Controller (MVC), Model-View-Presenter (MVP), or Model-View-View Model (MVVM) pattern.

Here's a tutorial from another site that I frequent:
The Right Way To Get Values From Form1 To Form2
Hi Skydiver
That tut was precise what I needed <3

It is never too late. My wife and I are in our late 40's. She started her Master's Degree in Nursing two years ago and finished it a couple of months ago. And that's juggling a fulltime job, two toddlers, a kid in college, and a husband with crazy work hours. I'm so proud of her!

For me it's to late, if I go back to school my income would drop to 6-7k pr month, but my expenses is ~11k pr month.
And there is only me to earn and pay, if I had had a Wife or I was living with a girl friend who also added to the expenses, it wouldn't be a problem, but as a single it's just not possible :(

Gratz on your wife's Master degree (y)

Cheers
/Rex

Edit: How do I change Question to Solved? Couldn't find anything under help :(
/R
 
Last edited:
Hi Sheepings
I can't edit my first post, was the first thing I tried.

Might be that I'm to new to the forum to do such editing.

/LR
 
Back
Top Bottom