|
Hello world!
I have a class and I wish to get a reference to one of its member variables at runtime.
class foo
{
public string bar1;
public string bar2;
..
}
I have called foo.GetFields() and I am the proud owner of an array of FieldInfo structures giving me information about all of the type's member variables. I can see my strings (checkboxes/bears) in the list.
My question is: How can I take this FieldInfo data, and get a reference to the desired variable and hence manipulate it??
Many thanks in advance,
Charlie (C:=
|
|
|
|
|
You've already got it: the FieldInfo . If you want to get or set a field's value, use FieldInfo.GetValue and FieldInfo.SetValue . If the fields are instance fields, you must pass the object whos fields you want to retreive.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Where does foo and bar come from, as it seems to be standard in using in examples?
Does it come from FUBAR?
Thanks
|
|
|
|
|
yes... now, what's a fubar?
|
|
|
|
|
Ok, here's the senario:
I have two projects in my solution, one of which (project A) has a custom control, and the second (Project B) has a form that uses the custom control. Frequently, if I have opened the project B form in the designer and then re-compile, Project A's dll cannot be written to the output directory because the designer is using it (at least that's what I presume is the reason it can't be written). Closing the designer window in Visual Studio doesn't help. Even closing the *solution* and re-opening doesn't help!! I have to close Visual Studio completely and re-launch to be able to compile my solution again.
How can I avoid this problem, outside of not having these two projects in the same solution (I'm working in both at the same time).
Thanks!
~Steve
|
|
|
|
|
Steve Schaneville wrote:
How can I avoid this problem, outside of not having these two projects in the same solution (I'm working in both at the same time).
You can't. It's a bug in VS.NET 2002 and 2003. Here's to hoping it's been fixed in VS.NET 2005
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
oh. good news.
Thanks!!
~Steve
|
|
|
|
|
I have an instance of class A that publishes an event and several instances of class B that subscribe to it. How can I get a B instance to UNsubscribe from the event?
|
|
|
|
|
It's just like subscribing to the event, except that you use -= instead of +=
intanceOfA.FireTorpedoes -= new EventHandler(intanceOfA_FireTorpedoes);
Charlie
if(!curlies){ return; }
|
|
|
|
|
I have a datatable and performaing select method on that.
I have to pass array of values to the select method,
is there any way how i can do that?
Here goes the code
string[] emp;
dtemp.select()- How should i pass array of strings inside the function.
|
|
|
|
|
You can't. Instead, split your array and pass each value as part of the WHERE clause:
private DataRow[] SelectRows(string field, string[] values)
{
if (field == null || values == null)
throw new ArgumentNullException(field == null ? "field" : "values");
if (values.Length < 1) throw new ArgumentException();
StringBuilder sb = new StringBuilder();
for (int i=0; i<values.Length; i++)
{
sb.AppendFormat("{0} = {1}", field, value);
if (i < (values.Length - 1))
sb.Append(" AND ");
return dataTable1.Select(sb.ToString());
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am creating a windows app in C# with a ListView control. It has three columns which are being filled with a ListViewItem (One .Text and two .SubItems being added to generate)
My problem is I can not figure out how to grab the text entered in one of the columns when someone clicks on one of the items. FullRowSelect is set to True and I'd like to keep it that way if possible. The first row is a list of FileNames, second the size and the third a mod date. On click, I want to simply open the file that was selected.
I've also noticed, while trying the SelectedItems/SelectedIndices options that they tend to be called twice on change. The first is a null value (for an Int??) and the second is the correct option.
Any help would be appreciated, I'm still going through articles on the Web regarding this control, but everything is oriented on sorting - not how to execute some code based on the selection. Thanks!
|
|
|
|
|
Get the index of the ListViewItem and get the sub item:
private void listView1_SelectedIndexChanged(object sender,
EventArgs e)
{
int index = listView1.SelectedIndex;
if (index >= 0)
{
ListViewItem item = listView1.Items[index];
string value = item.SubItems[1];
MessageBox.Show(value);
}
} If you actually want the column that the user clicked on, you need to perform a hit text:
private int GetColumnIndex()
{
Point p = listView1.PointToClient(
new Point(MousePosition.X, MousePosition.Y));
int width = 0;
foreach (ColumnHeader header in listView1.Columns)
{
width += header.Width;
if (p.X < width) return header.Index;
}
return -1;
} You could call this method to get the column index (be mindful that it could be -1) and use that as the index into ListViewItem.SubItems .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I had to modify it a bit:
int index = listView1.FocusedItem.Index;
if (index >= 0)
{
ListViewItem item = listView1.Items[index];
string value = item.SubItems[0].Text;
MessageBox.Show(value);
}
(There is no listView1.SelectedIndex)
Unfortunately I still have the same problem, the method fires twice. Once if its the first time I selected something, but on subsequent selections - it will show what the currently selected item is and then the new one. Any ideas why?
|
|
|
|
|
Adam Hable wrote:
(There is no listView1.SelectedIndex)
Oops. Sorry about that. I sometimes seem to miss something like that when throwing out examples.
Be sure that in your event handler (which I assume is the method you're talking about) that you don't set the selected index mistakenly using an assignment operator (=) instead of an equals operator (==). It could even by by mistake.
I do work a lot with ListView controls and have even done a lot to extend them with functionality (overriding WndProc ) provided by the List-View common control which ListView enscapsulates. I've never had the problem you're having.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Obviously not a problem, I just wanted to make sure I didn't make a mistake by trying something else. Here is the entire event:
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
int index = listView1.FocusedItem.Index;
if (index >= 0)
{
ListViewItem item = listView1.Items[index];
string value = item.SubItems[0].Text;
MessageBox.Show(value);
}
}
I created a new project and left defaults (So that's why the 'listView1' naming) -- but it fires twice on the MessageBox.Show I agree - it shouldn't, so that's why I'm so confused!
I did just uninstall VS.Net 2k2 Pro and install a fresh copy of VS.Net 2k3 Ent. Architect, I doubt that has anything to do with this error but...could it?
|
|
|
|
|
Adam Hable wrote:
I did just uninstall VS.Net 2k2 Pro and install a fresh copy of VS.Net 2k3 Ent. Architect, I doubt that has anything to do with this error but...could it?
VS.NET has nothing to do with it. It's the version of the .NET Framework that you have installed that matters, and that - even the SDK - is independent of VS.NET. Obviously 2002 will install .NET 1.0 and 2003 will install .NET 1.1.
Also, just a little CPU-time saving tip: ListViewItem.Text == ListViewItem.SubItems[0].Text
As for the problem, you've got me there. I would suggest debugging your code and examining the protected Events property of the ListView (inheritted from Component , so you may have to walk up the derivation stack in the debugger) and see if somehow there are two handlers. I don't even think that's possible, though, since even a new delegate to the same method is "referentially" equal to an existing delegate pointing to the method.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for the advice, but I don't have time to play with this. So, a bit of playing is necessary. I set an int that is checked everytime the listView is clicked, if it doesn't match the focuseditem.index, it fires off the code you listed first. Otherwise it does nothing.
Written well? No.
Works? Yes.
Thanks again!
|
|
|
|
|
Sorry I can't be of more help. I'm not sure why you're getting this problem. You might double check to make sure that you have any .NET service packs (only SP2 for .NET 1.0 available now until XP SP2 is released - an SP for both 1.0 and 1.1 will be required) and your system is patched. Probably won't help, but I don't know what else to tell you. In such a case - as painful as it may be - hacks are necessary (until the real problem is found).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Add an event handler to SubItemClicked. Access an item's text using this:
this.listviewObject.Items[itemIndex].SubItems[subItemIndex].Text
Kuphryn
|
|
|
|
|
There is no SubItemClicked event for either the ListView or any other class in the .NET Framework Class Library. Perhaps for a third-party control, but not for System.Windows.Forms.ListView, System.Windows.Forms .
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello. I need to show a message in a Windows Forms application. I was thinking in a ListBox.
I did some tests and I know how to show the message in a Console application. Just doint:
public class Message: MarshalByRefObject
{
public void Show(string s)
{
Console.WriteLine(s);
}
}
That works because it's easy to write in the standard output. The doubt is how to write in a component.
Can you help me?
Regards,
Diego F.
|
|
|
|
|
What exactly are you trying to do and where do you want this message displayed? If your remoting object is hosted by a console application with a loop to keep it from terminating, you can use the Console class at any time anywhere in your code, but it will be output on the remote host (i.e., server).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
What I need is a Windows Application that is continuously running and receiving messages. I want to show these messages in a ListBox. My question is how can I write the remote object to show the messages in a ListBox.
I told about the Console because I tried it first with a Console Application, but I'd rather prefer using a Windows app.
Regards,
Diego F.
|
|
|
|