0
Answer

reading a RAM file....to list box

bn9

bn9

20y
1.7k
1
need a hand please... i have am designing a phone directory program that reads a .txt file with a "lastname, firstname, phone#, comment." as each entry. i am trying to access the file with a main menu 'open' button to retrieve the file. i have to be able to click on the text file, and have each entry in the list box on the GUI. can anyone help!??!? i have pasted the form and what i have so far. thanks! using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization; namespace phoneDIR { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.VScrollBar vScrollBar1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.ListBox lstbox; private System.Windows.Forms.TextBox txtBoxSearch; private System.Windows.Forms.TextBox txtBoxAdd; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuItem2; private System.Windows.Forms.MenuItem menuItem3; private System.Windows.Forms.Button SearchButton; private System.Windows.Forms.Button AddButton; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.lstbox = new System.Windows.Forms.ListBox(); this.vScrollBar1 = new System.Windows.Forms.VScrollBar(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtBoxSearch = new System.Windows.Forms.TextBox(); this.txtBoxAdd = new System.Windows.Forms.TextBox(); this.SearchButton = new System.Windows.Forms.Button(); this.AddButton = new System.Windows.Forms.Button(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuItem2 = new System.Windows.Forms.MenuItem(); this.menuItem3 = new System.Windows.Forms.MenuItem(); this.SuspendLayout(); // // lstbox // this.lstbox.Location = new System.Drawing.Point(24, 88); this.lstbox.Name = "lstbox"; this.lstbox.Size = new System.Drawing.Size(112, 238); this.lstbox.Sorted = true; this.lstbox.TabIndex = 0; this.lstbox.DoubleClick += new System.EventHandler(this.lstbox_DoubleClick); // // vScrollBar1 // this.vScrollBar1.Location = new System.Drawing.Point(120, 88); this.vScrollBar1.Name = "vScrollBar1"; this.vScrollBar1.Size = new System.Drawing.Size(16, 240); this.vScrollBar1.TabIndex = 1; // // label1 // this.label1.Location = new System.Drawing.Point(24, 48); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(112, 32); this.label1.TabIndex = 3; this.label1.Text = "Directory"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label2 // this.label2.Location = new System.Drawing.Point(192, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(240, 32); this.label2.TabIndex = 4; this.label2.Text = "Search.."; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // txtBoxSearch // this.txtBoxSearch.Location = new System.Drawing.Point(192, 120); this.txtBoxSearch.Name = "txtBoxSearch"; this.txtBoxSearch.Size = new System.Drawing.Size(240, 20); this.txtBoxSearch.TabIndex = 5; this.txtBoxSearch.Text = ""; // // txtBoxAdd // this.txtBoxAdd.Location = new System.Drawing.Point(192, 160); this.txtBoxAdd.Name = "txtBoxAdd"; this.txtBoxAdd.Size = new System.Drawing.Size(320, 20); this.txtBoxAdd.TabIndex = 6; this.txtBoxAdd.Text = ""; // // SearchButton // this.SearchButton.Location = new System.Drawing.Point(448, 120); this.SearchButton.Name = "SearchButton"; this.SearchButton.Size = new System.Drawing.Size(80, 24); this.SearchButton.TabIndex = 7; this.SearchButton.Text = "Search"; // // AddButton // this.AddButton.Location = new System.Drawing.Point(192, 200); this.AddButton.Name = "AddButton"; this.AddButton.Size = new System.Drawing.Size(80, 24); this.AddButton.TabIndex = 8; this.AddButton.Text = "Add"; // // mainMenu1 // this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1}); // // menuItem1 // this.menuItem1.Index = 0; this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2, this.menuItem3}); this.menuItem1.Text = "File"; // // menuItem2 // this.menuItem2.Index = 0; this.menuItem2.Text = "Open"; this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click); // // menuItem3 // this.menuItem3.Index = 1; this.menuItem3.Text = "Save As.."; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(544, 358); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.AddButton, this.SearchButton, this.txtBoxAdd, this.txtBoxSearch, this.label2, this.label1, this.vScrollBar1, this.lstbox}); this.Menu = this.mainMenu1; this.Name = "Form1"; this.Text = "Phone Directory"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// /// private string fileName; public class ReadRAM { private System.Windows.Forms.Button AddButton(); private System.Windows.Forms.Button SearchButton(); // stream through the data in the .txt file private FileStream fileInput; } [STAThread] static void Main() { Application.Run(new Form1()); } private void menuItem2_Click(object sender, System.EventArgs e) { OpenFileDialog fileChooser = new OpenFileDialog(); DialogResult result = fileChooser.ShowDialog(); string fileName = fileChooser.FileName; fileChooser.Filter = "Text files (*.txt)|*.txt|All files|*.*"; if ( result == DialogResult.Cancel) return; if ( fileName == "" || fileName == null) MessageBox.Show( "Invalid File Extension", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); else { fileInput = new FileStream(fileName, FileMode.Open, FileAccess.Read); } } private void lstbox_DoubleClick(object sender, System.EventArgs e) { } } }