|
I have a list box that I am populating from a file. My file has a reference ID and I want to display text in the list box and associate it with the ID. This is easy when I display the ID in the list box but I want to hide it without just change the text color to match the text box. Any help would be great.
|
|
|
|
|
Your question is not clear. Please explain it more.
|
|
|
|
|
It's not very clear what you want to do.
If you want to add all items but have no text for items that match a particular ID then you have two options. Both require overriding the ToString method of the objects you are adding to return string.Empty for the ones you want empty.
You can then either add the object itself to the listbox (handy cause you can cast back to the object itself later)
or
When you add the items, call the ToString method and use the ID property to set the Tag.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Sorry guys for not being clear.
Here is what I am doing. In the list box I want to show say a list of first names. Each first name has an ID that goes with it. Say John is number 18. If the user clicks on John I want to be able to look up number 18 associated with that John. If I add John to the list box origianlly like this (listBox1.Items.Add(ID[i] + " " + Name[i]);) then getting the 18 is not a problem. But I want to only show to the user John not the ID on each record. Like I said I could change the font color on the ID and not show it that way but I was wondering if there might be another way to do it.
|
|
|
|
|
The list box holds a collection of objects not strings so you can add anything to it. The text it displays is controlled by the ToString method or in it's absence, the base.ToString method. At the moment you're adding a string so the string.ToString method is being called.
Instead, if it's not already - make each person an instance of your own class. In the class, override the ToString method to return just the text you want and add each instance to the list box. To retrieve the ID, cast the listbox item to your class and you will then be able to access that instance's properties including ID.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Or even cooler, write a new listbox control that contains a new property called "Hidden" that you can set from the form.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hello
I am using Event in my program. Below i am giving the Code thats being used, but somehow this is not working.
// Class from triggering Event
Public class Manager
{
public static Manager myManager;
public delegate TestEventDelegate (object sender, int i);
public event TestEventDelegate TestEvent;
public Manager Initialize() // This initilaize is called
//from another class which gets 1st called
{
if(myManager == null)
myManager = new Manager();
else
myManager;
}
public void RaiseEvent(object sender, int i)
{
TestEventDelegate temp = TestEvent;
if(temp != null)
temp(this, i);
}
// Now inside a function from where i am triggering the Event
public void InitializeEvent()
{
TestEvent = new TestEventDelegate(RaiseEvent);
TestEvent(myManager, 10);
}
}
// Now in another Class
public class Reciever
{
public Reciever() // Constructor
{
Subscribe(Manager.Initialize());
}
public void RecieveEvent( object sender, int i)
{
MessageBox.Show("Event Success!!!");
}
public Subscribe(Manager mgr)
{
mgr.TestEvent += new Manager.TestEventDelegate(RecieveEvent);
}
}
So this is the code i am using. It compiles fine. But doesn't work.
Any idea?
The "Initialize()" in the triggering class makes that class Singleton.
|
|
|
|
|
I can't find where you are calling InitializeEvent() method?
Donosguy wrote: mgr.TestEvent += new Manager.TestEventDelegate(RecieveEvent);
Donosguy wrote: TestEvent = new TestEventDelegate(RaiseEvent);
You have hooked TestEvent in the Subscribe method. You are hooking it again in InitializeEvent() which will overwrite the previous one. You can do something like
Public class Manager
{
public static Manager myManager;
public delegate TestEventDelegate (object sender, int i);
public event TestEventDelegate TestEvent;
public static Manager Initialize() {
if(myManager == null)
myManager = new Manager();
else
myManager;
}
public void InitializeEvent()
{
if(TestEvent != null)
TestEvent(this, 10);
}
}
public class Reciever
{
public Reciever()
{
Manager manager = Manager.Initialize();
Subscribe(manager);
manager.InitializeEvent();
}
public void RecieveEvent( object sender, int i)
{
MessageBox.Show("Event Success!!!");
}
public Subscribe(Manager mgr)
{
mgr.TestEvent += new Manager.TestEventDelegate(RecieveEvent);
}
}
|
|
|
|
|
Using C#, how do I call the queue method printQueue to print out to rtbox a list of the (Batch No, weight) pairs stored in a data queue?
|
|
|
|
|
I think you might be mixing objects here.
There is the System.PrintQueue class, which represents a queue of printer jobs queued up by Windows to submit to a printer; and there are Queue objects (System.Collections.Queue), which you use to hold collections of data.
There is no "print" method for a Queue object.
|
|
|
|
|
amm actually refering to queue method and not "print" method.
|
|
|
|
|
It's still not clear what it is you want to do.
1. do you have data sitting in a System.Queue object?
2. would you like to Dequeue that queue, and put a copy of the text/values there in a text box?
|
|
|
|
|
Thats right. I have this data in pairs enqueued already and now I would like to print out to the rich t.box a list of the pairs stored in the data queue.
|
|
|
|
|
I have a library class which does a bunch of work and eventually returns to the caller a struct (call it UserInfo ). That struct contains other structs. All of the members of all of the structs are public readonly, as I don't want the caller to able to change any of the values. However, I want all of the values visible.
So I'm limited to using constructors to build all the nested structs. However, this isn't practical in the flow of my class - I need to be able to create a new UserInfo and update the members of its nested structs as it bounces around inside my class.
I really only want to "seal" the struct right before I pass it to the caller.
What's the best design pattern for this?
Should I create a mirrored "TempUserInfo" struct with all public members and fill that out instead, and at the last minute copy those values into the final UserInfo struct?
|
|
|
|
|
Use properties with public getters and internal setters. Also add a private boolean to indicate locked.
|
|
|
|
|
I'm still not following.
I have fields who's value I want the caller to be able to see. I need to change them internally, before I "finalize" the values, but I don't want the caller to be able to change them.
Are you suggesting that I implement properties, and in the "Set" property, I check my boolean to see if the struct has been "finished"?
If so, what access properties do I put on this boolean, so that I can change it to "locked" but the caller cannot change it to "unlocked"?
I'm assuming that I'm misunderstanding your suggestion.
|
|
|
|
|
Thanks, I believe you lead me down the right path.
I've implemented all the struct fields as auto-properties:
public DateTime TimeStamp { get; internal set; }
The internal setter allows my class to change fields at will, but prevents the caller from doing so.
Bravo!
|
|
|
|
|
Right, that should do it.
|
|
|
|
|
Doh.
Apparently you can't set properties in nested structs... the following is not allowed:
my Class
{
myStructInner
{
public int myInt { get; set; }
}
myStructOuter
{
public myStructInner structProp { get; set; }
}
myMethod()
{
myStructOuter thingy = new myStructOuter();
myStructOuter.myStructInner.myInt = 5;
}
}
Compiler claims that you cannot "change the return value from myStructOuter.myStructInner.myInt", as it's not a "variable".
|
|
|
|
|
Because it is a struct, a value type not a reference type. When you write someObject.SomeStruct , you are getting a new copy of the somestruct instance and modifying something on that copy won't reflect on the original one.
To avoid this issue, you should create a new struct instance and assign to the property.
|
|
|
|
|
yeah, that kind of defeats my purpose. I need more resolution than that. I need to be able to hand the top level struct around and let any code in my class modify any individual field therein, no matter how nested.
So I'm left with either
(a) flattening out the entire struct, which I am loathe to do or
(b) writing Set() methods at the top level which are knowledgeable about all of the fields of the nested structs.
I'm leaning towards (b). But I hate it.
|
|
|
|
|
Are there any specific reasons for a choosing struct ? It's hard to make it right. Use classes instead.
|
|
|
|
|
I want to test a "name" field on my form and am using regualr expression. My regular expression doesn't pass all the test cases shown below can you please help me in it. The maximum name field can have is 100 characters.
if (Regex.Match(this.textBox1.Text, "^[a-zA-Z0-9_ ]+(')?([ a-z])?$").Success)
{
MessageBox.Show("valid");
}
else
{
MessageBox.Show("not valid");
}
test cases
code project -->valid
codeproject -->valid
codeproject's -->valid
codeproject's program --->valid
codeproject's and codeproject's -->valid
codeproject's and code project's --->valid
code project's and code project's -->valid
|
|
|
|
|
What is it that you are trying to do, really? This regular expression should work with all your test cases, but I'm pretty sure that's not what you want to do:
^code ?project('s( program| and code ?project's)?)?$
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi
I need to build a program which do the following:
1. Convert the sound output(amplitude) from the sound card in to floating point numbers ranging from 0.0f to 1.0f.
2. Additional requirements:
a) The audio can be split up in to 4 different frequency ranges:
ie one floating point number for the amplitude of frequency range 0kHz to 4KHz for voice
one floating point number for 4Khz to 8KHz
one floating point number for 8Khz to 15Khz
and last one for 15 to 24Khz
Basically, what I would need is a function which can recognize the frequency and amplitude, like ones frequently used for creating visualizations for media players.
If anyone have such a readymade code, please send to me at deepakpaul_john@infosys.com and also at deepakpaul13@yahoo.com or post in the forum.
Please note that I am very new to C# and be as elaborate as possible while giving your suggestions.
Thank you
|
|
|
|