Inhalt einer listBox in einer *.txt Datei speichern



  • Moin

    Ich habe mir eine Programm geschrieben was für mich HTML tabellen erstellt. Jetzt kann man Inhalte aus einer Listbox nicht einfach per drag&drop irgendwo anders einfügen. Jetzt wollte ich dann den Inhalt in einer *.txt Datei speichern. Wie bekomme ich das hin?

    using System;
    using System.IO;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace TabHTMLv1
    {
    	/// <summary>
    	/// Description of MainForm.
    	/// </summary>
    	public class MainForm : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.TextBox textBox1;
    		private System.Windows.Forms.Panel panel1;
    		private System.Windows.Forms.Button button2;
    		private System.Windows.Forms.ListBox listBox1;
    		public MainForm()
    		{
    			//
    			// The InitializeComponent() call is required for Windows Forms designer support.
    			//
    			InitializeComponent();
    
    			//
    			// TODO: Add constructor code after the InitializeComponent() call.
    			//
    		}
    
    		[STAThread]
    		public static void Main(string[] args)
    		{
    			Application.Run(new MainForm());
    		}
    
    		#region Windows Forms Designer generated code
    		/// <summary>
    		/// This method is required for Windows Forms designer support.
    		/// Do not change the method contents inside the source code editor. The Forms designer might
    		/// not be able to load this method if it was changed manually.
    		/// </summary>
    		private void InitializeComponent() {
    			this.listBox1 = new System.Windows.Forms.ListBox();
    			this.button2 = new System.Windows.Forms.Button();
    			this.panel1 = new System.Windows.Forms.Panel();
    			this.textBox1 = new System.Windows.Forms.TextBox();
    			this.button1 = new System.Windows.Forms.Button();
    			this.label1 = new System.Windows.Forms.Label();
    			this.SuspendLayout();
    			// 
    			// listBox1
    			// 
    			this.listBox1.Location = new System.Drawing.Point(8, 64);
    			this.listBox1.Name = "listBox1";
    			this.listBox1.Size = new System.Drawing.Size(120, 69);
    			this.listBox1.TabIndex = 4;
    			this.listBox1.SelectedIndexChanged += new System.EventHandler(this.ListBox1SelectedIndexChanged);
    			// 
    			// button2
    			// 
    			this.button2.Location = new System.Drawing.Point(488, 24);
    			this.button2.Name = "button2";
    			this.button2.Size = new System.Drawing.Size(72, 24);
    			this.button2.TabIndex = 2;
    			this.button2.Text = "Schließen";
    			this.button2.Click += new System.EventHandler(this.Button2Click);
    			// 
    			// panel1
    			// 
    			this.panel1.Location = new System.Drawing.Point(184, 80);
    			this.panel1.Name = "panel1";
    			this.panel1.Size = new System.Drawing.Size(440, 368);
    			this.panel1.TabIndex = 5;
    			this.panel1.Paint += new System.Windows.Forms.PaintEventHandler(this.Panel1Paint);
    			// 
    			// textBox1
    			// 
    			this.textBox1.Location = new System.Drawing.Point(8, 24);
    			this.textBox1.Name = "textBox1";
    			this.textBox1.Size = new System.Drawing.Size(352, 20);
    			this.textBox1.TabIndex = 0;
    			this.textBox1.Text = "";
    			this.textBox1.TextChanged += new System.EventHandler(this.TextBox1TextChanged);
    			// 
    			// button1
    			// 
    			this.button1.Location = new System.Drawing.Point(384, 24);
    			this.button1.Name = "button1";
    			this.button1.Size = new System.Drawing.Size(80, 24);
    			this.button1.TabIndex = 1;
    			this.button1.Text = "Berechnen";
    			this.button1.Click += new System.EventHandler(this.Button1Click);
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(616, 24);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(104, 24);
    			this.label1.TabIndex = 3;
    			this.label1.Text = "";
    			this.label1.Click += new System.EventHandler(this.Label1Click);
    			// 
    			// MainForm
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(1016, 741);
    			this.Controls.Add(this.panel1);
    			this.Controls.Add(this.listBox1);
    			this.Controls.Add(this.label1);
    			this.Controls.Add(this.button2);
    			this.Controls.Add(this.button1);
    			this.Controls.Add(this.textBox1);
    			this.Name = "MainForm";
    			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    			this.Text = "<TabHTML>©";
    			this.ResumeLayout(false);
    		}
    		#endregion
    		void TextBox1TextChanged(object sender, System.EventArgs e)
    		{
    
    		}
    
    		void Button1Click(object sender, System.EventArgs e)
    		{
    			// Initialation von den Variabeln.
        		string path = " ";
    	    	string myFilename = " ";
    	    	int i = 0;
    	    	int n = 0;
    	    	//path wird ueberprueft.
    	    	path = textBox1.Text;
    	    	// Abfrage in welchem Pfad gesucht werden soll.
    	        DirectoryInfo di = new DirectoryInfo(path);
        	    // Erstellt ein Array mit den information des Ordners.
            	FileInfo[] fi = di.GetFiles();
    	        //Leeren der listBox1.
            	listBox1.Items.Clear();
            	//Die ausgabe der Tabelle.
            	if(textBox1.Text != null)
            	{
            		listBox1.Items.Add("<table border=\"0\" cellspacing=\"5\" cellpadding=\"0\">");
        	    	listBox1.Items.Add(" <tr>");
            		listBox1.Items.Add("  <td><a href=\"java\1:history.back();\">Zur&uuml;ck</a></td>");
    
            		listBox1.Items.Add(" </tr>");
        	    	listBox1.Items.Add(" <tr>");
            		foreach (FileInfo fiTemp in fi)
        	    	{
    	        		if(i == 5)
    		       		{
    		       			listBox1.Items.Add(" </tr>");
    	    	   			listBox1.Items.Add(" <tr>");
    	        			i = 0;
    	        		}
    	    	   	    myFilename = fiTemp.Name;
        		   	    listBox1.Items.Add("  <td><a href=\"java\1:GrafikAnzeigen('"+myFilename+"', '1024', '768');\"><img src=\""+myFilename+"\" height=\"96\" width=\"128\" border=\"0\"></a></td>");
    	    	        i++;
        	    	}
    		       	while(i != 5)
    		       	{
    	    	   		listBox1.Items.Add("  <td style=\"width:128; height:96;\">&nbsp;</td>");
    	        		i++;
    		       	}
    		       	listBox1.Items.Add(" </tr>");
    	    	   	listBox1.Items.Add(" <tr>");
    	        	listBox1.Items.Add("  <td><a href=\"java\1:history.back();\">Zur&uuml;ck</a></td>");
    	        	listBox1.Items.Add(" </tr>");
    	        	listBox1.Items.Add("</tabel>");
            	}
    			else
    			{
    				if(n <= 3)
    				{
    					listBox1.Text = "Ein gueltiger Pfad is z.B. \"F:\\HTML\\....";
    					n++;
    				}
    				else if(n <= 5)
    				{
    					listBox1.Text = "So langsam sollte man eine gueltigen Pfad gefunden haben";
    					n++;
    				}
    				else
    				{
    					label1.Text = "Bitte eine Gueltige Pfad angeben.";
    					n++;
    				}
    			}
    		}
    
    		void Button2Click(object sender, System.EventArgs e)
    		{
    			this.Close();
    		}
    
    		void Label1Click(object sender, System.EventArgs e)
    		{
    
    		}
    
    		void ListBox1SelectedIndexChanged(object sender, System.EventArgs e)
    		{
    
    		}
            }
    }
    

    MFG

    -=TeWeS=-



  • Da ich absolut keine Lust hab dir hier alles vorzubeten ein kleiner Denkanstoß: Schau in den Namespace System.IO - dort gibts ne Klasse StreamWriter - damit kannst du Dateien ziemlich praktisch schreiben. Jetzt gehst du einfach listBox.Items durch (for/foreach) und schreibst die Einträge so in deine .txt, wie du s benötigst ....

    Greetz



  • Und noch was ... google bzw. die Suche hier im Forum hilft manchmal Wunder 😉

    Greetz



  • thx, habs jetzt schon anders hinbekommen


Anmelden zum Antworten