none
Les Class RRS feed

  • Question

  • Bonjour a tous

    Est il possible d'utiliser un Class en C# dans une solution VB

    Autrement dit, j'ai une class qui est fait en C# et je veut savoir

    si je monte un programme en VB Est-ce que je peut utiliser cette class dans ce projet 

    MERCI


    Daniel

    mardi 7 janvier 2014 12:59

Réponses

  • Bonjour,

    Vous ne pouvez définir une classe C# que dans des projets C#. Un projet est défini dans un langage spécifique.

    En revanche dans une solution vous pouvez ajouter tout type de projet, et on peut accéder aux classes d'un projet depuis un autre projet quelque que soit leur langage (il faut juste que se soit des projets .Net).

    Dans votre cas vous avez une solution avec au moins un projet en VB, vous pouvez ajouter un nouveau projet de type "Bibliothèque de classes" en C#. Dans cette bibliothèque vous mettez vos classes C#. Puis dans votre projet VB vous ajoutez une référence vers votre projet C#. A parti de ce moment là votre projet VB pourra voir toutes les classes publiques du projet C#.

    C'est le principe des librairies, les projets .Net étant compilés dans un langage intermédiaire ils peuvent se partager les objets.

    Cordialement et mes meilleurs vœux pour cette nouvelle année,


    Yan Grenier

    • Proposé comme réponse Aurel Bera mercredi 8 janvier 2014 09:06
    • Marqué comme réponse Aurel Bera lundi 13 janvier 2014 07:20
    mardi 7 janvier 2014 13:55
  • Bonjour,

    Le fait que la classe soit statique ne pose pas de problème particulier, en revanche comme elle n'est pas dans un espace de nom elle est accessible à la racine des espaces de noms ce qui n'est pas forcément une bonne pratique de programmation, on privilégie l'organisation du code.

    En revanche je constate que votre classe fait référence à plusieurs librairies qui ne sont pas référencées en standard dans une bibliothèque de classes, il faut donc ajouter les références aux dll suivantes :

    • System.Drawing
    • System.Windows.Forms
    • System.Data.SqlServerCe

    Pour la dernière il s'agit du SQL Server Compact qui n'est pas forcément installé (a vérifier) je crois qu'il est accessible via la Web Platform Installer (WPI).

    Une fois fait depuis votre code VB vous aurez accès à votre classe :

    Public Sub Test()
      Cs.CreateDB()
    End Sub

    Cordialement,


    Yan Grenier

    • Proposé comme réponse Aurel Bera mercredi 8 janvier 2014 13:58
    • Marqué comme réponse Aurel Bera lundi 13 janvier 2014 07:20
    mercredi 8 janvier 2014 07:39

