Showing posts with label c#. Show all posts
Showing posts with label c#. Show all posts
C# - Determine if a Form is Open
I had a requirement to determine if a form was open in Visual Studio using C#. If it wasn't open, I wanted to open a new one, and if it was, I wanted to pop it to the front. Obviously, I needed a method to see if there was one.
Here is the code for the method:
private Boolean checkFormOpen(string formName)
{
Boolean openForm = false;
FormCollection fc = Application.OpenForms;
foreach (Form namer in fc)
{
if (namer.Name.Equals(formName))
{
openForm = true;
}
}
return openForm;
}
I just call this method and pass the handle of the Form name to it. Hope this helps.
C# - Truncating FileName
Some people, especially lawyers and bankers who create documents, often have an essay for the filename. Most systems are quite adept at handling very long filenames, but for practical reasons, I recently had to truncate a filename to 250 characters in a C# .Net program.
You simply cannot truncate with a substring, because you will lose the file extension. Also, in the past, you could always bet that a file extension is always three characters preceded by a dot "," like .doc, or .jpg. However now there is a good possiblity that you encounter a 5 character file extension including the dot.
So the algorithm is to get a string of the last 5 characters of the very long file name. Then you get a substring of the first 245 characters. The number in the substring is an index number that begins at zero, so it is 245 - 1 or 244. Here is the code snippet:
if (filename.Length > 250)
{
String t1 = filename.Substring(0, 245);
String t2 = filename.Substring(filname.Length - 6, 5);
filename = t1 + t2;
}
Hope this helps someone.
You simply cannot truncate with a substring, because you will lose the file extension. Also, in the past, you could always bet that a file extension is always three characters preceded by a dot "," like .doc, or .jpg. However now there is a good possiblity that you encounter a 5 character file extension including the dot.
So the algorithm is to get a string of the last 5 characters of the very long file name. Then you get a substring of the first 245 characters. The number in the substring is an index number that begins at zero, so it is 245 - 1 or 244. Here is the code snippet:
if (filename.Length > 250)
{
String t1 = filename.Substring(0, 245);
String t2 = filename.Substring(filname.Length - 6, 5);
filename = t1 + t2;
}
Hope this helps someone.
Form not visible in task bar
When you support old code, you get a lot of surprises and you learn a lot of things. I had a client tell me that one of the forms windows written in Visual Studio with C# needed its own window. I didn't understand what he meant. He kept saying that when he switched between apps, he couldn't find a certain form on the C# app. When pressed further as to the problem, it turned out that when he hovered over the app in the task bar, if he had different forms open, one was not visible in the task bar. This required some investigation.
As it turns out, there is a Form property which is a boolean. It is called Form.ShowInTaskbar.
So when I added the following line to the constructor, everything was fixed:
this.ShowInTaskbar = true;
I still don't know what was turning it off, but the explicit assignment fixed the problem.
Hope this helps someone.
Killing Application With Escape Key in .NET C#
So, you are a C# weenie and you want to kill your Forms Application by hitting the escape key. It's as easy as pie.
In the forms load method, put in the following two lines:
this.KeyPreview = true;
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
Then create the KeyDown method:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
}
Hope this helps.
Invoke or BeginInvoke cannot be called on a control until the window handle has been created
I had a C# program where I kicked a new thread to check for messages in the database. I used a delegate in the new thread to call a method in the class that went to the database message table and checked for new messages higher than the message primary key id that it knew about.
It kept throwing this error:
Invoke or BeginInvoke cannot be called on a control until the window handle has been created
As it turns out, I had a couple of variables that were declared but not initialized or set and instead of throwing a null exception, because they were called using a delegate by a different thread, it threw the above error instead.
The fix was to initialize and set the variables.
Hope this helps someone.
Copying Forms Error in Visual Studio .Net and C#
A colleague phones me up and asks me about a Visual Studio C# project that I had written a couple of years ago.
He tried simply copying a form and he kept getting the error:
The item "obj\Release\<filename>.resources" was specified more
than once in the "Resources" parameter.
Duplicate items are not supported by the "Resources" parameter.
Hmm. I went to stacktrace (the website) and they said that the easiest way was to go into Explorer, duplicate the files and add them to the project. It didn't work because the .resx file was not under the .cs file structure.
It turns out that the re-factoring isn't complete. If you just plain old copy the form in the Solutions Explorer, rename it, and then immediately go and fix all of the references to the old name in the .cs file (rename the class, the constructor and all references to the old name) and then do the same thing in the .resx file, everything eventually works.
It eliminates the above problem.
Hope this helps someone.
Subscribe to:
Posts (Atom)