Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to use a Control Library I created in a WinForms C# app.
I am on Win 7 VS 2019 ver 16.11.24 Net Frame 4.8 C# 7.3
I tried to create the Library by selecting new project and
Win Forms Class Library & Win Forms Control Library denied outdated framework?
OK so I right clicked the project Add User Control Library GREAT.
The code flow is frmStart tests if DB exists NO btnCreateDB calls the library
DBControl NO DB or Table is created.
Code on DBControl works in another form.
So is the code not executing due to how it was added to DBControl?
I have not implemented a Using statement for DBControl

What I have tried:

Form Start Button Click to call DBControl
private void btnCreateDB_Click(object sender, System.EventArgs e)
 {
     Hide();
     DBControl dB = new DBControl();
     dB.Show();
     //UserControl dB = new UserControl();
     //DBControl dB = new DBControl();
     //dB.Show();
     //form.Show();
     //frmMakeDB fMDB = new frmMakeDB();
     //fMDB.Show();
 }

Code in DBControl
namespace ACLSTest
{
    public partial class DBControl : UserControl
    {
        public static string dbName = "APeople.db";
        public DBControl()
        {
            InitializeComponent();
        }
        private void UserControl_Load(object sender, EventArgs e)
        {
            makeDB();
            makeFriendsTable();
        }

        private static void makeDB()
        {
            throw new NotImplementedException();
        }
        private static void makeFriendsTable()
        {
            throw new NotImplementedException();
        }

