|
The question is not clear.
Two hints nevertheless:
1. ListBoxes (anc ComboBoxes) can do much more than hold strings; you can store any kind of object in there, i.e. show some text (maybe their ToString result) but also contain a lot more information, which might define their sorting order.
2. There are all kinds of ways to get a collection sorted; here[^] is an overview I created.
|
|
|
|
|
Excuse Me !
See Here :
ListBox1.Items[0]-->"City One" ---------------- ListBox2.Items[0]=2500
ListBox1.Items[1]->"City Two" ---------------- ListBox2.Items[1]=3000
ListBox1.Items[2]-->"City Three" ---------------- ListBox2.Items[2]=1500
When I sorting ListBox2(by Number) , I want To Change ListBox1 (by the Index of ListBox2) Like these :
ListBox1.Items[0]->"City Two" <----------------> ListBox2.Items[0]=3000
ListBox1.Items[1]-->"City One" <----------------> ListBox2.Items[1]=2500
ListBox1.Items[2]-->"City Three" <----------------> ListBox2.Items[2]=1500
Thanks
|
|
|
|
|
If I understand you correctly (you are still only spending one short sentence to "explain" things!), I think you're best approach would be:
1. to have a class City with properties for Name, SomeNumber, and maybe more.
2. have one, not two, collections of such City instances; the collection lives outside any ListBox (as the cities do). A List<City> would do fine as collection here.
3. sort that collection any way you choose. My article explains how.
4. have a ListBox that shows the SomeNumber property of said collection. Databinding could do that for you (set DataSource and DisplayMember, and that is why City needs properties).
5. have another ListBox that shows the Name property of said collection. Databinding again.
|
|
|
|
|
my English is not Good
my item in ListBox2 is 3 and is Only Number field :
as I said :
ListBox2.Items[0]=2500
ListBox2.Items[1]=3000
ListBox2.Items[2]=1500
Also the Items for ListBox1 is 3 but String Field , Like these as I Said :
ListBox1.Items[0]-->"City One"
ListBox1.Items[1]->"City Two"
ListBox1.Items[2]-->"City Three"
--------------------------------
in fact my project is about Population , in ListBox1 , we Name of City and in ListBox2 is Their Population :Like these ,
City One =2500
City Two=3000
City Three=1500
Now When I Want To sort ListBox2 , Also ListBox1 should be Sorted .
thanks for your patient
Regards!
|
|
|
|
|
My earlier reply stands.
You should not put just strings in a ListBox, if the population numbers were 10000, 3000, and 1000 you would never get them sorted properly using strings.
|
|
|
|
|
But We have "Two" ListBox, ListBox1 for City And ListBox2 for their Population
Regards!
|
|
|
|
|
I interpret your question as:
Given two List Boxes:
1. where initially ListBox1 is sorted by some criterion (alpha-numeric ?)
2. where initially each ListBox2 item is associated with the item in ListBox1 with the same index (even though they may "appear" unsorted).
Then:
1. in the case where you sort ListBox2 by some criterion
2. you want the items in ListBox1 to remain in matching ordinal position with their "associated" items in ListBox2.
What's "missing" from the picture ... but we can "guess" at ... is if you always are going to sort ListBox2 the same way: we assume here that you are not sorting by just setting
ListBox2.Sorted = true; Which will give you an alphabetic ascending sort. Your example result shows a sort of ListBox2 in descending order.
Since ListBox Items are all strings, there is the question of whether you really want to sort the Items in ListBox2 as strings or as numbers.
Solution ?
1. abstract away the data and associations from the ListBoxes:
a. I'd do this with a Dictionary<string, int> : why use string as the Key: because it's unlikely you'd have two identical city-names, but, perhaps, very possible, you'd have identical population ?
2. On Form Load, or whatever, populate this Dictionary with the CityName-Population data.
3. Create a function that populates the ListBoxes based on enumerating the Dictionary<string, int>
4. Create a function that sorts the Dictionary by Value (strings) in descending order: Dictionary + Linq will do this for you.
Example code:
private Dictionary<string, int> PopCityDict;
private void Form1_Load(object sender, EventArgs e)
{
PopCityDict = new Dictionary<string, int>
{
{"City One", 2500},
{"City Two", 3000},
{"City Three", 1500}
};
InitializeListBoxes();
}
private void InitializeListBoxes()
{
listBox1.Items.Clear();
listBox2.Items.Clear();
foreach (var kvp in PopCityDict)
{
listBox1.Items.Add(kvp.Key);
listBox2.Items.Add(kvp.Value);
}
}
private void SortDescendingButton_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
listBox2.Items.Clear();
foreach (var kvp in PopCityDict.OrderByDescending(criterion => criterion.Value))
{
listBox1.Items.Add(kvp.Key);
listBox2.Items.Add(kvp.Value);
}
} Discussion: The sort will not change the underlying structure (order) of the PopCityDict Dictionary ! Which means to reset the ListBoxes to their original order you can just call
InitializeListBoxes(); If you really want to re-arrange the actual order in the Dictionary: have fun
... edit #1 ...
You could easily implement any number of sorting modes by making a more general-purpose sort routine where the OrderBy#.... Linq method used on 'PopCityDict was chosen based on an input parameter to the sorting routine.
And use of a Dictionary is, I am sure, only one option; you could probably use a List of Tuples, etc.
... end edit #1 ...
Scipio: "That's true, Berganza; and what makes the miracle greater is, that we not only speak, but hold intelligent discourse, as though we had souls capable of reason; whereas we are so far from having it, that the difference between brutes and man consists in this, that man is a rational animal, and the brute is irrational." Cervantes, "Colloquy of Dogs," 1613CE. The two talking dogs, Scipio, Berganza, are hallucinations in the mind of a soldier with plague fever undergoing a "sweating" cure.
modified 27-Nov-11 8:25am.
|
|
|
|
|
There have been several things suggested but you don't seem to be understanding it, I'll try to help.
You seem to be getting confused about how much data you have. You only have ONE collection of cities, you just happen to be showing the individual bits of information (name, population) on two seperate controls. The first thing to do is to get your information into one City class and provide methods for sorting based on name or population - something like:
public class City
{
public const int MinPopulation = 0;
public event EventHandler PopulationChanged;
private string name;
private int population;
public City(string name, int population)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentNullException("name", "Name cannot be null or empty");
if (population < MinPopulation)
throw new ArgumentOutOfRangeException(
"population",
string.Format("Population cannot be less than {0}", MinPopulation));
this.name = name;
this.population = population;
}
public string Name
{
get { return name; }
}
public int Population
{
get { return population; }
set
{
if (value < MinPopulation)
throw new ArgumentOutOfRangeException(
"Population",
string.Format("Population cannot be less than {0}", MinPopulation));
population = value;
OnPopulationChanged(EventArgs.Empty);
}
}
protected virtual void OnPopulationChanged(EventArgs e)
{
EventHandler eh = PopulationChanged;
if (eh != null)
eh(this, e);
}
public static int CompareName(City first, City second)
{
if (object.ReferenceEquals(first, second))
return 0;
if (object.ReferenceEquals(first, null))
return -1;
if (object.ReferenceEquals(second, null))
return 1;
return first.name.CompareTo(second.name);
}
public static int ComparePopulation(City first, City second)
{
if (object.ReferenceEquals(first, second))
return 0;
if (object.ReferenceEquals(first, null))
return -1;
if (object.ReferenceEquals(second, null))
return 1;
return first.population.CompareTo(second.population);
}
}
Now you can keep a collection of cities somewhere (perhaps a List<City> ). You can then use that list as a common DataSource for both of your ListBox es, using the DisplayMember property to choose which property to display. The rough code below needs a button adding buttonSort (with suitable width) for changing the sort.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
public partial class FormMain : Form
{
private const string SortByNameText = "&Sort by Name";
private const string SortByPopulationText = "&Sort by Population";
private List<City> cities;
public FormMain()
{
InitializeComponent();
cities = new List<City>();
BuildCityList();
listBox1.DataSource = cities;
listBox1.DisplayMember = ("Name");
listBox2.DataSource = cities;
listBox2.DisplayMember = ("Population");
buttonSort.Text = "&Sort by Name";
buttonSort.Click += new EventHandler(buttonSort_Click);
}
void buttonSort_Click(object sender, EventArgs e)
{
if (buttonSort.Text == SortByNameText)
{
cities.Sort(new Comparison<City>(City.CompareName));
buttonSort.Text = SortByPopulationText;
}
else
{
cities.Sort(new Comparison<City>(City.ComparePopulation));
buttonSort.Text = SortByNameText;
}
listBox1.DataSource = null;
listBox2.DataSource = null;
listBox1.DataSource = cities;
listBox1.DisplayMember = ("Name");
listBox2.DataSource = cities;
listBox2.DisplayMember = ("Population");
}
private void BuildCityList()
{
cities.Add(new City("City One", 2500));
cities.Add(new City("City Two", 3000));
cities.Add(new City("City Three", 1500));
}
}
|
|
|
|
|
Okey , Thanks , My Means is the same that u wrote ,
Thanks!
|
|
|
|
|
hi
i suspend a thread now i have to resume that thread but with out using sleep method the code is below
using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace threadsuspend
{
public class myclass
{
public void method()
{
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(Thread.CurrentThread.Name + "=" + i);
if (i == 5)
{
Console.WriteLine(Thread.CurrentThread.Name +" is going to suspend");
Thread.CurrentThread.Suspend();
}
Thread.CurrentThread.Resume();
}
}
}
class Program
{
static void Main(string[] args)
{
myclass mc = new myclass();
Thread thr1 = new Thread(new ThreadStart(mc.method));
Thread thr2 = new Thread(new ThreadStart(mc.method));
thr1.Start();
thr2.Start();
thr1.Name = "ntr";
thr2.Name = "anr";
Console.ReadLine();
}
}
}
please help me out
thanks to you
j somasekhar
|
|
|
|
|
Another thread would have to resume a suspended thread. Regardless, you shouldn't be using those methods anyway - they aren't only deprecated, they're marked obsolete.
What are you trying to do? Surely you can use proper synchronization objects?
Mark Salsbery
|
|
|
|
|
Hi,
please keep your code nicely formatted, using PRE[^] tags does that for you.
Probably the only time it makes sense for a thread to suspend itself, is when another thread is going to resume it. A suspended thread cannot resume itself, it is dead and buried for all intents and purposes, i.e.
Thread.CurrentThread.Resume();
is nonsense.
|
|
|
|
|
Hi All
I have a panel to draw some line in there in c#,but when it draw many lines,some of them will be out of sight.i want to see them with scrolling.please help me how can i put scrollBars in a panel object in c#.
thanks every body
|
|
|
|
|
You can get that almost for free, see my example here[^].
|
|
|
|
|
thanks
very nice,really usable
|
|
|
|
|
Luc Pattyn wrote: see my example here[^]. That is an excellent article ! Very useful.
thanks, Bill
Scipio: "That's true, Berganza; and what makes the miracle greater is, that we not only speak, but hold intelligent discourse, as though we had souls capable of reason; whereas we are so far from having it, that the difference between brutes and man consists in this, that man is a rational animal, and the brute is irrational." Cervantes, "Colloquy of Dogs," 1613CE. The two talking dogs, Scipio, Berganza, are hallucinations in the mind of a soldier with plague fever undergoing a "sweating" cure.
|
|
|
|
|
Thanks Bill.
|
|
|
|
|
If you enable scrolling your panel to fix the width and height with make scrollBars="Auto".
bellow the example:
<asp:Panel ID="Panel1" runat="server" Width="100px" Height="100px" ScrollBars="Auto" >
if you want show panel data on scrollbar just put your panel on another panel or div the fix the
</asp:Panel>
|
|
|
|
|
How can the following be achieved in a strongly typed dataset ?
int n = (int)Tables[0].Rows[0]["Field1", DataRowVersion.Original];
Thx
|
|
|
|
|
Hello everbody,
i want to automate a simple login to a website. I've seen a lot of examples and i can automatically fill the form, but i can't submit this form now. I dont't know what i am doing wrong. Can somebody help me.
The HTML Code for the button shows:
<TD><SPAN id=Button><INPUT value="I Concur " type=submit>
</SPAN></TD>
and my C# listing to fill the form is shown bellow:
public void csid_input(EXCHANGEDATA inhale)
{
HTMLDocument html_doc = new HTMLDocument();
html_doc = (HTMLDocument)iexp.Document;
HTMLInputElement logname= (HTMLInputElement)html_doc.all.item("login", 0);
cdsid.value = inhale.cdsid;
HTMLInputElement password = (HTMLInputElement)html_doc.all.item("pw", 0);
password.value = inhale.password;
iexp.Visible = true;
HTMLInputElement btn= (HTMLInputElement)html_doc.all.item("Button");
btn.click();
}
I am getting crazy. I tested several ways to get my problem solved. So can someone please help me.
Thanks.
|
|
|
|
|
I want to use remoting Library for sharing the object. could you please give me the example project and some ducument about remoting Library? .. Thank in advance.....
|
|
|
|
|
See here[^] for an overview, which will lead you to the technical details.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
It's amazing what you can find on the internet if you actually try looking.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
I want to share connection string object and class object to many exe....could you tell me the example document and simple project?
but I use it in local computer..
waiting to hearing from you.
thank in advanced....
|
|
|
|
|