Ajouter items à combobox en c#

Bonjour,
j'ai un combobox dans un form1(avocat),quand je veux ajouter de nouveaux items je fais appel à un autre form2,voici le code que j'ai associé au bouton d'insertion des items:
string dipfr = textBox2.Text;
avocat av = new avocat();
av.comboBox1.Items.Add(textBox2.Text);
av.comboBox1.Refresh();
this.TopMost = false;
av.TopMost = true;
this.Close();
mais j'aurais rien dans combobox,quel est le probleme.
Configuration: Windows XP / Internet Explorer 6.0

6 réponses

  • Votes
  • Date
  • Date décroissante
  1. Salut,

    Holow1 je comprend pas ta reponse

    Voila une methode qui ne respecte pas trop les standars mes bon a mes yeux se sera la plus simple pour toi

    dans ta fenetre avec la combobox tu modifie la propriete Modifiers de ta combobox en Internal

    ensuite dans le clic de ton bouton pour afficher la deuxieme fenetre tu fais

    Form2 frm = new Form2();
    frm.show();
    this.Hide();

    dans la deuxieme fenetre du mets ton bouton pour revir a la premiere fenetre et du code cela

    Form1 frm = new Form1()
    frm.combobox1.items.add(Textbox1.text);
    frm.refresh();
    frm.show();
    this.close();

    et voila tu as bien un nouvel items d'ajouter.

    Il te reste plus qu'a reprendre mon code et l'adapter a tes noms
    1. bonjour Nico# et merci pour ta réponse,il marche bien avec moi,mais il reste un ptit pbm,quand je termine l'execution les nouvels items que j'ai ajouté disparaissent,je les trouves pas dans la proprieté Items de mon combobox,je veux qu'ils restent permanents.comment le faire?
  2. Bonjour,

    merci pour ma réponse ^_^

    holow@hotmail.fr
    1. merci holow1,pour toutes tes reponses,apparemment t'es fort en c#,est ce que tu peux m'aider dans la realisation des etats de sortie,je t'ai envoyé un email où je t'ai expliquer mon probleme,si tu l'as pas reçu et que tu veux bien m'aider informe moi.merci
  3. Bonjour,

    C'est normal qu'ils disparaissent après la fermeture de la form .

    donc je propose de solution sois les stocker une fichier(text,XML,..ect) ou les stocker dans une base de donne a vous de choisir

    ^_^
    1. bonjour,

      voici le code qu'il va vous permet de conserver les valeur déjà jouter dans ton combobox
      j'ai utilisé un fichier texte pour les stocker ^_^

      le code qui concerne la form1

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.IO;
      
      namespace combo
      {
          public partial class Form1 : Form
          {
      
      
              public void ouvrirFrm2()
              {
                  Form2 frm2 = default(Form2);
                  frm2 = new Form2();
                  frm2.ShowDialog();
              }
      
      
              public void RemplirCombobox()
              {
                  if (File.Exists(Application.StartupPath + "\\fichier.txt"))
                  {
                      StreamReader sr = new StreamReader(Application.StartupPath + "\\fichier.txt");
                      while (!(sr.Peek() == -1))
                      {
                          combobox1.Items.Add(sr.ReadLine());
                      }
                      sr.Dispose();
                  }
                  else
                  {
                      StreamWriter sw = new StreamWriter(Application.StartupPath + "\\fichier.txt");
                      sw.Dispose();
      
                  }
              }
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  RemplirCombobox();
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  ouvrirFrm2();
              }
          }
      }


      le code qui concerne le form2

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.IO;
      namespace combo
      {
      
       
      
          public partial class Form2 : Form
          {
              public void AjouterFichier()
              {
                  StreamWriter sw = new StreamWriter(Application.StartupPath + "\\fichier.txt", true);
                  sw.WriteLine(TextBox1.Text);
                  sw.Close();
              }
              
              public Form2()
              {
                  InitializeComponent();
              }
      
              private void Button1_Click(object sender, EventArgs e)
              {
                  {
                      if (TextBox1.Text != string.Empty)
                      {
      
                          Form1 frm = new Form1();
                      
                          frm.combobox1.Items.Add(TextBox1.Text);
                          frm.combobox1.Refresh();
                          AjouterFichier();
                          this.Close();
                      }
                  }
              }
      
            
          }


      et pour imprimer le contenu du combobox il faut juste imprimer le fichier texte et pour ça voire ici ^_^

      https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.printdocument.printpage?redirectedfrom=MSDN&view=netframework-4.8

      Bon Chance a+
      1. merci beaucoup beaucoup beaucoup beaucoup beaucoup beaucoup beaucoup beaucoup beaucoup,holow1