|
You've got it almost right. The property used for storing info about items is called Tag . For other purposes (when addressing items in the ListView if you don't know the index) you might like Name .
Also - the leftmost column is somewhat main and contains SubItems that specify text displayed in other comlums. Therefore set the Text and Tag property of the leftmost column and populate its subitems (the way you do) to specify the text of other coumns.
The code:
foreach (Person person in personList)
{
ListViewItem lvi = new ListViewItem(person.Name);
lvi.Tag = person.Id;
lvi.SubItems.Add(person.Age.ToString());
listView1.Items.Add(lvi);
}
Hope it helps, H.
|
|
|
|
|
The previous answer was good!
If you're going to need this functionality alot then it can be useful to create a ToListViewItem method in the Person class.
Also, if a reference to the person object itself would be useful (sometimes it can be easier to deal with the object rather than one of its properties to save multiple lookups etc) then you can put that into the Tag instead - just cast the Tag back to Person when you need it.
public ListViewItem ToListViewItem()
{
ListViewItem result = new ListViewItem(Name);
result.Tag = this;
result.SubItems.Add(Age.ToString());
return result;
} Add like this:
foreach (Person person in personList)
{
listView1.Items.Add(person.ToListViewItem());
} To get the object:
Person selectedPerson = (Person)listView.Items[itemIndex].Tag;
Nothing wrong with the previous answer, just throwing a couple more ideas out there
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
The two ways totally helped me understanding how to work with ListView, thank you guys!!
Though I have a doubt about the two replies,
you both told me to put person.Name in the first column.. but, if I need to put a checkbox column?
And Dave, respect to your answer,
I really find it a good idea, in fact i'm going to do it that way, but..
I've been told not to produce cycles between packages, and I have the ClassLibrary package which contains the Persons, etc, etc. and the WindowsFormsApp which would have the ListView and etc. Is it ok to return a ListView from the ClassLibrary?
Thanks again
|
|
|
|
|
If you want name in a different column then add it as a sub item.
Your other point is a good question. It depends on just how fussy you want/need to be.
Quake2Player wrote: Is it ok to return a ListView
Making the Person class reliant on a list view would be a bad idea:
public void AddToListView(ListView listView)
{
ListViewItem listViewItem = new ListViewItem(Name);
listView.Items.Add(listViewItem);
} but in my opinion, returning a ListViewItem from the class is OK as it doesn't rely on any existing object instance to be able to perform the method. I would draw the line at a ListViewItem (or a List<ListViewItem> or ListViewItem[] for a collection), returning an entire ListView would be taking it too far.
Not everyone would agree that returning a ListViewItem is OK - if this is for work/college, ask your boss/tutor for their opinion. The way you were doing it is good, but the reusability of having a method like that in the class (especially if it's going to be used a lot) can be advantageous.
Another minor thing that's often worth doing (along the same lines) is to override the ToString method e.g.
public override string ToString()
{
return string.Format("Name: {0}, Age: {1}", Name, Age);
} This is a very standard thing to do and not that disimilar to what I suggested.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi ppl
In my sql server 2005 database I have a column named "VistDate" in a table. Data type is DateTime and formate is short Date. when I try to get a date from that column and take it in a textbox it appears, if the date is 10/10/2009, it works fine. But if the date is
like 6/7/2009, it splits. Reason is I want to get indivisual formate: like
VisitorDate = row["VistDate"].ToString();
txtVisDay.Text = VisitorDate.ToString().Substring(0, 2);
txtVisMon.Text = VisitorDate.ToString().Substring(3, 2);
txtVisYr.Text = VisitorDate.ToString().Substring(6, 4);
Result:
6/
7/
09...
result is spliting.
But if the date is 10/10/2009 then it appears perfect.
Ques is converting 6/7/2009 to 06/07/2009?
whats the solution
Thanks in advance
|
|
|
|
|
for that sort of thing you would really have to tokenise it - based on the '/'
The Substring will work fine if each data part always has exactly the same amount of characters, eg dates/months < 10 are padded with a leading '0'
When I read dates from Oracle (so Im not sure about Sql Server), I explicitly tell Oracle in the sql select statement to return the date in a particular format, which includes padding etc
oh, please dont use 'text speak' like 'ppl', 'Ques', and try to make more of an effort with spelling
'g'
|
|
|
|
|
This is entirely the wrong answer, the DateTime object obtained using row["VisitDate"] has methods for obtaining the Day, Month and Year of that instance.
Your database should not be responsible for formatting anything, least of all Dates.
|
|
|
|
|
shows you how much I know about Sql server then - I didnt know it returned a DateTime object, which yes, should have methods for obtaining parts of the date.
J4amieC wrote: Your database should not be responsible for formatting anything, least of all Dates.
So doing a sql query in Oracle and outputting a date column using to_char(column, 'output format') is wrong ? ok .. whatever
'g'
|
|
|
|
|
If its stored in Sql Server as datetime or smalldatetime datatype, then the result of the datareader call will be a .NET DateTime instance.
DateTime visitDate = (DateTime)row["VisitDate"];
now that instance has properties representing the Day, Month and Year (as an integer, so remember to .ToString() it when putting it in a textbox.
txtVisDay.Text = visitDate.Day.ToString();
I'll leave the other 2 to your imagination
|
|
|
|
|
Many Many thanks J4amieC.
Solved. Just a lil edit.
DateTime visitDate = (DateTime)row["VistDate"];
txtVisDay.Text = visitDate.ToString("dd");
txtVisMon.Text = visitDate.ToString("MM");
txtVisYr.Text = visitDate.ToString("yyyy");
Thanks a gain
|
|
|
|
|
i want to implement connect four game
one player with computer
-->how can i make play computer?
-->how can i check if the layer or computer won?
|
|
|
|
|
this doesnt go into great details about what you need, but its a start :-
http://www.codeproject.com/KB/mobile/Connect4.aspx Connect4 using MIN-MAX algorithm[^]
(and its only one way of doing it)
'g'
|
|
|
|
|
Garth J Lancaster wrote: and its only one way of doing it)
I strongly doubt that.
|
|
|
|
|
I dont get you - I did not say 'it was the only way' (which would definately make me wrong)
for solving 'simple' issues, there's usually
a) brute force
b) anything smarter than brute force, a class of algorithms - in this case AI MinMax
are you implying that it cant be solved in some brute force manner ?
|
|
|
|
|
My bad, I misunderstood you. I thought you actually said that minimax was the only way of dealing with connect-four, which obviously isn't.
That being said - what exactly do you mean by brute force? How more brute can it get than minimax?
|
|
|
|
|
Member 1033907 wrote: How more brute can it get than minimax?
A baseball bat with a nail in it? The none holdy end obviously.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
More precisely, i need the following embeddable video player displayable in an HTML window. I managed to make an HTML file but I would like to be able to use it independent of my web browser. Basically make my own mini browser that can only use either the file with the embedded code, or even better, utilize the code to display the video viewer.
The embedded code is the following (I added the quotes to be sure i wasn't creating running code in the message):
"""""""
|
|
|
|
|
You don't need the quote marks, you can't post anything dangerous in your messages. We also have code tags for these kinds of things, a like so:
<code></code> which makes code look like this
Or perhaps what you would want to be using <pre></pre>
which does this
But moving forwards, you can have a web browser control on your form that you can display web pages in. Its basically just an instance of IE running in your window. You have some control over it, but not alot. Check it out here[^].
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Yeah ive been tinkering with it. But all i can do is define a static location for the file on my machine. The other thing i could do is host it online, and be able to manually set a path. Is there a way to incorporate the html file into the executable, and have the webbrowser object automatically open that file? This way I can use the app on any computer and it will load the video player.
Update!!!!
This is a pic of what i managed to make. The trick is basically a basic webbrowser with resizing abilities stripped away and some trickery with the window boundaries. So ultimately my problem is this. This program relies on the fact that an HTML file exists is specific location on my computer. I would like to make this app portable by including the html file inside the executable. I dont have the means to host the html file online.
modified on Friday, May 1, 2009 10:11 PM
|
|
|
|
|
You can add the html file as an embedded resource, or add it to a resource file (like the ones Visual Studio generates, they store all sorts of strings pictures and other stuff).
To make it an embedded resource, just add the file to your solution and in its properties to it to an embedded resource. To then get the the file back out you should be able to do something like:
Stream myHTMLFile = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyFile.html");
You could then pass that stream into a StreamReader and get a string representing your HTML which you can set yourself. Or save it to disk somewhere as a temporary file and point to that.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
I wrote some COM+ Applications Using Microsoft .NET Enterprise Services and its work fine.
But i don't know how to debug it.
I try to use "attached to process" and it is not stop on the break point ( the break point is valid - the modules are up ).
Someone know how to debug this thing ? and why its not stopping on the break point ? ? ?
Thanks ...
|
|
|
|
|
how would u enable and disable ur nic card in C#. i searched a bit and found about WMI but then msdn showed that wmi doesn't have any function like disableNIC or enable... so if anybody has a good link... or can provide help, i will really appreciate it...
thanx
|
|
|
|
|
Seems your not the first to ask this question try[^]
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
It would also be much appreciated if you could fully spell your words. And perhaps less of the ellipses, but maybe that's just me.
There may not be a specific function to disable a NIC (there could be more than one you know), but I bet that around somewhere is a function that disables a hardware device, and that just nearby is a function you can use to find hardware devices.
Of course, that google search which Mycroft Holmes linked to probably has all the information you need. You'll just need to do a little reading.
My current favourite word is: Delicious!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
I have managed to put a listbox in my listview and have gotten the box to appear but it is empty. I want to click on the Edit link and based on the datakeyname value pass the parameter to the objectdatasource to fill the listbox. I'm trying to use IEnumerable and am not getting it. Can someone help with an understandable example. Or another way to do it.
<asp:listbox runat="server" selectionmode="Multiple" id="lbNoAuthorizations"
="" datatextfield="AuthorizationDefinition " datavaluefield="AuthorizationID" width="400px" datasourceid="odsAuthorizations">;
<;/asp:ListBox>
<asp:objectdatasource
="" id="odsAuthorizations" runat="server" selectmethod="GetAuthorizations" typename="Authorizations">;
<selectparameters>;
<;asp:Parameter Name="PersonnelNumber" />;
<selectparameters>;
;
|
|
|
|