|
That sounds like an designer error. Go back into your resources and make a change, then save it. It should generate a get accessor.
Note that you won't be able to store it in a constant field, however. You could store it in a readonly field if you need to:
private readonly string MY_NAME = Resources.MyResourceName;
Also note that MY_NAME isn't following standard .NET naming conventions. Run the free Microsoft tool FxCop on your assembly, it will tell you what you're doing wrong.
|
|
|
|
|
Thanks.
Actually I need to use the string in a switch statement as one of the case.
<br />
class MyClass<br />
{<br />
private const string MY_NAME = Resources.MyResourceName;
void Myfunction(string str)<br />
{<br />
switch(str)<br />
{<br />
case MY_NAME:
break;<br />
}<br />
}<br />
}<br />
About the naming convention, this code is just sample code.
|
|
|
|
|
You can switch on regular strings:
string s = "foo";
switch (s)
{
case "foo":
MessageBox.Show("it was foo");
break;
}
p.s. To preserve formatting, use <pre> tags around your code when posting on this forum.
|
|
|
|
|
I think you are not getting the point.
I want to eliminate hard coded strings hence I have stored all the hard coded strings like "foo" in Application Resources. Now I want to use these resource strings in my application. I have no problem using the resource string except in switch statements. where the case requires a constant value. This is the place where I am finding hard to replace
string s = "foo";
switch (s)
{
case "foo":
MessageBox.Show("it was foo");
break;
}
|
|
|
|
|
Ah, gotcha. I don't think that will work, because the string in the resources isn't constant. Instead of doing a switch, you should just use if/else, using string.Equals(first, second, comparisonOptionsAndCulture)
|
|
|
|
|
Can anyone tell me how to retrieve the data from my table and display it all in one continoues form like that form of MS Access where I create just one control bound to each field, and this list of contrls repeates itself continouesly till all the records are displayed.
I can do the same task by either DataGrid control or even ListBox
But I am familier with the method of MS Access Forms and I think C# will provide similar forms since both Access and C# are of Microsoft
Mohamed Gouda
Egypt
|
|
|
|
|
I'm trying to make a directory watcher application that uses the FileSystemWatcher.
The basic program requirements are:
* The program should watch a directory for list of files given by user (setup values)
* The file may be new or old and may exist in the directory (when application not active)or be created when the application is active.
* When the program begins it should move the existing files.
* If say ten zip files and ten doc files are copied to the directory then it should move the zip files to the directory the user specified for zip files (setup value for particular) file extension and the doc files to the directory the user specified for doc files.
* The program should have a UI that has a listview that shows the activities done.
I'm using the FileSystemWatcher and a main worker function that does all the finding and moving.I need to tell the list box the events.
Any suggestion on how this should be done in the threading way (best practice)
Some input to go about it based on the above ideas appreciated
Thanks in advance.
mejax
|
|
|
|
|
Look up the BackgroundWorker class: it has a DoWork event that fires on a background thread. Do your work there. It has a ProgressChanged (or something) event that fires on the UI thread. You can cause the ProgressChanged event to fire from the background thread by calling worker.ReportProgress(state, percentComplete).
Another option is to get the SynchronizationContext of the UI (using the static System.Windows.Forms.WindowsFormsSynchronizationContext.Current property), then on the background thread, call syncContext.Post(someDelegateToExecute). The someDelegateToExecute method will execute on the UI thread.
|
|
|
|
|
If you are familiar with delegates and the BeginInvoke stuff for UI threads, please use that to update your list box. If your not familiar with it, you should read up on it. There are some good articles on this web site and the help documentation is good on this subject (it comes with examples).
Phil
|
|
|
|
|
In my class I have to update a property every second until the user stops this task.
I choose a System.Threading.Timer to do the job but the timer runs on its own thread so it cannot update the class property.
My question:
Is it possible to implement something like Control class's Invoke method or do I have to choose another approach?
|
|
|
|
|
Hi,
you could use the InvokeRequired and Invoke members of Control.
But it is much simpler to use a Forms.Timer; that one ticks on the GUI thread.
|
|
|
|
|
Thanks Luc for your quick response!
I think I have to go in more detail:
I developed a class which accesses my tv card only for radio stations so I do not have a gui thread.
But I would like to send the signal level of the station by calling a method of a com interface every second within the radio class..
Now using a timer who makes the call to the interface, I get an error, this is because the timers thread is not the thread which created the radio class
As far as all com objects and interfaces are part of a windows forms application I was able to use this.Invoke((MethodInvoker)delegate()... to make a call to the com interface method in the timers TimerCallback.
Of course I can solve my problem using a timer in the windows forms application, but I would like to use the timer in my radio class.
|
|
|
|
|
Hi,
if you have a "TVCard" class in C#, it should probably not be concerned by GUI stuff at all;
but it might offer a "Station" property getting/setting the current station selected.
Then an app using the TVCard class could periodically read and display the Station property
using the right thread, and based on its own requirements.
Alternatively, but I dont like this as much, the TVCard class could offer an event that
periodically reports the Station setting; then the app can subscribe to that event,
and must itself take care of the threading issues. The reason I dont like this that much
is that now the TVCard is deciding on how often something happens in the app, normally
the app is in charge !
Of course you could give the TVCard an Interval property as well,
so it really starts to look like a timer.
And yes, you could teach your TVCard to always raise the event on the GUI thread too,
but that would not be needed when an app wants to know but not display the Station setting,
therefore it should not be included in the TVCard ("separation of concerns").
So I would stick with the simple Station propery and let the app do timing and GUI stuff.
|
|
|
|
|
-So I would stick with the simple Station propery and let the app do timing and GUI stuff-
I think its a clean solution!
Thank you very much
pusch
|
|
|
|
|
The System.Timers.Timer has a SynchronizingObject[^] property. This can be used to make sure that the timer callback function is called on a specific thread.
Maybe this will help . . .
|
|
|
|
|
this is what I was looking for!
May be it is not the best solution for my posted question, but its exactly what I had in mind.
Thanks a lot.
regards
pusch
|
|
|
|
|
Hi,
I have written simple code in click event of button as below
MessageBox.Show("HI");
When I run the application, I got the blank messagebox means it doesn't have the message text "HI" and also doesn't display OK messagebox button. Anybody have any idea. Please guide. Any type of help is appreciated.
Himanshu Batavia
|
|
|
|
|
You only need to post your question once. Someone will answer eventually.
Your code should look something like this
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
I would seriously suggest buying a book and working through it to get started in programming before looking for help online. Learn the basics first.
I think I'm going to call my next project "Chuck Norris". It's a sure way to guarantee it's unbreakable.
• • •
But fortunately we have the nanny-state politicians who can step in to protect us poor stupid consumers, most of whom would not know a JVM from a frozen chicken. Bruce Pierson
|
|
|
|
|
|
Hello
I have written simple code like this :
MessageBox.Show("Hello");
in click event of Button1. But when I run the program and click on Button1, I get the messagebox but having no output means I don't get "Hello" text and also don't get OK messagebox button. Why it is so? Any type of help is appreciated
Himanshu Batavia
|
|
|
|
|
Sounds like your code isn't working right. Are you certain the button1.Click event is setup to be handled by your handler function? You should show us the appropriate code.
|
|
|
|
|
I want to know image dimension but I can't use Image.FromFile method to load image file because it is very slow and I want get image dimention quickly.
my image file format is JPEG
i will thank every one help me
(excuse me i can't speak english correctly)
|
|
|
|
|
You will need to read the header information from the file then. This link[^] takes you to the specification. Be aware though that there are *many* different versions of JPEG, so you will need to test this thoroughly.
|
|
|
|
|
thank you a lot.
but this site does not offer any C# code and I can't find image dimension obviously in it's jpeg header format.
thank you again.
if any one have more simple answer please send it. thank you again.
|
|
|
|
|
I answered this question a couple of days ago right here.[^]
|
|
|
|