1.11M Members

Help nedded to show selected Items from Form1 in Form2.

 
0
 

I need to create a software that manages a bank account.
I created 2 forms.
Form one (Form1 "in reality is KlientetForm") has 5 textboxes, 6 buttons and a listbox.
Form twoo (Form2 "in reality is GjendjaForm") has 3 textboxes (the same name as in Form1), 2 listboxes and 3 buttons.
I created the program that it can save the data in text file.
I can read the data from Form1 and show the details in listbox.
When i select a row in a listbox the items are shown in textboxes.

My question is how to show the content of a row if I select it from Form1 in textboxes in Form2.
Here are some code I made to show the details in Form1.

 private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

           this.ShtoButon.Enabled = false;
            this.AzhuroButon.Enabled = true;
            this.FshijButon.Enabled = true;
            this.AktivButon.Enabled = true;
            this.JoaktivButon.Enabled = true;
            this.GjendjaButon.Enabled = true;

            try

             {
                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());




            }
            catch
            { }

        }

The Form2 begins with following:

namespace LlogariaBankare
{
    public partial class GjendjaForm : Form
    }

I'm trying to fix the problem with GjendjaForm.emriBox.Text = emriPjesa;
but i can not fix the problem because: it is inaccessible due to its protection level.

Thank you in advance for your reply.

 
0
 