Toutes les réponses

  • Bonjour,

    Vous ne pouvez définir une classe C# que dans des projets C#. Un projet est défini dans un langage spécifique.

    En revanche dans une solution vous pouvez ajouter tout type de projet, et on peut accéder aux classes d'un projet depuis un autre projet quelque que soit leur langage (il faut juste que se soit des projets .Net).

    Dans votre cas vous avez une solution avec au moins un projet en VB, vous pouvez ajouter un nouveau projet de type "Bibliothèque de classes" en C#. Dans cette bibliothèque vous mettez vos classes C#. Puis dans votre projet VB vous ajoutez une référence vers votre projet C#. A parti de ce moment là votre projet VB pourra voir toutes les classes publiques du projet C#.

    C'est le principe des librairies, les projets .Net étant compilés dans un langage intermédiaire ils peuvent se partager les objets.

    Cordialement et mes meilleurs vœux pour cette nouvelle année,


    Yan Grenier

    • Proposé comme réponse Aurel Bera mercredi 8 janvier 2014 09:06
    • Marqué comme réponse Aurel Bera lundi 13 janvier 2014 07:20
    mardi 7 janvier 2014 13:55
  • Merci Mr Grenier pour cette réponse rapide à ma question

    comme ma class est déclare en static public  Est-ce que je peut l'inclure dans un projet

    de classLibrary sans la modifier ou quelles modification dois-je faire pour ce projet de librairie

    Voici la class

    using System;
    using System.Data;
    using System.Data.SqlServerCe;
    using System.Drawing;
    using System.Security.Cryptography;
    using System.Windows.Forms;
    
    static public class Cs
    	{
    		#region " Main Declaration "
    
    		static private string sql = "";
    		static private HashAlgorithm mhash;
    		static private Font NewFont;
    		static public String Dpath = Application.StartupPath + "\\Data\\";
    		static public String DB_NAME = Dpath + Application.ProductName + ".sdf";
    		static public String DB_PWD = "";
    
    		#endregion
    
    		#region " Contacts Declaration "
    
    		static private string _ID;
    		static public string ID
    		{
    			get { return _ID; }
    			set { _ID = value; }
    		}
    		static private string _Firstname;
    		static public string Firstname
    		{
    			get { return _Firstname; }
    			set { _Firstname = value; }
    		}
    		static private string _Lastname;
    		static public string Lastname
    		{
    			get { return _Lastname; }
    			set { _Lastname = value; }
    		}
    		static private string _Adress;
    		static public string Adress
    		{
    			get { return _Adress; }
    			set { _Adress = value; }
    		}
    		static private string _City;
    		static public string City
    		{
    			get { return _City; }
    			set { _City = value; }
    		}
    		static private string _PCode;
    		static public string PCode
    		{
    			get { return _PCode; }
    			set { _PCode = value; }
    		}
    		static private string _State;
    		static public string State
    		{
    			get { return _State; }
    			set { _State = value; }
    		}
    		static private string _Country;
    		static public string Country
    		{
    			get { return _Country; }
    			set { _Country = value; }
    		}
    		static private string _Telephone;
    		static public string Telephone
    		{
    			get { return _Telephone; }
    			set { _Telephone = value; }
    		}
    		static private string _Cell;
    		static public string Cell
    		{
    			get { return _Cell; }
    			set { _Cell = value; }
    		}
    		static private string _EMail;
    		static public string EMail
    		{
    			get { return _EMail; }
    			set { _EMail = value; }
    		}
    
    		#endregion
    
    		#region " Security Declaration "
    
    		static private string _UID;
    		static public string UID
    		{
    			get { return _UID; }
    			set { _UID = value; }
    		}
    		static private string _UName;
    		static public string UName
    		{
    			get { return _UName; }
    			set { _UName = value; }
    		}
    		static private string _UPwd;
    		static public string UPwd
    		{
    			get { return _UPwd; }
    			set { _UPwd = value; }
    		}
    		static private string _UEMail;
    		static public string UEMail
    		{
    			get { return _UEMail; }
    			set { _UEMail = value; }
    		}
    
    		#endregion
    
    		#region " Log Declaration "
    
    		static private string _LID;
    		static public string LID
    		{
    			get { return _LID; }
    			set { _LID = value; }
    		}
    		static private string _LName;
    		static public string LName
    		{
    			get { return _LName; }
    			set { _LName = value; }
    		}
    		static private string _LContent;
    		static public string LContent
    		{
    			get { return _LContent; }
    			set { _LContent = value; }
    		}
    
    		#endregion
    
    		#region " Database Connection "
    
    		static public string ConnectString()
    		{
    			string connectionString = string.Format("DataSource=\"{0}\"; Password=\"{1}\"", DB_NAME, DB_PWD);
    			return connectionString;
    		}
    
    		#endregion
    
    		#region " Create DataBase "
    
    		static public void CreateDB()
    		{
    			SqlCeEngine en = new SqlCeEngine(ConnectString());
    			en.CreateDatabase();
    			en.Dispose();
    			en = null;
    		}
    
    		static public bool TableExists(this SqlCeConnection connection, string tableName)
    		{
    			if (tableName == null) throw new ArgumentNullException("tableName");
    			//if (string.IsNullOrWhiteSpace(tableName)) throw new ArgumentException("Invalid table name");
    			if (connection == null) throw new ArgumentNullException("connection");
    			if (connection.State != ConnectionState.Open)
    			{
    				throw new InvalidOperationException("TableExists requires an open and available Connection. The connection's current state is " + connection.State);
    			}
    
    			using (SqlCeCommand command = connection.CreateCommand())
    			{
    				command.CommandType = CommandType.Text;
    				command.CommandText = "SELECT 1 FROM Information_Schema.Tables WHERE TABLE_NAME = @tableName";
    				command.Parameters.AddWithValue("tableName", tableName);
    				object result = command.ExecuteScalar();
    				return result != null;
    			}
    		}
    
    		#endregion
    
    		#region " Contacts Table "
    
    		static public void FindContacts()
    		{
    			using (SqlCeConnection cn = new SqlCeConnection(ConnectString()))
    			{
    				using (SqlCeCommand cmd = new SqlCeCommand("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @Contacts", cn))
    				{
    					int résultat;
    					cmd.Parameters.AddWithValue("@Contacts", "Contacts");
    					if (cn.State == ConnectionState.Closed)
    					{
    						cn.Open();
    					}
    					résultat = Convert.ToInt32(cmd.ExecuteScalar());
    					if (résultat == 1)
    					{
    						cn.Close();
    						return;
    					}
    					else
    					{
    						CreateContacts();
    					}
    				}
    			}
    		}
    
    		static private void CreateContacts()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			sql = "create table Contacts ("
    				+ "ID INT IDENTITY (1,1) PRIMARY KEY not null, "
    				+ "LastName nvarchar (50), "
    				+ "FirstName nvarchar (50), "
    				+ "Adress nvarchar (100), "
    				+ "City nvarchar (50), "
    				+ "PCode nvarchar(15), "
    				+ "State nvarchar(50), "
    				+ "Country nvarchar(50), "
    				+ "Telephone nvarchar(25), "
    				+ "Cell nvarchar(25), "
    				+ "EMail nvarchar(256))";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void SaveContacts()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			sql = "insert into Contacts "
    				+ "(Lastname, Firstname, Adress, City, PCode, State, Country, Telephone, Cell, EMail) "
    				+ "values (@lastName, @firstname, @adress, @city, @pcode, @state, @country, @telephone, @cell, @email)";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.Parameters.AddWithValue("@lastname", Lastname);
    			cmd.Parameters.AddWithValue("@firstname", Firstname);
    			cmd.Parameters.AddWithValue("@adress", Adress);
    			cmd.Parameters.AddWithValue("@city", City);
    			cmd.Parameters.AddWithValue("@pcode", PCode);
    			cmd.Parameters.AddWithValue("@state", State);
    			cmd.Parameters.AddWithValue("@country", Country);
    			cmd.Parameters.AddWithValue("@telephone", Telephone);
    			cmd.Parameters.AddWithValue("@cell", Cell);
    			cmd.Parameters.AddWithValue("@email", EMail);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    			cn = null;
    		}
    
    		static public void UpdateContacts()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			sql = "UPDATE Contacts SET Adress='" + Adress + "', City='" + City + "', PCode='" + PCode + "', State='" + State + "', Country='" + Country + "', Telephone='" + Telephone + "', Cell='" + Cell + "', EMail='" + EMail + "' WHERE ID= '" + ID + "'";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.Parameters.Add(new SqlCeParameter("@adress", SqlDbType.NVarChar, 100));
    			cmd.Parameters.Add(new SqlCeParameter("@city", SqlDbType.NVarChar, 50));
    			cmd.Parameters.Add(new SqlCeParameter("@pcode", SqlDbType.NVarChar, 15));
    			cmd.Parameters.Add(new SqlCeParameter("@state", SqlDbType.NVarChar, 50));
    			cmd.Parameters.Add(new SqlCeParameter("@pays", SqlDbType.NVarChar, 50));
    			cmd.Parameters.Add(new SqlCeParameter("@telephone", SqlDbType.NVarChar, 25));
    			cmd.Parameters.Add(new SqlCeParameter("@cell", SqlDbType.NVarChar, 25));
    			cmd.Parameters.Add(new SqlCeParameter("@email", SqlDbType.NVarChar, 256));
    			cmd.Parameters["@adress"].Value = Adress;
    			cmd.Parameters["@ville"].Value = City;
    			cmd.Parameters["@pcode"].Value = PCode;
    			cmd.Parameters["@province"].Value = State;
    			cmd.Parameters["@pays"].Value = Country;
    			cmd.Parameters["@telephone"].Value = Telephone;
    			cmd.Parameters["@portable"].Value = Cell;
    			cmd.Parameters["@eMail"].Value = EMail;
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void DeleteContacts(string strid)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			sql = "DELETE FROM Contacts WHERE ID='" + strid + "'";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    			return;
    		}
    
    		static public void LoadData(this DataGridView D)
    		{
    			SqlCeDataAdapter dta;
    			DataSet dts = new DataSet();
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			dts = new DataSet();
    			String sql = "Select * From Contacts";
    			dta = new SqlCeDataAdapter(sql, cn);
    			dta.Fill(dts, "Contacts");
    			D.DataSource = dts.Tables["Contacts"];
    			D.Columns[0].Visible = false;
    			D.Columns[1].Visible = true;
    			D.Columns[2].Visible = true;
    			D.Columns[3].Visible = false;
    			D.Columns[4].Visible = false;
    			D.Columns[5].Visible = false;
    			D.Columns[6].Visible = false;
    			D.Columns[7].Visible = false;
    			D.Columns[8].Visible = true;
    			D.Columns[9].Visible = true;
    			D.Columns[10].Visible = true;
    			cn.Close();
    		}
    
    		#endregion
    
    		#region " Data Encryption "
    
    		static private string EncryptData(string Data)
    		{
    			byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(Data);
    			string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    			return returnValue;
    		}
    
    		static private string DecryptData(string Data)
    		{
    			byte[] encodedDataAsBytes = System.Convert.FromBase64String(Data);
    			string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
    			return returnValue;
    		}
    
    		#endregion
    
    		#region " Log Table "
    
    		static public void FindLog()
    		{
    			using (SqlCeConnection cn = new SqlCeConnection(ConnectString()))
    			{
    				using (SqlCeCommand cmd = new SqlCeCommand("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @Journal", cn))
    				{
    					int résultat;
    					cmd.Parameters.AddWithValue("@Journal", "Journal");
    					if (cn.State == ConnectionState.Closed)
    					{
    						cn.Open();
    					}
    					résultat = Convert.ToInt32(cmd.ExecuteScalar());
    					if (résultat == 1)
    					{
    						cn.Close();
    						return;
    					}
    					else
    					{
    						CreateLog();
    					}
    				}
    			}
    		}
    
    		static public void CreateLog()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			sql = "create table Log ("
    				+ "LID INT IDENTITY (1,1) PRIMARY KEY not null, "
    				+ "LName NVarChar(50), "
    				+ "LContent NVarChar(MAX))";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    
    			sql = "insert into Log "
    			   + "(LName) "
    			   + "values ('Mon Journal')";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void LoadLog()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd = cn.CreateCommand();
    			cmd.CommandText = "SELECT * FROM Log";
    			SqlCeDataReader sdr = cmd.ExecuteReader();
    			while (sdr.Read())
    			{
    				LID = sdr["LID"].ToString();
    				LName = sdr["LName"].ToString();
    				string EncData = sdr["LContent"].ToString();
    				LContent = DecryptData(EncData);
    			}
    			sdr.Close();
    			cn.Close();
    		}
    
    		static public void SaveLog(string Data)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			string EncData = EncryptData(Data);
    			sql = "UPDATE Log SET LContent='" + EncData + "' WHERE LName='Mon Journal'";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.Parameters.Add(new SqlCeParameter("@lcontent", SqlDbType.NVarChar));
    			cmd.Parameters["@lcontent"].Value = EncData;
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void DeleteLogRow(string strID)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			sql = "DELETE FROM Log WHERE ID='" + strID + "'";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    
    			sql = "insert into Log "
    			   + "(LName) "
    			   + "values ('Mon Journal')";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    			return;
    		}
    
    		static public void DropIT()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			sql = "Drop Table Log";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    		}
    
    		#endregion
    
    		#region " Hashing "
    
    		static private HashAlgorithm SetHash()
    		{
    			return new SHA1CryptoServiceProvider();
    		}
    
    		static public string HashString(string Value)
    		{
    			mhash = SetHash();
    			// Convertit la chaîne originale en un tableau de Bytes
    			byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(string.Concat(Value, "123456"));
    			// Procède au hashage et retourne un tableau de Bytes
    			byte[] bytHash = mhash.ComputeHash(bytValue);
    			mhash.Clear();
    			// Retourne une chaîne de caractères en base 64 de la valeur hashée
    			return Convert.ToBase64String(bytHash);
    		}
    
    		#endregion
    
    		#region " Security Table "
    
    		static public void FindLogon()
    		{
    			using (SqlCeConnection cn = new SqlCeConnection(ConnectString()))
    			{
    				using (SqlCeCommand cmd = new SqlCeCommand("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @Logon", cn))
    				{
    					int résultat;
    					cmd.Parameters.AddWithValue("@Logon", "Logon");
    					if (cn.State == ConnectionState.Closed)
    					{
    						cn.Open();
    					}
    					résultat = Convert.ToInt32(cmd.ExecuteScalar());
    					if (résultat == 1)
    					{
    						cn.Close();
    						return;
    					}
    					else
    					{
    						CreateLogon();
    					}
    				}
    			}
    		}
    
    		static public void CreateLogon()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			sql = "create table Security ("
    				+ "SID INT IDENTITY (1,1) PRIMARY KEY not null, "
    				+ "SUName nvarchar (50), "
    				+ "SPWord nvarchar (256), "
    				+ "SEMail nvarchar(256))";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void ReadAll(this DataGridView DV)
    		{
    			SqlCeDataAdapter dta;
    			DataSet dts = new DataSet();
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			dts = new DataSet();
    			String sql = "Select * From Security";
    			dta = new SqlCeDataAdapter(sql, cn);
    			dta.Fill(dts, "Security");
    			DV.DataSource = dts.Tables["Security"];
    			cn.Close();
    		}
    
    		static public void ReadUPwd(string strEM)
    		{
    			//string str
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd = cn.CreateCommand();
    			cmd.CommandText = "select SPWord from Security where SEMail = '" + strEM + "'";
    			SqlCeDataReader sdr = cmd.ExecuteReader();
    			while (sdr.Read())
    			{
    				UPwd = sdr["SPWord"].ToString();
    			}
    		}
    
    		//checking whether username already exist
    		static public void UNameCheck(string str)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd = new SqlCeCommand("Select * from Security where SUName= @SUName", cn);
    			cmd.Parameters.AddWithValue("@SUName", str);
    			SqlCeDataReader dr = cmd.ExecuteReader();
    			while (dr.Read())
    			{
    				MessageBox.Show("SUName " + dr[1].ToString() + " Already exist");
    				break;
    			}
    			cn.Close();
    		}
    
    		static public void datastore()
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			string sql = "insert into Security "
    				+ "(SUName, SPWord, SEMail) "
    				+ "values (@suname, @spword, @semail)";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.Parameters.AddWithValue("@suname", UName);
    			cmd.Parameters["@suname"].Direction = ParameterDirection.Input;
    			cmd.Parameters.AddWithValue("@spword", HashString(UPwd));
    			cmd.Parameters["@spword"].Direction = ParameterDirection.Input;
    			cmd.Parameters.AddWithValue("@semail", UEMail);
    			cmd.Parameters["@semail"].Direction = ParameterDirection.Input;
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void DataUpdate(string str)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd;
    			sql = "UPDATE Security SET SPWord='" + UPwd + "' WHERE SEMail = '" + str + "'";
    			cmd = new SqlCeCommand(sql, cn);
    			cmd.Parameters.Add(new SqlCeParameter("@spword", SqlDbType.NVarChar, 256));
    			cmd.Parameters["@spword"].Value = UPwd;
    			cmd.ExecuteNonQuery();
    			cn.Close();
    		}
    
    		static public void DeleteData(String str)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			sql = "DELETE FROM Security where ID='" + str + "'";
    			SqlCeCommand cmd = new SqlCeCommand(sql, cn);
    			cmd.ExecuteNonQuery();
    			cn.Close();
    			return;
    		}
    
    		static public void Verify(string str)
    		{
    			SqlCeConnection cn = new SqlCeConnection(ConnectString());
    			if (cn.State == ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    			SqlCeCommand cmd = new SqlCeCommand("Select * from Security where SEMail= @SEMail", cn);
    			cmd.Parameters.AddWithValue("@SEMail", str);
    			SqlCeDataReader dr = cmd.ExecuteReader();
    			while (dr.Read())
    			{
    				UEMail = dr[3].ToString();
    				break;
    			}
    		}
    
    		#endregion
    
    		# region " RTBExtention "
    
    		static public void AddRemBullets(this RichTextBox RTC)
    		{
    			if (RTC.SelectionBullet == false)
    			{
    				RTC.BulletIndent = 20;
    				RTC.SelectionBullet = true;
    			}
    			else
    			{
    				RTC.BulletIndent = 0;
    				RTC.SelectionBullet = false;
    			}
    		}
    
    		static public object CurrentColumn(this RichTextBox RTC)
    		{
    			return RTC.SelectionStart - RTC.GetFirstCharIndexOfCurrentLine() + 1;
    		}
    
    		static public object CurrentLine(this RichTextBox RTC)
    		{
    			return RTC.GetLineFromCharIndex(RTC.SelectionStart) + 1;
    		}
    
    		static public void MakeBold(this RichTextBox RTC)
    		{
    			NewFont = new Font(RTC.SelectionFont, (RTC.SelectionFont.Bold ? RTC.SelectionFont.Style & ~FontStyle.Bold : RTC.SelectionFont.Style | FontStyle.Bold));
    			RTC.SelectionFont = NewFont;
    		}
    
    		static public void MakeItalic(this RichTextBox RTC)
    		{
    			NewFont = new Font(RTC.SelectionFont, (RTC.SelectionFont.Italic ? RTC.SelectionFont.Style & ~FontStyle.Italic : RTC.SelectionFont.Style | FontStyle.Italic));
    			RTC.SelectionFont = NewFont;
    		}
    
    		static public void MakeUnderline(this RichTextBox RTC)
    		{
    			NewFont = new Font(RTC.SelectionFont, (RTC.SelectionFont.Underline ? RTC.SelectionFont.Style & ~FontStyle.Underline : RTC.SelectionFont.Style | FontStyle.Underline));
    			RTC.SelectionFont = NewFont;
    		}
    
    		static public int FindMyText(this RichTextBox RTC, string text, int start)
    		{
    			// Initialize the return value to false by default.
    			int returnValue = -1;
    
    			// Ensure that a search string has been specified and a valid start point.
    			if (text.Length > 0 && start >= 0)
    			{
    				// Obtain the location of the search string in richTextBox1.
    				int indexToText = RTC.Find(text, start, RichTextBoxFinds.MatchCase);
    				// Determine whether the text was found in richTextBox1.
    				if (indexToText >= 0)
    				{
    					returnValue = indexToText;
    				}
    				else
    				{
    					MessageBox.Show(null, "Je n'ai pas trouvé [" + text + "]", "Rechercher", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    				}
    			}
    			return returnValue;
    		}
    
    		static public int FindNext(this RichTextBox RTC, string text, int start)
    		{
    			int StartPosition = RTC.SelectionStart + 2;
    
    			StartPosition = RTC.Find(text, StartPosition, RichTextBoxFinds.MatchCase);
    			if (StartPosition == 0)
    			{
    				return 0;
    			}
    			else if (StartPosition != -1)
    			{
    				RTC.Select(StartPosition, text.Length);
    				RTC.ScrollToCaret();
    				RTC.Focus();
    			}
    			else { MessageBox.Show("je trouve plus rien dans ce texte.", Application.ProductName + "Rechercher suivante", MessageBoxButtons.OK, MessageBoxIcon.Information); }
    			return StartPosition;
    		}
    
    		static public int AddDate(this RichTextBox RTC, string text, int start)
    		{
    			// Initialize the return value to false by default.
    			int returnValue = -1;
    
    			// Ensure that a search string has been specified and a valid start point.
    			if (text.Length > 0 && start >= 0)
    			{
    				// Obtain the location of the search string in richTextBox1.
    				int indexToText = RTC.Find(text, start, RichTextBoxFinds.MatchCase);
    				// Determine whether the text was found in richTextBox1.
    				if (indexToText >= 0)
    				{
    					returnValue = indexToText;
    					RTC.SelectionStart = RTC.TextLength;
    					RTC.SelectedText = "\r\n";
    					RTC.Select();
    				}
    				else
    				{
    					RTC.SelectionStart = RTC.TextLength;
    					RTC.SelectedText = "\r\n";
    					RTC.SelectedText = "\r\n";
    					RTC.SelectionFont = new Font("Microsoft Sans serif", 14, FontStyle.Bold);
    					RTC.SelectedText = DateTime.Now.ToString("dddd, dd MMM yyyy");
    					RTC.SelectedText = "\r\n";
    					RTC.SelectionFont = new Font("Microsoft Sans serif", RTC.SelectionFont.Size, FontStyle.Regular);
    					RTC.SelectedText = "\r\n";
    					RTC.SelectionStart = RTC.TextLength;
    					RTC.Select();
    					RTC.Focus();
    				}
    			}
    			return returnValue;
    		}
    
    		#endregion
    
    		#region " DialogBox "
    
    		public static DialogResult Query(string title, string promptText, ref string value)
    		{
    			Form form = new Form();
    			Label label = new Label();
    			TextBox textBox = new TextBox();
    			Button buttonOk = new Button();
    			Button buttonCancel = new Button();
    			//______________________________________________________________________________________//
    			form.Text = title;
    			label.Text = promptText;
    			textBox.Text = value;
    			//______________________________________________________________________________________//
    			buttonOk.Text = "OK";
    			buttonCancel.Text = "Annuler";
    			buttonOk.DialogResult = DialogResult.OK;
    			buttonCancel.DialogResult = DialogResult.Cancel;
    			//______________________________________________________________________________________//
    			label.SetBounds(9, 20, 372, 13);
    			textBox.SetBounds(12, 42, 372, 20);
    			buttonOk.SetBounds(228, 72, 75, 25);
    			buttonCancel.SetBounds(309, 72, 75, 25);
    			//______________________________________________________________________________________//
    			label.AutoSize = true;
    			textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
    			buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    			buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    			//______________________________________________________________________________________//
    			form.ClientSize = new Size(396, 107);
    			form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
    			form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
    			form.FormBorderStyle = FormBorderStyle.FixedDialog;
    			form.StartPosition = FormStartPosition.CenterScreen;
    			form.MinimizeBox = false;
    			form.MaximizeBox = false;
    			form.AcceptButton = buttonOk;
    			form.CancelButton = buttonCancel;
    			//______________________________________________________________________________________//
    			DialogResult dialogResult = form.ShowDialog();
    			value = textBox.Text;
    			return dialogResult;
    		}
    
    		public static DialogResult ShowDate(string title, string promptText)
    		{
    			Form form = new Form();
    			Label label = new Label();
    			Button buttonOk = new Button();
    			//______________________________________________________________________________________//
    			form.Text = title;
    			label.Text = promptText;
    			label.Font = new Font(label.Font.Name, 12, FontStyle.Bold);
    			//______________________________________________________________________________________//
    			buttonOk.Text = "OK";
    			buttonOk.DialogResult = DialogResult.OK;
    			//______________________________________________________________________________________//
    			label.SetBounds(9, 20, 372, 13);
    			buttonOk.SetBounds(130, 80, 75, 25);
    			//______________________________________________________________________________________//
    			label.AutoSize = true;
    			buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
    			//______________________________________________________________________________________//
    			form.ClientSize = new Size(335, 114);
    			form.Controls.AddRange(new Control[] { label, buttonOk });
    			form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
    			form.FormBorderStyle = FormBorderStyle.FixedDialog;
    			form.StartPosition = FormStartPosition.CenterScreen;
    			form.MinimizeBox = false;
    			form.MaximizeBox = false;
    			form.AcceptButton = buttonOk;
    			//______________________________________________________________________________________//
    			DialogResult dialogResult = form.ShowDialog();
    			return dialogResult;
    		}
    
    		#endregion
    
    	}
    Merci


    Daniel

    mardi 7 janvier 2014 17:47
  • Bonjour,

    Le fait que la classe soit statique ne pose pas de problème particulier, en revanche comme elle n'est pas dans un espace de nom elle est accessible à la racine des espaces de noms ce qui n'est pas forcément une bonne pratique de programmation, on privilégie l'organisation du code.

    En revanche je constate que votre classe fait référence à plusieurs librairies qui ne sont pas référencées en standard dans une bibliothèque de classes, il faut donc ajouter les références aux dll suivantes :

    • System.Drawing
    • System.Windows.Forms
    • System.Data.SqlServerCe

    Pour la dernière il s'agit du SQL Server Compact qui n'est pas forcément installé (a vérifier) je crois qu'il est accessible via la Web Platform Installer (WPI).

    Une fois fait depuis votre code VB vous aurez accès à votre classe :

    Public Sub Test()
      Cs.CreateDB()
    End Sub

    Cordialement,


    Yan Grenier

    • Proposé comme réponse Aurel Bera mercredi 8 janvier 2014 13:58
    • Marqué comme réponse Aurel Bera lundi 13 janvier 2014 07:20
    mercredi 8 janvier 2014 07:39
  • Merci Beaucoup MR Grenier

    Je regarde ca de près.

    Merci

    Daniel Morais


    Daniel

    mercredi 8 janvier 2014 20:17