When i try to save my list view lines into a log file using StreamWriter:
sw.WriteLine(listview.Items[0]);
it places to the beginning of the line name of LIstViewItem class, and my log looks like:
System.Windows.Controls.ListViewItem: text text text;
I tried to use:
sw.WriteLine(listView.Items[0].ToString());
and:
sw.WriteLine(listView.Items[0].ToString() as string);
but nothing helpes:(
What am i doing wrong?
The ListView.Items property is an ItemCollection. The items contained are typed as Object. You must cast the item to whatever type you populated the ListView with and then access the appropriate property from there. See: https://msdn.microsoft.com/en-us/library/ms605127(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.items(v=vs.110).aspx for more information.
By default, the console will use the ToString() method as the output. All of the methods you have tried will be using the ToString() method.
Perhaps you are looking for the Text property.
sw.WriteLine(((ListViewItem)listview.Items[0]).Text);
Related
I have a series of mutator methods in another class, each of which is linked to a textBox.
(ClassA). Now, I am using an object of ClassA (myClassAObject.setFirstName(param here), to set the text, so the contents of that parameter will display in that textbox.
The content of that mutator method is as follows:
public void setFirstName (string newFirstName)
{
txtBoxFirstName.Text = newFirstName;
}
I know that the mutator works correctly, because otherwise I'd not see the first name of the patient, I'd see "null", or in the case of a println, a blank space.
The question is below this line
The mutator method, for some reason is not causing the textBox to display the text it is given in a parameter, when I access it from the other class. The textBox remains blank. How can I get it to work correctly? I have used MessageBoxes to check if there is a value for the textBox to display, and yes, the contents of the variable appear onscreen.
Here is how the parameter is passed from the second class, to the class in which the textBox resides:
myClassAObject.setFirstName(firstName);
The code above is example code, but it illustrates what I am doing.
Below is the actual code.
Firstly, from the class where the textBox resides.
Secondly, where the new parameter is being passed from.
public void setPatientFirstName(string newPatientFirstName)
{
txtBoxPatientFirstName.Text = newPatientFirstName;
}
Second part of code:
patientRecordClassOverseer.setPatientFirstName(patReaderFirstName);
I am using a "Reader" (OleDB) to read the data from the database. It is working, as I have MessageBoxes setup for debugging purposes.
I have an objectlistview which displays filename and its path in a column. I would like to run a function on selected items. Is there any way I can grab the value of the filename and loop through every file that is selected on the objectlistview? The column aspect name is Filename.
My function is as follows: sampleFunction(string inputFile, string outputFile);
so far I've tried this, but couldn't work, I know I'm missing reference to the column itself but I don;t know how to add it:
for(var i=0; i<=objectListView1.SelectedObjects.Count; i++)
{
encClass.sampleFunction(objectListView1.SelectedObjects[i], "output here");
}
edit:
I also tried append ToString() method to objectListView1.SelectedObjects[i].ToString(). It shows no error but the function couldn't run perfectly because I have 3 columns and I only wanted to use the first column's value in the function as the inputFile value.
I seems that you do not understand the concept of the OLV correctly. I suggest you read the tutorial (again).
The OLV allows you to work with the underlying model objects directly, so just cast the selected object(s) to the original type and access its properties. You can even use a TypedObjectListView<>, which simplifies access to the models.
Judging from you post, what you want to do is probably something like this:
foreach (var selectedObject in objectListView1.SelectedObjects) {
encClass.sampleFunction(((MyType)selectedObject).Filename, "output here");
}
Obviously, replace "MyType" with your model object type.
I got a problem with ListView in System.Windows.Forms, that i can't handle it on myself, begging for help or a hint where do I do wrong?
Description:
- I have a class - name it cListViewItem ('c' from custom), that inherits from standard ListViewItem, but stores my own data handling class. Now, after adding cListViewItem to a ListView class using ListView.items.Add( ) i don't seem to have any control over the item's name.
- fragments of my source (lil changed for the purpose of this post)
using System.Windows.Forms;
cListViewItem:ListViewItem
{
// gives a basic idea
public cListViewItem( myclass DataStorage )
{
// constructor only stores DataStorage to a local variable for further use;
this._storage = DataStorage;
}
protected myclass _storage;
// and there goes the fun:
// I thought that ListView class uses .Text property when drawing items, but that is not truth
// my idea was to 'cheat' a little with:
new public String Text
{
get
{
// overriding a getter should be enough i thought, but i was wrong
return( some string value from DataStorage class passed via constructor );
// setter is not rly needed here, because data comes from this._storage class;
// in later stages i've even added this line, to be informed whenever it's called ofc before return( ); otherwise VisualStudio would not compile
MessageBox.Show( "Calling item's .Text property" );
// guess what? this message box NEVER shows up;
}
}
}
I see that's important to use .Text setter, but constructor is the last moment i can do it, right after creation cListViewItem is being added to ListView Items property and displayed, so there's no place to call .Text = "" again.
My piece of code only works when I set all things in cListViewItem 's constructor like:
public cListViewItem( myclass DataStorage )
{
this._storage = DataStorage;
this.Text = DataStorage.String1;
// and if I add subitems here, I will see em when the ListView.View property be changed to View.Details
}
So am I blind or what? when I use cListViewItem.Text = "string" I will see 'string'
in the ListView but when I just override .Text getter i can't see the items :(
ListView class gives the flexibility of showing items the way I need. I wanted to create a class that will bind my custom data storage class with a ListView class. In the next stage of my application I want to bind a form for selected item in a ListView, that will allow me change item's (my custom class) values. That's why i wanted to make each ListViewItems item remembering corresponding custom data storage class.
Names shown in ListView will never be uniqe, so multiple same names all allowed, but items will differ by a id value (database-wise);
I just can't figure out why using ListViewItem.Text setter does the job, altho ListView class does not use ListViewItem.Text getter for displaying items (my MessageBox never pops up)??
Pls help.
The main problem here is that you are hiding the property with the new keyword. The original property is not virtual ("overwritable") so it is NOT overwritten but shadowed.
Read here for more information.
If I understand correctly, then the following points might be helpful.
For storing custom data you don't actually need to derive from the ListViewItem class, instead you can use an instance of a ListViewItem and set the Tag property to any object, this can be your DataStorage class. If you do this, then after you've constructed the ListViewItem set the Text of it
DataStorage storage = GetDataStorage();
ListViewItem item = new ListViewItem(storage.Name);
item.Tag = storage;
If you are going to inherit the ListViewItem then set the value in the constructor
public cListViewItem( myclass DataStorage )
{
// constructor only stores DataStorage to a local variable for further use;
this._storage = DataStorage;
this.Text = this._storage.Name;
}
Property and Method hiding gets a little confusing for myself at least, I can't quite remember the rules, but ultimately the call to Text that is done automatically is not going to be calling your version...
Using log4net, I have saved custom information in the following way
ThreadContext.Properties["context"] = info;
How can I output only this specific property, using the PatternLayout?
If i use %property it prints the whole collection like this:
{log4net:HostName=wrkst16, context=[my stuff here]}
I only want the content of "context" itself though. I tried %properties['context'], but it just appends the ['context'] part:
{log4net:HostName=wrkst16, context=[my stuff here]}['context']
Any way to get only a specific item of the collection?
This works:
%property{context}
I have a RichTextBox created programmatically with the following code:
RichTextBox RT = new RichTextBox();
RT.Name = "asdf";
RT.Text = "blah";
TableLayoutPanel.Controls.Add(RT,0,0);
Now let's say I want to modify the text of RT, and it's name is "asdf", Visual Studio won't allow me to write asdf.Text = "haha" because asdf doesn't exist yet.
How can I grab "asdf" specifically and set its text? Because this RichTextBox is in a specific cell, can I grab it based on its cell coordinates?
You should be able to get a reference to it via the TableLayoutPanel.Controls property, which returns a TableLayoutControlCollection. That class provides two ways to locate a control by name: the Item property and the Find method. The Item property returns a control with the specified name, whereas the Find method returns a collection of controls. In both cases you would need to cast from a Control to a RichTextBox.
var rt = (RichTextBox)myTableLayoutPanel.Controls.Item["asdf"];
// or
var rts = myTableLayoutPanel.Controls.Find("asdf", false);
foreach (var rt in rts)
// (RichTextBox)rt ...
EDIT: be sure to check that the result is not null before using it in case the control is not found.
Well... you did instantiate the RichTextBox and have a reference that you can use; it's called "RT" in your example.
Now, likely you've done this in a method so it was locally scoped and is no longer available when you want it. So you save that reference somehow by assigning it to some member you can access. If you have a lot of them and want to differentiate by name somehow, you might stick it into a Dictionary<string, RichTextBox>, for example. Or you could put it in some static variable; there are numerous options, each with their own pros and cons.
The one thing you probably don't want to do is walk the control tree looking for the control with the name you specified. But you could also do that, if you really wanted to.