i want to call a void from other form. there is no error when i called void from same form. when i called from another form it return "index was out of range" error

namespace browser
{
public partial class mainF : Form
    {
int bo;       
public List<string> home = new List<string>();
       public mainF()
        {
            InitializeComponent();
            string[] lineurl =  File.ReadAllLines(cupath + "\\db.txt");
            foreach (string line in lineurl)
            {
                if (line != "") { home.Add(line); }
            }
            bo = -1;
            
        }
 private void button1_Click(object sender, EventArgs e)
        {
            pembalik();
            label1.Text = initialing.curHome;
        }
public void pembalik()
        {
            
                bo += 1;
                initialing.curHome = home[bo];   ////index error here when called from other form
                domainmaker(initialing.curHome);
                initialing.domain = domain;
                linkopen open = new linkopen();
                open.show();
        }
private void domainmaker(string hook)
        {
            domain = hook.Substring(0, hook.LastIndexOf("/") + 1);
        }
    }
public class initialing
    {
        public static string __domain = "";
        public static string __cur = "";
        public static string domain
        {
            get { return _domain; }
            set { _domain = value; }
        }
        public static string curHome
        {
            get { return _cur; }
            set { _cur = value; }
        }
    }
}

i try call pembalik() from this form

namespace browser
{
    public partial class linkopen : Form
    {
       
        public linkopen()
        {
            InitializeComponent();
            label1.Text = label1.Text.Substring(0,label1.Text.IndexOf(":")+1)+ initialing.curHome;
            label2.Text = label2.Text.Substring(0,label2.Text.IndexOf(":") + 1) + initialing.domain;
            
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            mainF a = new mainF();
            
            a.pembalik();
            this.close();
            
        }

    }
}

You are not initiating the form, therefore home is empty..


you need to initiate form with its constructor in order to run the below code which will populate your list:

string[] lineurl = File.ReadAllLines(cupath + "\\db.txt");
foreach (string line in lineurl)
{
if (line != "") { home.Add(line); }
}

You are not initiating the form, therefore home is empty..


you need to initiate form with its constructor in order to run the below code which will populate your list:

string[] lineurl = File.ReadAllLines(cupath + "\\db.txt");
foreach (string line in lineurl)
{
if (line != "") { home.Add(line); }
}

each i clicked button in mainF form

#
private void button1_Click(object sender, EventArgs e)
#
{
#
pembalik();
#
label1.Text = initialing.curHome;
#
}

label1.text show url that stored in db.txt line by line

but if i click button in linkopen form then error appear

if you try accessing home[0] and home[1] from other form since its public.. after initialization.. do you get something? the error is implying that you are attempting to retrieve an index from the list which does not exist

if you try accessing home[0] and home[1] from other form since its public.. after initialization.. do you get something? the error is implying that you are attempting to retrieve an index from the list which does not exist

yep,, linkopen form return error
i have modified mainF form
i added

private void timer1_Tick(object sender, EventArgs e)
        {
            bo+=1;
            linkopen ap = new linkopen();
            Form open;
            if ((open = IsFormAlreadyOpen(typeof(linkopen))) == null && initialing.tutup == true)
            {
                tesbutton.PerformClick();
               label1.Text = bo.ToString();
            }

all url in db.txt appear on linkopen form
thanks jfarrugia
finally i can finish this project ^_^

good luck :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.