        public class MakeDB
        {
            public  void makeDB()
            {
                using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
                    if (!File.Exists(dbName))
                    {
                        try
                        {
                            conn.Open();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
            }

            public  void makeFriendsTable()
            {
                string create_table = @"CREATE TABLE IF NOT EXISTS FriendsData(
                        FID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                        fxFirstName TEXT,
                        fxLastName TEXT,
                        fxEmail TEXT,
                        fxInfo TEXT)";

                string dbTable = "FriendsData";
                using (SQLiteConnection conn = new SQLiteConnection($"Data Source = '{dbName}';Version=3;"))
                    if (!File.Exists(dbTable))
                    {
                        try
                        {
                            conn.Open();
                            SQLiteCommand cmd = new SQLiteCommand(create_table, conn);
                            cmd.ExecuteNonQuery();

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
            }
        }
    }
Posted
Updated 3-Apr-23 16:19pm

I ran a test:
1. Created a new solution with a Windows Form App (.Net Framework) project
2. Added a Windows Form Control Library (.Net Framework) project
3. in the Windows Form Control Library (.Net Framework) project I added a label to the UserControl1
4. In the Windows Form App (.Net Framework) project I added a reference to the Windows Form Control Library (.Net Framework) project
5. I built the solution
6. Dropped the UserControl1 on Form1 and ran the project.

Worked fine.

Please be careful when selecting project types. If you are doing .Net Framework 4.8, make sure that you are selecting the project types labeled (.Net Framework).

A final note, I would strongly suggest that you use Dot Net Core and not .Net Framework projects. They are faster and better maintained. .Net Framework is only their for legacy projects.
 
Share this answer
 
Comments
Choroid 12-Mar-23 16:33pm    
OK I am close but the Windows Form Control Library gets called but it does not acknowledge.
I used your solution and followed this with a label
https://stackoverflow.com/questions/56573520/handle-class-library-project-through-a-windows-form-project
Code on Control
public class Form1
{
private object lblWFC;

public void UpdateTextBoxWith(string newText)
{
lblWFC = newText;
}
}

private void UserControl1_Load(object sender, EventArgs e)
{
lblWFC.Text = "Hi we are here";
}
Code on frmStart
private void btnCreateDB_Click(object sender, System.EventArgs e)
{
Hide();
UserControl userControl = new UserControl();
//var form = new UserControl.UserContrlo();
userControl.UpdateTextBoxWith("New Text");
userControl.Show();
ERROR UserControl does not contain a definition for UpdateTextBoxWith are you missing an assembly reference ? I used WinFormsControlLibrary NOT WinFormsClassLibrary Thanks for the reply
Graeme_Grant 12-Mar-23 17:44pm    
If you followed my example, it works.

Using a Control Library makes sure that you have all the correct framework references in the project. Using a Class Library is like taking the training wheels off, you need to know what you are doing - you are not there yet.

The issue that you have now is your code is causing the error to be thrown. You need to remove (comment out) code to find which line of code is doing it.

Also, do the following:
Debug > Window > Exception Settings > tick "Common Language Runtime Exceptions"

This will stop at any exceptions, currently, this is not happening. It may be annoying but will force you to fix the issues.
Choroid 12-Mar-23 21:45pm    
I know the Error type Compiler Error CS1061 Tried a few fixes with this link
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
The Binding Extension Method and Layer-Specific Functionality Code in frmStart Parent Project FWW
public string newText { get; set; }
public void btnCreateDB_Click(object sender,System.EventArgs e)
{
newText = "Hello";
Hide();
UserControl userControl = new UserControl();
userControl.UpdateTextBoxWith(newText);
userControl.Show();

This is the line that generates the ERROR tried changing a number of ways to fix the formatting
NO LUCK will continue any other suggestions ? Thanks
Graeme_Grant 12-Mar-23 23:54pm    
Why are you doing that? You don't use UserControls that way ... I have already explained how to use it.

If you want to do it manually, look at the code generated in the [form_name].designer.cs file. Particularly where it adds the control to the form. I won't post the code here, I have given you which file. You need to learn where to look.
Choroid 13-Mar-23 2:18am    
Well I added this private Delegate UpdateTextBoxWith()
and re wrote this userControl.Invoke(UpdateTextBoxWith(),newText);
here is the ERROR NuGet Error NU1201
I have net frame work 4.8 Severity Code Description Project File Line Suppression State
Error NU1201 Project WinFormsControlLibrary is not compatible with net48 (.NETFramework,Version=v4.8) / win. Project WinFormsControlLibrary supports: net5.0-windows7.0
This only happens when I use delegate and Invoke OK I get the UserControls I was trying in the
original code to create a SQLite DB YES I diverted to just see if I could communicate with the
UserControls Project Sorry my mistake
Here is part of the solution for the question, I can create the DB and Table.
I am still working on the DB CRUD function. I created two projects a VB.Net &
a Class Library Project with a from. Here is the code in the VB.Net project
private void frmStart_Load(object sender, EventArgs e)
{
    if (!File.Exists(dbName))
    {
        btnCreateDB.Visible = true;
        btnAdd.Visible = false;
        Width = 205;
        Height = 115;
        Refresh();

    }
    else
    {
        Width = 460;
        Height = 320;
        Refresh();
        btnCreateDB.Visible = false;
        btnAdd.Visible = true;
        tbMessage.Text = "Select Function to Manage Peoples Information";
    }
}

private void btnCreateDB_Click(object sender, EventArgs e)
{
    Hide();
    var form = new DBControl.frmHDB();
    {
        form.addDB();
        form.ShowDialog();
    }
    Show(); // Not executed until after the form is closed.
    frmStart_Load(null, EventArgs.Empty);
}

Not over joyed with this line of code the frmStart only wants to Load once.
frmStart_Load(null, EventArgs.Empty);

The code in the Class Library Project called from the btnCreateDB on frmStart
public void addDB()
 {
     if (!File.Exists(dbName))
     {
         makeDB();
         makeFriendsTable();
         tbMessage.Text = "I Got Loaded";
     }
 }

I will comment on what I found very unusual when I was creating these projects
I created each separate from the other and added SQLite to the form in the Class Library then loaded the VB.Net project in VS 2019 and added the Existing Class Library project. The ERROR
Unable to load DLL 'SQLite.Interop.dll

So I am guessing it was not found! So I now have the two projects joined and I deleted the SQLite install and proceeded to reload SQLite. This time NeGet wanted the install in both projects?
It runs and creates the DB but does not show in the Solution Explore till I used the line of code I was not over joyed about or I added one record to the DB. I could see it in the source files.
Thanks to Graeme Grant who kept pushing
 
Share this answer
 
If you want to see an answer to my question here is a link
C# passing integer variable from one project to another project[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900