|
I have a problem reading lines and showing them from a text file in a ListBox using Windows Form Application.
I made some code but now I'm stucked in a place where I made a comment.
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 LlogariaBankare
{
public partial class KlientForm : Form
{
public string pathi = "Klientet.txt";
public KlientForm()
{
InitializeComponent();
}
private void KlientForm_Load(object sender, EventArgs e)
{
this.ShtoButon.Enabled = true;
this.AzhuroButon.Enabled = false;
this.FshijButon.Enabled = false;
}
private void ShtoButon_Click(object sender, EventArgs e)
{
StreamWriter swKlientet = new StreamWriter(pathi, true);
swKlientet.WriteLine(emriBox.Text + "\t" + mbiemriBox.Text + "\t" +
datelindjaBox.Text + "\t" + vendlindjaBox.Text + "\t" +
xhirollogariaBox.Text + "\t");
swKlientet.Close();
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
|
|
|
|
|
like StreamWriter you can use StreamReader to read text files.
Check the examples here MSDN StremReader class[^]
Jibesh.V.P
India
|
|
|
|
|
You should not be putting the file reading code in the SelectedIndexChanged event or it will be repeated every time the selection changes. You should initialise your ListBox after the form is first loaded, or in some other method.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
OK, now i konw how to read a text file and put it in a listbox, but the problem is that when I type a new line it doesn't show me that new line in Listbox until i debug again the form.
I solved the problem creating a method on form load.
Do I need to create a new method to reload the text file again or what ever.
Cheers in advance for your reply.
|
|
|
|
|
dr_iton wrote: Do I need to create a new method to reload the text file again or what ever. Not necessarily, but you do need to create a method that can add any extra information as it is entered into the program. These controls can be updated at any time by adding or removing entries from them, you do not need to reload them from the beginning.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
After all I have the following problem.
I can remove the lines from listbox, but those lines are not deleted from text file, how to delete a specific line from a text file.
Cheers in advance for your reply.
|
|
|
|
|
dr_iton wrote: I can remove the lines from listbox, but those lines are not deleted from text file Why would you expect them to be, since there is no connection between the two?
The easiest solution would be to recreate the text file from the contents of the ListBox , either when the content changes, or when the program terminates.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
I created a method to show the items in ListBox in Form 1, like this
string linjaListBox = this.ListBox.SelectedItem.ToString();
string emriPjesa = linjaListBox.Split('\t')[0];
string mbiemriPjesa = linjaListBox.Split('\t')[1];
string datelindjaPjesa = linjaListBox.Split('\t')[2];
string vendlindjaPjesa = linjaListBox.Split('\t')[3];
string xhirollogariaPjesa = linjaListBox.Split('\t')[4];
this.emriBox.Text = emriPjesa;
this.mbiemriBox.Text = mbiemriPjesa;
this.datelindjaBox.Text = datelindjaPjesa;
this.vendlindjaBox.Text = vendlindjaPjesa;
this.xhirollogariaBox.Text = xhirollogariaPjesa;
indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
In the next form I used three of TextBoxes from first Form such as emriBox,mbiemriBox and xhirollogariaBox.
How to show the text in those three TextBoxes in form2 if I select a row in ListBox from Form1.
|
|
|
|
|
Why not just:
string linjaListBox = this.ListBox.SelectedItem.ToString();
string[] items = linjaListBox.Split('\t');
this.emriBox.Text = items[0];
this.mbiemriBox.Text = items[1];
this.datelindjaBox.Text = items[2];
this.vendlindjaBox.Text = items[3];
this.xhirollogariaBox.Text = items[04];
indeksLista = ListBox.SelectedIndex;
Form2.emriBox.Text = items[0];
Form2.mbiemriBox.Text = items[1];
Form2.xhirollogariaBox.Text = items[4];
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
OK, my Form2 is named GjendjaForm, and after I rename Form2 with GjendjaForm it shows me the following errors.
GjendjaForm.emriBox is inaccessible due to its protection level
GjendjaForm.mbiemriBox is inaccessible due to its protection level
GjendjaForm.xhirollogariaBox is inaccessible due to its protection level
also it says that:
Error 3 An object reference is required for the non-static field, method, or property 'LlogariaBankare.GjendjaForm.mbiemriBox' C:\Users\Driton Gashi\Documents\Visual Studio 2008\Projects\LlogariaBankare\LlogariaBankare\KlientetForm.cs 112 17 LlogariaBankare
|
|
|
|
|
You need to make your textboxes public, or add some properties or delegate events that allow the transfer of data between the two classes. In the second message you are trying to call a non-static method of the class without an object reference. This is basic C# object handling which you really need to understand before you go much further in this development project. Take a look at these samples[^] for further details.
One of these days I'm going to think of a really clever signature.
|
|
|
|