[C#] Probleme entre relation listbox et textbox

tsubasa933 Messages postés 2 Date d'inscription mercredi 26 novembre 2008 Statut Membre Dernière intervention 14 avril 2009 - 8 avril 2009 à 11:45
tsubasa933 Messages postés 2 Date d'inscription mercredi 26 novembre 2008 Statut Membre Dernière intervention 14 avril 2009 - 14 avril 2009 à 14:38
Alors voila je vous explique mon probleme.

Dans ma listbox j'ai plusieurs choix qui quand j'appuye sur modifier m'ouvre notepad.
En appuyant sur Apercu j'aimerai que se que je peux modifier dans notepad s'affiche dans ma textbox.
Or je n'ai que le numero de case de ma listbox.
 
Voici mon code:

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 interface_de_gestion
{
   
    public partial class Form1 : Form
    {
        public string NomScript;
        public Form1()
        {
            InitializeComponent();
            listBox1.Items.Add("Script Drivers");
            listBox1.Items.Add("Script Imprimantes");
            listBox1.Items.Add("Script Configuration Automatique Thunderbird");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e) // Ma listbox
        {
            int Idx = listBox1.SelectedIndex;
            string Str = listBox1.SelectedItem.ToString();
           
         
            switch (Idx)
            {
                case 0:
                    NomScript = "c:\\new1.txt";
                    break;
                case 1:
                    NomScript = "c:\\final1.spt";
                    break;
                case 2:
                    NomScript = "c:\\final.spt";
                    break;
            }

        }

        private void button1_Click(object sender, EventArgs e) // Mon bouton "Apercu"
        {
            textBox1.Text = listBox1.SelectedIndex.ToString();  
        }

        private void button3_Click(object sender, EventArgs e) // Mon bouton "modifier"
        {
            StartProcess(NomScript);
        }

        void StartProcess(String Name)
        {
            // Instance de la classe Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            // Nom du fichier dont l'extension est connue du shell à ouvrir
            proc.StartInfo.FileName = Name;
            // Démarrage du processus.
            // Notepad, si il est associé aux fichiers .txt,
            // sera lancé et ouvrira le fichier monfichier.txt
            proc.Start();
            // On libère les ressources dont on a plus besoin.
            proc.Close(); // Attention Close ne met pas fin au processus.
        }

        void StartProcess(String Name, String Arg)
        {
            // Instance de la classe Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            // Nom du fichier dont l'extension est connue du shell à ouvrir
            proc.StartInfo.FileName = Name;
            proc.StartInfo.Arguments = Arg;
            // Démarrage du processus.
            // Notepad, si il est associé aux fichiers .txt,
            // sera lancé et ouvrira le fichier monfichier.txt
            proc.Start();
            // On libère les ressources dont on a plus besoin.
            proc.Close(); // Attention Close ne met pas fin au processus.
        }

        private void OK_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
          
        }
    }
}

Le resultat de se code est parfait pour la modification.
Mais pour l'apercu, je recois juste le numero de case de mon script.

Comment faire pour me renvoyer se qu'il y a DANS se script.

Merci d'avance.

2 réponses

cs_Robert33 Messages postés 834 Date d'inscription samedi 15 novembre 2008 Statut Membre Dernière intervention 14 janvier 2017 33
9 avril 2009 à 17:00
Bien le bonjour à toi aussi!

normal, listbox1.SelectedIndex est le numero d'ordre de l'élement selectionné.

Essaye
listBox1.items[listbox1.SelectedIndex].ToString();
ou simplement
listBox1.SelectedItem.ToString();
0
tsubasa933 Messages postés 2 Date d'inscription mercredi 26 novembre 2008 Statut Membre Dernière intervention 14 avril 2009
14 avril 2009 à 14:38
Bonjour, voici mon code apres ton aide.

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;
using System.Diagnostics;

namespace interface_de_gestion
{
    
    public partial class Form1 : Form
    {
        public string NomScript;
        public Form1()
        {
            InitializeComponent();
            listBox1.Items.Add("Script Drivers");
            listBox1.Items.Add("Script Imprimantes");
            listBox1.Items.Add("Script Configuration Automatique Thunderbird");
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Idx = listBox1.SelectedIndex;
            //string Str = listBox1.SelectedItem.ToString();
            
          
            switch (Idx)
            {
                case 0:
                    NomScript = "d:\\Drivers.spt";
                    break;
                case 1:
                    NomScript = "d:\\Deploiement_Imprimante.spt";
                    break;
                case 2:
                    NomScript = "d:\\Messagerie_2009.spt";
                    break;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //StartProcess("notepad.exe", "c:\\final1.spt");
            //StartProcess(NomScript);
            StreamReader sr = new StreamReader(NomScript);
            textBox1.Text = sr.ReadToEnd();
            sr.Close();            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StartProcess(NomScript);
        }

        void StartProcess(String Name)
        {
            // Instance de la classe Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            // Nom du fichier dont l'extension est connue du shell à ouvrir
            proc.StartInfo.FileName = Name;
            // Démarrage du processus.
            // Notepad, si il est associé aux fichiers .txt,
            // sera lancé et ouvrira le fichier monfichier.txt
            proc.Start();
            // On libère les ressources dont on a plus besoin.
            proc.Close(); // Attention Close ne met pas fin au processus.
        }

        void StartProcess(String Name, String Arg)
        {
            // Instance de la classe Process
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            // Nom du fichier dont l'extension est connue du shell à ouvrir
            proc.StartInfo.FileName = Name;
            proc.StartInfo.Arguments = Arg;
            // Démarrage du processus.
            // Notepad, si il est associé aux fichiers .txt,
            // sera lancé et ouvrira le fichier monfichier.txt
            proc.Start();
            // On libère les ressources dont on a plus besoin.
            proc.Close(); // Attention Close ne met pas fin au processus.
        }

        private void Close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            listBox1.Width = Width - 823 + 563;
            button1.Left = Width - 823 + 623;
            button3.Left = Width - 823 + 623;
            Close.Left = Width - 823 + 623;
            Close.Top = Height - 679 + 572;
            textBox1.Width = Width - 823 + 563;
            textBox1.Height = Height - 679 + 359;
 
        }

        private void Executer_Click(object sender, EventArgs e)
        {
            
        }
    }
}

Avec ton aide et celle d'un de mes amis je suis arrivé à sa. Et sa marche très bien.

Maintenant j'aimerai exécuter ces scripts, mais je ne trouve pas le moyen de le faire...
        private void Executer_Click(object sender, EventArgs e)

        {

            

        }
Merci et a tres vite j'espere.
0
Rejoignez-nous