Call a method of a WinForm from a class?!

MoPraL

Newcomer
Joined
Aug 28, 2004
Messages
16
Hello,

I have a WinForm called frmMain and a class called IRCEngine, they are in the same namespace. In my class i want to add items in a listview which is in the frmMain. but it doesn't work : its doesn't add but if u put a messagebox IN the same method, the msg box is shown but the listview are not added...

frmMain.cs (winform)
Code:
public void AddUserInList(ListViewItem user)
{
    MessageBox.Show("MainForm marche : " + user.Text );
    this.oConsole.Text = user.Text;
    this.Console(user.Text);
    this.lstUsers.Items.AddRange( new ListViewItem[] { user } );
}

// other method i use in my class IRCEngine.
public void Console(string text)
{
    this.oConsole.AppendText("\n" + text);
}

IRCEngine.cs (my class)
Code:
frmMain MainForm = new frmMain();

public void Connect()
{
    this.MainForm.Console("-> Connecting...");
    try
    {
        ...
        ListViewItem item; // <==========================

        while (true)
        {
            while ((inputLine = reader.ReadLine()) != null)
            {
                if (...)
                {
                    ...
                }

                else if (...)
                {
                    ...
                    foreach (string user in split)
                    {
                        if (user.Length <= 5) { ...  }
                        else
                        {
                            item = new ListViewItem(new string[] { user, user.Substring(user.IndexOf('|') + 4)}, -1);
                            MainForm.AddUserInList(item); // <==========================
                        }
                        item = null;
                    }

                    Thread.Sleep(1000);
                }

                else
                {
                    
                } 
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}
ListView (lstUsers) & oConsole (richtextbox) are "Public" (modifiers).
So why its doesn't append or add items in the form from the class? and why its doesn't add items BUT SHOWS MY MESSAGEBOX which is in AddUserInList() ?

Thx a lot :)
 
Pass your form instance as a parameter to IRCEngine.Connect, and set that your global "MainForm" equal to this passed param - now you should have addressibility back to your actual form instance.

I'm assuming you're calling into IRCEngine from the current instance of frmMain.
 
It doesn't work :
private frmMain MainForm = new frmMain(); // i've tester many combinations but nothing..

public IRCEngine()
{
// something for me
_pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0";
}

public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname)
{
// HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
this.MainForm = MainForm2;
}
But on build it says an error :

Error : Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' K:\...\IRCEngine.cs
It underlines this (only IRCEngine) : public IRCEngine(...

It's frmMain (my form) and MaibnForm is the object i wanna use in my class representing my form frmMain. IRCEngine is my class. ALl are in the same namespace
 
MoPraL said:
It doesn't work :
Error : Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)' K:\...\IRCEngine.cs

I have run into this error. Pass the the form as a generic form and then cast it as your form. I.E.

Code:
 private frmMain MainForm; 
public IRCEngine()
{
// something for me
_pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0";
}

public IRCEngine(System.Windows.Forms.Form MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname)
{
// Now cast the passed var to your global one.
// Might need the namespace in front of the frmMain if its not in 
// the current namespace.
this.MainForm = (frmMain)MainForm2;
}
Hope that helps
 
Last edited:
MoPraL:

The error message you are receiving is telling you that the accessibility of the frmMain object itself is more strict than the constructor for IRCEngine. Your frmMain object class declaration must me internal or even private? Just make your frmMain class declaration public (jut to clarify: not your local MainForm variable, the frmMain class itself).
 
Last edited:
i've tested all your solutions but i have always the same error!
frmMain is my WinForm.. so i cant say "public partial class" because if i do it, it shows me an other error : "Can't write the output file..." so i can't!
habitually, it was easy to pass a form as parameter but in this case...

ps: it's impossible to change modifiers of a WinForm! the winform's controls have "Modifiers" and i can put Public, Private, Internal, etc. But the form, we can't :confused:

please, help!
 
MoPral:
I have many, many Forms that are internal or private. You can definitely control the accessibility of a Form just like any other class.

Any time I have received the error message you have depicted, it was because the accessibility of a method parameter was more strict than the accessibility of the method accepting the parameter.

Could you post your frmMain code and/or the code where the IRCEngine constructor is being called from?
 
Ok :
frmMain.Designer.cs
Code:
namespace SKYClient
{
    partial class frmMain
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
............. (that continue)

frmMain.cs
Code:
#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using SKYClient;

#endregion

namespace SKYClient
{
    partial class frmMain : Form
    {
        private IRCEngine IRC;
        private Thread tCnx;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Console("Appuie sur le bouton CONNECT...");
            txtHote.Text = txtPseudo.Text + "|" + cmbAge.Text + cmbDept.Text + cmbSexe.Text.ToLower().Substring(0, 1) + "0!" + txtUtilisateur.Text;
            Console("Host créé!");

            IRC = new IRCEngine(this, cmbServer.Text, txtPort.Text, txtPseudo.Text, cmbDept.Text, cmbSexe.Text, cmbAge.Text, txtUtilisateur.Text, txtHote.Text, txtNomReel.Text);

            tCnx = new Thread(new ThreadStart(IRC.Connect));
            tCnx.Start();
        }

IRCEngine.cs :
Code:
#region Using directives

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Windows.Forms;

#endregion

namespace SKYClient
{
    public class IRCEngine
    {
public Form mainForm = new frmMain();

        private frmMain MainForm;

        public IRCEngine()
        {
            // On créé le pseudo complet
            _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0";
        }

        public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname)
        {
            //this.MainForm = formMain; ?
            this.MainForm = (frmMain)MainForm2;

            // On créé le pseudo complet
            _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0";
        }

The error is the same :
public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname)
---> Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)'

I don't udnerstand? How to put my frmMain class (real class frmMain.cs) a public class? i put public before all partial class?

thanks for help!
 
The way I've done this in the passed is to change

[CS]
public Form mainForm = new frmMain();
[/CS]

to

[CS]
public frmMain mainForm;
[/CS]

Then you should only need to do

[CS]
this.MainForm = MainForm2;
[/CS]
 
I have always the same error AND a new error.
(my new code):
Code:
namespace SKYClient
{
    public class IRCEngine
    {
        public frmMain mainForm;

        public IRCEngine()
        {
            // On créé le pseudo complet
            _pseudoCpl = Pseudo + "|" + Age + Departement + Sexe + "0";
        }

        public IRCEngine(frmMain MainForm2, string svr, string port, string pseudo, string dpt, string sexe, string age, string utilisateur, string hote, string realname)
        {
            // On instancie la frmMain
            //this.MainForm = formMain;
            this.mainForm = MainForm2;
Errors :
- Inconsistent accessibility: field type 'SKYClient.frmMain' is less accessible than field 'SKYClient.IRCEngine.mainForm
----> here : public frmMain mainForm;

- Inconsistent accessibility: parameter type 'SKYClient.frmMain' is less accessible than method 'SKYClient.IRCEngine.IRCEngine(SKYClient.frmMain, string, string, string, string, string, string, string, string, string)'
----> here : public IRCEngine(frmMain MainForm2,...)
 
Back
Top