Set the "Modifiers" property of the controles on Form2 to "public".

  • At the top of Form1 declare a new instance of Form2 so you can access the controls on Form2 Form2 frm2 = new Form2();.
  • Next, in a method in Form1 do frm2.emriBox.Text = this.emriBox.Text; (this will put the contents of emriBox on Form1 into emriBox of Form2.
 
0
 

I tryed but i still get the error message:
Form1.emriBox it is inaccessible due to its protection level.

 
0
 

it's set to private, make it public and it should work

 
0
 

I made this:

 public void ListBox_SelectedIndexChanged(object sender, EventArgs e)

And still have no solution.

 
0
 

If something is inaccessable due to protection level they are usually set to private and means they are NOT set to public. If a textbox on form1 is set to private it mean that the textbox can only be seen by form1 but NOT form2 and like wise for a textbox on form2 this is set to private and not public.

In design view go to the properties off all your textboxes and change their "Modifiers" property to say "public". This will make the textbox accessable and solve your "inaccessible" issue.

 
0
 

I fixed all but I still can't see the contents on textboxes, so please tell me where I'm wrong. Finally I don't see any errors now.

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 LlogariaBankare
{

    public partial class KlientForm : Form
    {

        List<KlasaKlientet>ListaKlientet = new List<KlasaKlientet>();
          // int pjesLista;
        public void LexoKlientetFajll()


        {
            ListBox.Items.Clear();
            List<string> linja = new List<string>();

            string strLinja = "";
            StreamReader srKlientin = new StreamReader(pathi, true);
            try
            {
                while ((strLinja = srKlientin.ReadLine()) != null)
                {
                    ListBox.Items.Add(strLinja);
                }
                srKlientin.Close();
            }
            catch
            { }
        }
        public void AppendTxt(string text)
        {

        }  


        public string pathi = "Klientet.txt";
        int indeksLista;


        public KlientForm()
        {
            InitializeComponent();

        }

        private void KlientForm_Load(object sender, EventArgs e)
        {

            this.ShtoButon.Enabled = true;
            this.AzhuroButon.Enabled = false;
            this.FshijButon.Enabled = false;
            this.AktivButon.Enabled = true;
            this.JoaktivButon.Enabled = true;

            LexoKlientetFajll();
            indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());

        }


        private void ShtoButon_Click(object sender, EventArgs e)
        {
            ShtoKlientFajll();
            ShtoKlientList();
            MerrShenimet();


        }
        public string MerrShenimet()
        {
            return emriBox.Text + "\t" + mbiemriBox.Text + "\t" + datelindjaBox.Text + "\t" +
                vendlindjaBox.Text + "\t" + xhirollogariaBox.Text;
        }



                public void ShtoKlientFajll()
                {
                    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();
                }
                public void ShtoKlientList()
                {
                    try
                    {
                        ListaKlientet.Add(new KlasaKlientet(emriBox.Text, mbiemriBox.Text, datelindjaBox.Text, 
                            vendlindjaBox.Text, xhirollogariaBox.Text));
                    }
                    catch
                    {
                        MessageBox.Show("Gabim ne shenime", "Gabim!");
                    }
                }
        public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            //GjendjaForm gjf = new GjendjaForm();
            this.ShtoButon.Enabled = false;
            this.AzhuroButon.Enabled = true;
            this.FshijButon.Enabled = true;
            this.AktivButon.Enabled = true;
            this.JoaktivButon.Enabled = true;
            this.GjendjaButon.Enabled = true;

            try
            {
                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;



                string emrPjesa = linjaListBox.Split('\t')[0];
                string mbiemrPjesa = linjaListBox.Split('\t')[1];
                string xhirollogariPjesa = linjaListBox.Split('\t')[2];

                KlientForm kf = new KlientForm();
                {
                    kf.emriBox.Text = emriPjesa;
                    kf.mbiemriBox.Text = mbiemriPjesa;
                    kf.xhirollogariaBox.Text = xhirollogariaPjesa;
                    //indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
                }
                indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
            }
            catch
            { }

        }
        //Pastro fushat
        public void PastroFushat()
        {
            this.emriBox.Text = "";
            this.mbiemriBox.Text = "";
            this.datelindjaBox.Text = "";
            this.vendlindjaBox.Text = "";
            this.xhirollogariaBox.Text = "";
        }



        private void AzhuroButon_Click(object sender, EventArgs e)
        {

        }


        public void vendlindjaBox_TextChanged(object sender, EventArgs e)
        {

        }

        public void xhirollogariaBox_TextChanged(object sender, EventArgs e)
        {

            Random rasti = new Random();
             int numri = rasti.Next();
            xhirollogariaBox.Text = Convert.ToString(numri);


        }

        private void FshijButon_Click(object sender, EventArgs e)
        {
            FshijKlientin(indeksLista);

         }





        public void FshijKlientin(int indeksi)
        {

            try
            {
                ListBox.Items.RemoveAt(indeksi);

            }
            catch { }
        }

        private void GjendjaButon_Click(object sender, EventArgs e)
        {
                GjendjaForm frmGjendja = new GjendjaForm();
                frmGjendja.Show();
                string linjaListBox = this.ListBox.SelectedItem.ToString();

                string emriPjesa = linjaListBox.Split('\t')[0];
                string mbiemriPjesa = linjaListBox.Split('\t')[1];
                string xhirollogariaPjesa = linjaListBox.Split('\t')[2];

                this.emriBox.Text = emriPjesa;
                this.mbiemriBox.Text = mbiemriPjesa;
                this.xhirollogariaBox.Text = xhirollogariaPjesa;

                indeksLista = Convert.ToInt32(ListBox.SelectedIndex.ToString());
        }

        public void emriBox_TextChanged(object sender, EventArgs e)
        {

        }

        public void mbiemriBox_TextChanged(object sender, EventArgs e)
        {

        }

        public void datelindjaBox_TextChanged(object sender, EventArgs e)
        {

        }

        }

    }

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 LlogariaBankare
{
    public partial class GjendjaForm : Form
    {

        public string caku = "Gjendja.txt";

        List<string> rreshti = new List<string>();


        public GjendjaForm()


        {
            InitializeComponent();


        }

        public void GjendjaForm_Load(object sender, EventArgs e)
        {

        }

        private void ShtoButon_Click(object sender, EventArgs e)
        {
            StreamWriter scGjendja = new StreamWriter(caku, true);
            scGjendja.WriteLine(emriBox.Text + "\t" + mbiemriBox.Text + "\t" +
                xhirollogariaBox.Text + "\t");
            scGjendja.Close();
        }

        public void emriBox_TextChanged(object sender, EventArgs e)
        {

        }

        public void KlientiListBox_SelectedIndexChanged(object sender, EventArgs e)
        {



        }

        public void xhirollogariaBox_TextChanged(object sender, EventArgs e)
        {

        }

        public void mbiemriBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LlogariaBankare
{
   public class KlasaKlientet
    {

       string emri, mbiemri, datelindja, vendlindja, xhirollogaria;
       public KlasaKlientet(string emri, string mbiemri, string datelindja,
                            string vendlindja, string xhirollogaria)
       {
           this.emri = emri;
           this.mbiemri = mbiemri;
           this.datelindja = datelindja;
           this.vendlindja = vendlindja;
           this.xhirollogaria = xhirollogaria;
       }



    }

}

This is all I've done.

 
0
 

Try putting a break point in a the position in your code where data is frist read from the file to see if it actually reads the data, then press F11 to step through the code and hover over your variables to try and see where the code is going wrong.

Sorry I can't be more helpful at the moment.

 
0
 

Try this:

     public GjendjaForm()
    {
        InitializeComponent();
        emriBox.Text = KlientForm.emrBox.Text;
        mbiemriBox.Text = KlientForm.mbiemriBox.Text;
        xhirollogariaBox.Text = KlientForm.xhirollogariaBox.Text;
    }

When you Show the form the textboxes should thave the values.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: