I have list, listbox, button and a textbox.
My idea is to make that by clicking on the button, the content of the textbox is added to the list, and then pass the data to a listbox.
My problem is, if you add what I write, but the items that are in the listbox are overwritten by the new one that you insert. and I want only more items added. to the list and to go to the listbox. Thank you very much for your answers. This is the code of my button:
private void button54_Click(object sender, RoutedEventArgs e)
{
List<Playlists> List1 = new List<Playlists>();
List1.Add(new Playlists(textBox3.Text, #rutaalbum));
lbListbox.ItemsSource = List1;
}
I am just making a demo stand in for your Playlists class.
ToString has been overridden in order to display some text in the listbox. Without it, you will only display the class name.
ObservableCollection is used instead of List, so that the listbox updates when a new item is added.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MyList = new ObservableCollection<Playlists>();
MyListBox.ItemsSource = MyList;
}
private ObservableCollection<Playlists> MyList { get; }
private void Button_Click(object sender, RoutedEventArgs e)
{
MyList.Add(new Playlists(textBox3.Text));
}
}
public class Playlists
{
public Playlists(string title) { Title = title; }
public string Title { get; }
public override string ToString() { return Title; }
}
It seems , you are always creating items with new list. need to add items to existing list.
use below code. it would be useful.
private void button54_Click(object sender, RoutedEventArgs e) {
lbListBox.Items.Add(new Playlists(textBox3.Text, #rutaalbum));
}
Related
I have a child form that displays with a button in user control and I want to clone ListView in user control to its child form.
I checked with breakpoint and printed the list but it gives me error like out of bounds or instance variable is zero.
public partial class JobForm: Form
{
public Job()
{
InitializeComponent();
}
List<string> joblist = new List<string>();
public List<string> _var
{
set {
joblist = value; }
}
}
User Control
private void button_Click(object sender, EventArgs e)
{
//MessageBox.Show(_var[0].ToString());
JobForm jb = new JobForm();
jb.Show();
}
public List<string> listViewJob()
{
_var.Add(item);
return _var;
}
public List<string> _var { get;} = new List<string>();
I used also 'var parent = this.Parent as JobForm; parent.ID2 = ID2; but it gives me same error. So I check with breakpoint it is a correct list until the form shows then I get null or out of bound (_var) in User Control and in Form. I would appreciate if you could write an example.
It is very confusing that both JobForm and the user control have a list called _var. Its best to use more descriptive variable names.
I'm not completely sure what you want to achieve, but basically, when you have created the child form you can access its properties.
For instance:
private void button_Click(object sender, EventArgs e)
{
//MessageBox.Show(_var[0].ToString());
JobForm jb = new JobForm();
// Members of 'jb' are available here
this._var = jb._var;
jb.Show();
}
I have a ComboBox and an ObservableCollection set as DataSource for that ComboBox.
When I programmatically add/remove items from the observable collection, nothing changes in the ComboBox.
What am I doing wrong?
Part 2: tried to put a BindingSource as a proxy for ObservableCollection. When programmatically added/removed items from ObservableCollection, no event like ListChanged or similar fired.
How can I make a ComboBox automatically update its list when underlying collection changes?
public Form1()
{
InitializeComponent();
comboBox1.DataSource = new ObservableCollection<MyItem>(
new []
{
new MyItem() { Name = "AAA"},
new MyItem() { Name = "BBB"},
});
}
private void Button3_Click(object sender, EventArgs e)
{
// Nothing changes in the ComboBox when I add a new item to ObservableCollection
((ObservableCollection<MyItem>)(comboBox1.DataSource))
.Add(new MyItem() { Name = Guid.NewGuid().ToString()});
}
}
public class MyItem
{
public string Name { get; set; }
}
It helps to wrap a list in a BindingList<T>. Here a little test code:
public partial class Form1 : Form
{
private readonly List<string> _coll = new List<string> { "aaaaa", "bbbbb", "ccccc" };
private readonly BindingList<string> _blist;
private readonly Random _rand = new Random();
private const string Templ = "mcvnoqei4yutladfffvtymoiaro875b247ytmlarkfhsdmptiuo58y1toye";
public Form1()
{
InitializeComponent();
_blist = new BindingList<string>(_coll);
comboBox1.DataSource = _blist;
}
private void AddButton_Click(object sender, EventArgs e)
{
int i = _rand.Next(Templ.Length - 5);
string s = Templ.Substring(i, 5);
_blist.Add(s);
}
}
Note that you have to make the changes (Add, Remove etc.) to the BindingList. The BindingSource works the same way.
Beginner here.
I have a list on my MainPage that I am adding items to by typing stuff into a textbox submitting it. After creating a listview item, I would like to be able to click on an item and see its value in another page.
I created a DetailsPage for this, but I don't know what the best way is to share that data across these pages.
This is what I have in my MainPage so far:
public sealed partial class MainPage : Page
{
public int itemCounter;
public MainPage()
{
this.InitializeComponent();
//This is the button that submits the entered text
public void btnSubmitInPopup_Click_1(object sender, RoutedEventArgs e)
{
//Create new listview item and textblock
ListViewItem item = new ListViewItem();
TextBlock txtBloodSugarValue = new TextBlock();
//Save text input value to label and add item to list
txtBloodSugarValue.Text = txtInput.Text;
item.Content = txtBloodSugarValue;
mainListView.Items.Add(item);
//Reset text input field
txtInput.Text = "";
//Update counter of items in list
itemCounter = mainListView.Items.Count();
lblItemCounter.Text = itemCounter.ToString();
}
}
My DetailPage literally has just one label that I am trying to populate with whatever value is in the listview item that I clicked on in my MainPage.
What's the best way to do this?
Hope this makes sense.
Thanks so much.
You have 3 way to do that:
declaring static variable and set that in first form and use in second form(or anywhere!)
Create public field/propeey inside second form class and set it from first form
Pass value as parameter to constructor/function of second class
I add second form class here:
public static int StaticVariable { get; set; }//First Method
public int PublicProperty { get; set; }
public Form2(int Value)
{
InitializeComponent();
//Do your code here with constructor way here
}
public Form2()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//Do your code for 1,2 here
}
public void SetValueWithFunction(int value)
{
//Do your code for setting value with second type in Number 3
}
I hope this helps :)
As the title suggests, I want to filter the values in a combobox according to what is written in a textbox. The combobox takes values from a list. I have tried AutoCompleteMode and AutoCompleteSource but it doesn't let me add any values to the combobox when I use these. The combobox holds values of a list of the following class.
class Groep
{
//Fields
private string naamGroep;
//Properties
public string NaamGroep
{
get { return this.naamGroep; }
set { naamGroep = NaamGroep; }
}
//Constructor
public Groep(string naam)
{
this.naamGroep = naam;
}
This is the list:
List<Groep> Groepen = new List<Groep>();
I have two textboxes. One to add items to the list and the other to filter the combobox.
Do it using a foreach loop
private void Button1_Click(object sender, EventArgs e)
{
ComboBox1.Items.Clear();
foreach (Groep g in Groepen.Where(g => g.NaamGroep.Contains(TextBox1.Text)))
ComboBox1.Items.Add(g.NaamGroep);
}
I have a listbox full of items for my order.
I want to take all of the items inside my listbox and transfer them into my listview.
Then I want to take my listview and display it in another form (my messagebox).
My new listview:
private void CustomerInfo_Click(object sender, EventArgs e)
{
ListViewItem customers = new ListViewItem(fullName.Text);
customers.SubItems.Add(totalcount.ToString());
customers.SubItems.Add(total.ToString());
customers.SubItems.Add(Address.Text);
customers.SubItems.Add(telephone.Text);
for (int i = 0; i < OrderlistBox.Items.Count; i++)
{
customers.SubItems.Add(OrderlistBox.Items[i].ToString());
}
Customers.Items.Add(customers);
//CLEAR ALL FIELDS
OrderlistBox.Items.Clear();
fullName.Text = "";
Address.Text = "";
telephone.Text = "";
totalDue.Text = "";
totalItems.Text = "";
}
My contextMenuStrip, so when I click on the customer I can get its info (name, address, order, etc.):
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.ShowDialog();
}
}
My new form, the messagebox where I will display all the info for the client:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
public string name;
public string address;
public string telephone;
public ListViewItem order = new ListViewItem();
private void MessageBoxForm_Load(object sender, EventArgs e)
{
lblName.Text = name;
lbladdress.Text = address;
lbltelephone.Text = telephone;
orderListView.Items.Add(order);
}
}
I'm sorry if this seems confusing but I'm just looking for help to go in the right direction. Any help is appreciated.
One way to do this is to put the data that you want to display in some sort of ViewModel, basically a class or set of classes that has the data that you want to display. Then the main form can display it, and you can pass a reference to that ViewModel to the message box and it can display it as well.
In general you want to avoid any kind of code that directly ties controls from different forms together.
The easiest way based on your current setup is to simply pass your list view data across to your MessageBoxForm e.g.
public partial class MessageBoxForm : Form
{
...
public void LoadListView(ListViewItemCollection items)
{
orderListView.Clear();
orderListView.AddRange(items);
}
}
....
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm
{
name = Customers.SelectedItems[0].SubItems[0].Text,
address = Customers.SelectedItems[0].SubItems[3].Text,
telephone = Customers.SelectedItems[0].SubItems[4].Text,
};
myformmessagedialog.LoadListView(Customers.Items);
myformmessagedialog.ShowDialog();
}
}
Basic answer is you don't.
You maintain a collection of items (whatever they are).
You display them in a list box.
You display them in a list view.
If you want say select some from the list box and only move them to the list view.
Then you use the listbox selection to find them in your collections of items, create a list of selected ones then passs that to the form with the listview to display.
Don't use UI controls to store your data and try really hard to never make one form's UI directly dependant on another.
I'm guessing what you'd need (and I could have misunderstood what you are looking for) is a new method in you MessageBoxForm to pass in your Customers object:
private void customerInformationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Customers.SelectedItems.Count != 0)
{
var myformmessagedialog = new MessageBoxForm;
myformmessagedialog.Customers = Customers;
if (myformmessagedialog.ShowDialog() == DialogResult.OK)
{
Customers = myformmessagedialog.Customers;
}
}
}
If so, simply modify your class to be something like this:
public partial class MessageBoxForm : Form
{
public MessageBoxForm()
{
InitializeComponent();
}
private void MessageBoxForm_Load(object sender, EventArgs e)
{
if (Customers != null)
{
// add your code here to add your Customers as needed
}
}
public Customers Customers { get; set; }
}
To access anything from the parent form you need to pass it to the child form so
myformmessagedialog.ShowDialog();
becomes
myformmessagedialog dialog = new myformmessagedialg(this);
dialog.ShowDialog();
and your class constructor becomes this:
public MessageBoxForm(myformmessagedialog parent){
name=parent.fullName.Text;
address=parent.address.Text;
...etc...
InitializeComponent();
}
Though it might be better to just pass in the name, address, etc rather than the whole form, this way is nice for while you are changing things because you have one less place to change to add another variable to pass.