I need to populate a listview control on Takenshows.cs form, passing parameters to constructor from Main.cs form.
When I'm running the code, the listview is empty. I don't know what is wrong with my code.
Here's the main.cs form on button when I'm sending parameters to Takenshows.cs form:
shows = new Takenshows(Ordnumber,meddt, values,
num);
And this is the Takenshows.cs form:
int Ordnum;
string medDateTime;
string val;
int number;
public Takenshows()
{
InitializeComponent();
}
public Takenshows(int Ordnumber, string
dtmed, string values, int num)
{
Ordnum = Ordnumber;
medDateTime = dtmed;
val = values;
number = num;
InitializeComponent();
}
I know the problem is on Takenshows_load, but I don't know why listview doesn't fill.
private void Takenshows_Load(object sender, EventArgs e)
{
for(int i=0; i< listView1.Items.Count; i ){
listView1.Items[i].SubItems[0].Text = Ordnum.ToString();
listView1.Items[i].SubItems[1].Text = medDateTime;
listView1.Items[i].SubItems[2].Text = val;
listView1.Items[i].SubItems[3].Text = number.ToString();
ListViewItem item = new ListViewItem(listView1.Items[i].SubItems[0].Text);
item.SubItems.Add(listView1.Items[i].SubItems[1].Text);
item.SubItems.Add(listView1.Items[i].SubItems[2].Text);
item.SubItems.Add(listView1.Items[i].SubItems[3].Text);
listView1.Items.Add(item);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Anything can I do to fix this issue?
By certain this is Main.cs form:
public partial class Main : Form
{
int Ordnumber;
string meddt;
string values;
int num;
TakenShows shows;
public Main()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Ordnumber = GetOrdNumberLN.getInstance().GettingOrdNumber();
Ordnumber = Ordnumber 1;
meddt = DateTime.Now.ToString();
num = GetNumberLN.getInstance().GettingNumber();
values = textBox_F.Text " " textBox_PT.Text " " textBox_QT.Text
shows = new Takenshows(Ordnumber,meddt, values,
num);
shows.Show();
}
CodePudding user response:
You need to keep the data you create in some collection container such as DataTable
or List<T>
to be able to set/get/manipulate the collected items. Here's a List<T>
implementation example.
- Switch to the Solution Explorer window and select your project.
- Press Shift Alt C to open the Add New Item dialog.
- Rename the class to
ShowItem.cs
and hit the Add button.
Edit the class as follows:
public class ShowItem
{
public ShowItem() { }
// Find better names...
public int Number { get; set; }
public int OrderNumber { get; set; }
public DateTime MedDate { get; set; }
public string F { get; set; }
public string PT { get; set; }
public string QT { get; set; }
public string Values => $"{F} {PT} {QT}";
}
Now you have the ShowItem
type to create instances from it and keep them in a List<ShowItem>
.
In case you want to show modal dialog, edit the Main
Form as follows:
//
using System.Collections.Generic;
// ...
public partial class Main : Form
{
private readonly List<ShowItem> showItems;
public Main()
{
InitializeComponent();
showItems = new List<ShowItem>();
}
// Find a better name...
private void button2_Click(object sender, EventArgs e)
{
var show = new ShowItem
{
OrderNumber = GetOrdNumberLN.getInstance().GettingOrdNumber() 1,
MedDate = DateTime.Now,
Number = GetNumberLN.getInstance().GettingNumber(),
F = textBox_F.Text,
PT = textBox_PT.Text,
QT = textBox_QT.Text
};
showItems.Add(show);
using (var frm = new Takenshows(showItems))
{
frm.ShowDialog();
}
}
}
... and the Takenshows
Form:
//
using System.Linq;
// ...
public partial class Takenshows : Form
{
public Takenshows()
{
InitializeComponent();
}
public Takenshows(IEnumerable<ShowItem> items) : this()
{
AddItems(items);
}
internal void AddItem(ShowItem item) => AddItems(new[] { item });
internal void AddItems(IEnumerable<ShowItem> items)
{
var lvis = items.Select(x => new ListViewItem(new[]
{
x.OrderNumber.ToString(),
x.MedDate.ToString(),
x.Values,
x.Number.ToString()
}));
listView1.Items.AddRange(lvis.ToArray());
}
}
In contrast, if you need to keep the Takenshows
Form open, then edit the button2_Click
event handler in Main
as shown below. Note, the access modifier of the AddItem
and AddItems
methods is internal
for this scenario.
private void button2_Click(object sender, EventArgs e)
{
var show = new ShowItem
{
OrderNumber = GetOrdNumberLN.getInstance().GettingOrdNumber() 1,
MedDate = DateTime.Now,
Number = GetNumberLN.getInstance().GettingNumber(),
F = textBox_F.Text,
PT = textBox_PT.Text,
QT = textBox_QT.Text
};
showItems.Add(show);
var frm = Application.OpenForms.OfType<Takenshows>().FirstOrDefault();
if (frm == null)
{
frm = new Takenshows(showItems);
frm.Show();
}
else
{
frm.AddItem(show);
frm.Activate();
}
}
Hope this helps you to take-off.