Hey guys,
I need some help.
So i have a windows form and some extra user contols for it.
For one user control, lets call it C1, i have a button, and for the second control i have a listbox with items (websites).
Now what i want is to loop trough all those site when pressing the button on C1 and perform an action.
So I’m a bit confused on how to do this, could someone help?
Thanks in advance!
🤝
Feb 4, 2018 03:34 PM
(This post was last modified: Feb 4, 2018 03:35 PM by Siy.)
I hope i'm understanding you correctly.
Code private void C1_Click(object sender, RoutedEventArgs e)
{
foreach (string website in lb.Items) //loop through your listbox (lb is the listbox)
{
MessageBox.Show(website); // your action here
}
}
(Feb 4, 2018 03:34 PM)Siy Wrote: I hope i'm understanding you correctly.
Code private void C1_Click(object sender, RoutedEventArgs e)
{
foreach (string website in lb.Items) //loop through your listbox (lb is the listbox)
{
MessageBox.Show(website); // your action here
}
}
Well kinda, but now, the items (here lb) are on a diffrent form (user control), so i cant acces it like that but i need to do it like this: secondControl.lb.Items but for this i need to declare lb as a variable, but i dont know if i need to make it like an array etc
DO NOT DEAL WITH THIS MEMBER
Feb 5, 2018 11:39 AM
(This post was last modified: Feb 5, 2018 11:39 AM by DeadKube.)
(Feb 4, 2018 03:55 PM)Owl Wrote: You can pass the data to a constructor of the new form. If synchronization is required you could encapsulate as a binding source.
All you can hope for now is that he even understands what you just said.
Code public Form2(ListBox listBox)
{
InitializeComponent();
listBox1.Items.AddRange(listBox.Items);
}
Even better, make it into an array instead of a ListBox, if you can handle that of course.
DO NOT DEAL WITH THIS MEMBER
(Feb 5, 2018 11:39 AM)DeadKube Wrote: All you can hope for now is that he even understands what you just said.
Code public Form2(ListBox listBox)
{
InitializeComponent();
listBox1.Items.AddRange(listBox.Items);
}
Even better, make it into an array instead of a ListBox, if you can handle that of course.
Why would you pass a whole listbox reference if all he needs is the array of items to copy? Just sent over the array or a collection in the constructor.
DO NOT DEAL WITH THIS MEMBER
Feb 5, 2018 11:46 AM
(This post was last modified: Feb 5, 2018 11:48 AM by DeadKube.)
(Feb 5, 2018 11:45 AM)Ninja Wrote: Why would you pass a whole listbox reference if all he needs is the array of items to copy? Just sent over the array or a collection in the constructor.
Read the last line ffs. Also, you know I don't post full solutions, now don't you?
You can argue with me all you like about it not being helpful or what not, but I will not change my methods. If people want to learn, they'll learn and I prefer they do it the hard way instead of getting the 'food' delivered to their doorsteps.
DO NOT DEAL WITH THIS MEMBER
Feb 5, 2018 11:49 AM
(This post was last modified: Feb 5, 2018 11:50 AM by Ninja.)
(Feb 5, 2018 11:46 AM)DeadKube Wrote: Read the last line ffs.
You still learn him bad things by making him think that is okay to do.
(Feb 5, 2018 11:46 AM)DeadKube Wrote: Also, you know I don't post full solutions, now don't you?
Neither do you post good solutions.
(Feb 5, 2018 11:46 AM)DeadKube Wrote: You can argue with me all you like about it not being helpful or what not, but I will not change my methods. If people want to learn, they'll learn and I prefer they do it the hard way instead of getting the 'food' delivered to their doorsteps.
But you are teaching the wrong thing. If you really wanted to help you would have either gave a valid example or no example at all and told him what to do.
DO NOT DEAL WITH THIS MEMBER
(Feb 5, 2018 11:49 AM)Ninja Wrote: You still learn him bad things by making him think that is okay to do.
Neither do you post good solutions.
But you are teaching the wrong thing. If you really wanted to help you would have either gave a valid example or no example at all and told him what to do.
Oh great. Another pointless discussion. Some people can't leave it alone now can they? The answer has been provided to the man already. It's been stated. Now let it be and allow the chips to fall where they may. There are better uses for your time. Trust me.
DO NOT DEAL WITH THIS MEMBER
(Feb 5, 2018 11:52 AM)DeadKube Wrote: Oh great. Another pointless discussion. Some people can't leave it alone now can they? The answer has been provided to the man already. It's been stated. Now let it be and allow the chips to fall where they may. There are better uses for your time. Trust me.
I am perfectly capable of managing my time and what I do with it. But thanks.
So i see that there is a discussion going on.
I just wanna say to y’all thanks though for taking the time to want to help, an i dont need a full exemple, but a push in the right direction would be nice,
Thanks again :)
Is there any chance we can move childish arguments off threads? If none of you have nothing nice to say then why say it at all. The question was explained terribly but this has distracted both of you from what was even asked.
OP: You need some serious work on your standard of posting questions. You have given limited information with a poor example. Now a lot of people will not bother posting on threads when it comes down to lack of effort.
Ok, so let's move forward. You did not even need to mention the user control or a form. The instance where these controls exist is irrelevant. Your question Is so badly wrote all you have asked is
once broken down is how can i loop through a listbox. Or are you saying you have Two USER CONTROLS that you would like to communicate?
Firstly not controls should be accessed by anything else that they created on. PRIVATE. So when you create this second user control, pass the user control (1) instance through the constructor
and then using a property to be readonly and of type string().
DeadKube: Ninja is correct. CONTROLS, are strictly UI elements. If not being used as such then don't. Ever. No if or buts. NEVER.
If the listbox is on a different form than the button then you need to consider what populates that listbox, and use that directly instead of the object collection if you're not using a datasource binding.
|