Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm working on a project. And I have a listbox in Form1 and a listbox in Form2. What I have already done is that I have a button that adds values from numericUpDown to listbox in Form1 (values from 1 to 999999). What I need help with is that when I open the dialogbox(Form2) it should showcase values from listbox(Form1) which are between and including 1-100 in listbox(form2). After clicking a button named "yes" it should save all the values from listbox(form2) to a text file.

Thank anyone willing to help.

What I have tried:

Tried anything within my powers.
Posted
Updated 12-Mar-20 22:36pm
Comments
ZurdoDev 12-Mar-20 13:16pm    
You have several different requirements. Where are you stuck?
Member 14771221 12-Mar-20 13:23pm    
I'm stuck at the showcasing the values ranging from 1 to 100 in the listbox in form 2. I don't know how to transfer them from form1 to form2
ZurdoDev 12-Mar-20 13:17pm    
Saving to a text file is very easy, so just google c# save text to file.

Passing values from one form to another can be done many different ways so google that as well.
Member 14771221 12-Mar-20 13:24pm    
I tried to google it many times. If I understood it I wouldn't be asking for help right now.
ZurdoDev 12-Mar-20 13:29pm    

You could give Form2 a constructor accepting an enumeration of integers as parameter, which you would use to populate the second listbox:
C#
public Form2()
{
   InitializeComponent();
}

public Form2(IEnumerable<int> values) : this()
{
   theSecondListBox.Items.AddRange(values);
}
You would then instantiate the Form2 instance by using the new constructor:
C#
// In Form1:
Form2 form = new Form2(theFirstListBox.Items.Where(i => i > 0 && i < 101));
 
Share this answer
 
Comments
Member 14771221 12-Mar-20 14:03pm    
I don't really know how to implement it into my code. It seems like it's beyond my understanding of c#.
-----------------------------------------------------------
private void Pridej_Click(object sender, EventArgs e)
{
listBox1.Items.Add(Hodnota.Value);
Hodnota.ResetText();
}

private void Odeber_Click(object sender, EventArgs e)
{
int smaz = listBox1.SelectedIndex;
listBox1.Items.RemoveAt(smaz);
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
using (Form2 form2 = new Form2())
{
if (form2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.Text = form2.SelectedText;
}
}
}

This is os far in my Form1 (as the stuff to add and remove values to/from listboxForm1)

Form2 is dialog windows/box. Is empty for me. Except for simple SelectedText { get; set; }
phil.o 13-Mar-20 4:53am    
Then you will have to get deeper into your "understanding of c#". This is pretty basic stuff: constructors, method parameters, instances.
I already showed you how to modify your Form2 class so that you can instantiate it with a list of integers, as well as the way to instantiate it on Form1 to take advantage of the new constructor. The code block in your comment shows that you did not even try to apply it to your existing code. Maybe you are really missing the very basic stuff about C# / OOP / Windows Forms; in that case, I can only advise you to follow a serious tutorial about them before trying to implement any concrete solution. We can eventually provide some useful links for that, if you need.
Maciej Los 13-Mar-20 4:36am    
5ed!
 
Share this answer
 
Comments
phil.o 13-Mar-20 4:55am    
Good links :)
Maciej Los 13-Mar-20 5:05am    
Thanks ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900