|
Thanks very much, it's really helpful!
|
|
|
|
|
Hi guys
Does anyone know how to cast the text of a textBox into a integer in C#?
Thanks and best wishes
|
|
|
|
|
You can't cast it. Use Int32.TryParse method.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Look at the Convert class. Type "Convert." and see what comes up in IntelliSense. You'll find Convert.ToInt32() is probably what you want. While it is not as save as Int32.TryParse(), it is quick and dirty.
Hogan
|
|
|
|
|
Most native numeric types in c# 2.0 have a "TryParse" method attached. An example method that would return the integer from a TextBox.Text property would be something like this:
<br />
public static int GetIntFromTextBox(TextBox textbox)<br />
{<br />
if(textbox != null && !string.IsNullOrEmpty(textbox.Text))<br />
{<br />
int myInt;<br />
<br />
string myNumberString = textbox.Text;<br />
int.TryParse(myNumberString, out myInt);<br />
<br />
return myInt;<br />
}<br />
return 0;<br />
}<br />
This will work for floats, doubles, etc as well. Just change the "datatype" in the TryParse line (double.TryParse() instead of int for instance). In the cases where the string is not a number, I believe it assigns zero (0), so the evaluation may not be neccessary.
-------------------------
Spiffdog Design
It's ok.. he's no ordinary dog...
|
|
|
|
|
In a C# project in VS 2005, when I add a reference to a DLL in the Solution Explorer (using the Browse Tab in the Add Reference dialog) I have to specifiy the full path. That means that if the DLL is a Debug DLL bin\Debug\ appears in the path. So how do I reference the Release DLL for the Release configuration?
Thanks in advance
-- modified at 5:52 Tuesday 30th October, 2007
|
|
|
|
|
You don't set a reference to the .DLL, you set it to the project that builds that .DLL, so long as the project is part of the same solution.
|
|
|
|
|
I'm afraid the project isn't part of the solution.
-- modified at 5:53 Tuesday 30th October, 2007
|
|
|
|
|
Then you have to add the reference to the Release version manually. There is no way to automatically switch between the two.
|
|
|
|
|
Hi ,
I'm having some trouble. My winform app is running SQL Queries , and I would like some sort of a progressbar to be displayed while the query is running (just a progress bar that constantly moves back & forth - I'm using the Circular Progress bar - http://www.codeproject.com/vb/net/sql2005circularprogress.asp[^]
Anyhow - It seems that I need to create a new thread to run the progress bar - If anyone would be so kind and explain me the basics on how to implement this - I would really appreciate it.
Thanks again.
|
|
|
|
|
The progress bar is UI. I would imagine you need a new thread to run the query. System.Threading.ThreadPool.QueueUserWorkItem and IsInvokeRequired will get you on the right path.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Thanks. I'd appreciate more information on how to use the IsInvokeRequired exactly ? Sorry but I'm not so familier with threading.
Plus - how do I prevent my app from continuing while waiting for the Query running thread to finish ?
|
|
|
|
|
I will have to recommend looking through WinForms threading articles for that answer as it begins to be a deep topic. The Methods I gave you (and maybe it is InvokeRequired instead) should allow you to find the exact articles that will suit your needs.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Hello. I'm attaching a delegate which throws an exception to the BackgroundWorker.DoWork event, but the exception isn't caught by the worker and passed to RunWorkerCompleted event. Apparently it has something to do with asynchronous calling of delegate, since if I make the equivalent named function and attach that to DoWork, the exception is caught as expected.
(My goal is to make a function "RunInBackgroundWithProgressDialog" so that I can launch long functions like:
<br />
RunInBackgroundWithProgressDialog(delegate(){<br />
}); )
Offending code:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker w = new BackgroundWorker();
w.DoWork += delegate(object o, DoWorkEventArgs e)
{
Console.WriteLine("sleeping 1000");
System.Threading.Thread.Sleep(1000);
throw new Exception("thrown");
};
w.RunWorkerCompleted += delegate(object o, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine("should be handled here, but isn't");
}
};
w.RunWorkerAsync();
Console.Read();
}
}
}
|
|
|
|
|
JerryNakamura wrote: Hello. I'm attaching a delegate which throws an exception to the BackgroundWorker.DoWork event, but the exception isn't caught by the worker and passed to RunWorkerCompleted event. Apparently it has something to do with asynchronous calling of delegate, since if I make the equivalent named function and attach that to DoWork, the exception is caught as expected.
(My goal is to make a function "RunInBackgroundWithProgressDialog" so that I can launch long functions like:
RunInBackgroundWithProgressDialog(delegate(){
// open up a progress dialog with a cancel button
// do stuff that takes a long time here.
});)
Looking at your code, I think the problem is in the code you aren't showing. The code in BackgroundWorker that calls DoWork needs to call it in a try/catch block and create event args with the exception if an exception is thrown from DoWork.
Nathan
|
|
|
|
|
Hi guys, I used the class below to print RTF format text on the screen the problem is that once the text is converted into an image by using the class bleow the text letters are not clear, and sharp. The text letter edges are little blurred. Could somebody please help me.
Thanks
<br />
public class RichTextBoxPrintCtrl : RichTextBox<br />
{<br />
<br />
<br />
<br />
private const double anInch = 14.4;<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
<br />
private struct RECT<br />
{<br />
<br />
public int Left;<br />
<br />
public int Top;<br />
<br />
public int Right;<br />
<br />
public int Bottom;<br />
<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
<br />
private struct CHARRANGE<br />
{<br />
<br />
public int cpMin;
<br />
public int cpMax;
<br />
}<br />
<br />
[StructLayout(LayoutKind.Sequential)]<br />
<br />
private struct FORMATRANGE<br />
{<br />
<br />
public IntPtr hdc;
<br />
public IntPtr hdcTarget;
<br />
public RECT rc;
<br />
public RECT rcPage;
<br />
public CHARRANGE chrg;
<br />
}<br />
<br />
private const int WM_USER = 0x0400;<br />
<br />
private const int EM_FORMATRANGE = WM_USER + 57;<br />
<br />
[DllImport("USER32.dll")]<br />
<br />
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);<br />
<br />
<br />
<br />
public int Print(int charFrom, int charTo, Graphics gr, Rectangle bounds)<br />
{<br />
<br />
<br />
RECT rectToPrint;<br />
<br />
rectToPrint.Top = 0;
<br />
rectToPrint.Bottom = (int)(bounds.Height * anInch);
<br />
rectToPrint.Left = 0;
<br />
rectToPrint.Right = (int)(bounds.Width * anInch);
<br />
<br />
RECT rectPage;<br />
<br />
rectPage.Top = 0;
<br />
rectPage.Bottom = (int)(gr.ClipBounds.Height * anInch);
<br />
rectPage.Left = 0;
<br />
rectPage.Right = (int)(gr.ClipBounds.Right * anInch);
<br />
IntPtr hdc = gr.GetHdc();<br />
<br />
FORMATRANGE fmtRange;<br />
<br />
fmtRange.chrg.cpMax = charTo;
<br />
fmtRange.chrg.cpMin = charFrom;<br />
<br />
fmtRange.hdc = hdc;
<br />
fmtRange.hdcTarget = hdc;
<br />
fmtRange.rc = rectToPrint;
<br />
fmtRange.rcPage = rectPage;
<br />
IntPtr res = IntPtr.Zero;<br />
<br />
IntPtr wparam = IntPtr.Zero;<br />
<br />
wparam = new IntPtr(1);<br />
<br />
<br />
IntPtr lparam = IntPtr.Zero;<br />
<br />
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange));<br />
<br />
Marshal.StructureToPtr(fmtRange, lparam, false);<br />
<br />
<br />
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam);<br />
<br />
<br />
Marshal.FreeCoTaskMem(lparam);<br />
<br />
<br />
gr.ReleaseHdc(hdc);<br />
<br />
<br />
return res.ToInt32();<br />
<br />
}<br />
<br />
}<br />
<br />
|
|
|
|
|
Blurred on screen usually results in nice print on paper. The blur is most likely from anti-aliasing.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Thanks for your reply but what should i do when i preview on screen the font looks nice and sharp and no problem but as soon as i print then that's where the font messed up. Can you tell me where can i change the fonts.
|
|
|
|
|
There is a lot of code there. Have you examined the rtf files themselves and tried to print them from word?
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
My applicaiton is independent. I don't want to depend on MS word. I have even set to
Graphic.SmoothingMode = SmoothingMode.HighQuality;
Graphics.InterpolationMode== InterpolationMode.HighQualityBicubic;
but no success. I looked in some article tooo but doesn't solve my problem when am printing RTF format text.
Thanks for replying
|
|
|
|
|
Actually the point was verification of your code. Does word open the rtf file fine? If yes then examine your rendering code, if no examine your rtf creating code.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Hello,
The code below I have:-
static string filename;
static XmlDocument xmlDoc;
and open the XML file in the Form Load.
If I remove the :- if (testDialog.ShowDialog() == DialogResult.OK)
Line the XML addnode code works OK. With the ShowDialog() line in the XML node add code does not work. Why?
Also is there something I can check (return code?) to make sure each part of the add node lines of code has worked?
void Button1Click(object sender, EventArgs e)
{
NewForm testDialog = new NewForm();
if (testDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("Here");
XmlNode root = xmlDoc.DocumentElement;
XmlElement childNode = xmlDoc.CreateElement("ABCD");
XmlElement childNode2 =xmlDoc.CreateElement("Name");
XmlText textNode = xmlDoc.CreateTextNode("ABCD");
root.AppendChild(childNode);
childNode.AppendChild(childNode2);
childNode2.SetAttribute("Name", "Name");
childNode2.AppendChild(textNode);
xmlDoc.Save(filename);
MessageBox.Show("Here2");
}
testDialog.Dispose();
}
Thanks is Advance,
Rapier
|
|
|
|
|
Does your NewForm return a DialogResult anywhere in your code? Supposing that the NewForm form has been created by you.
There are 10 kinds of people: those who understand binary and those who don't
|
|
|
|
|
Hello everyone,
I have a string which contains all sort of charateres. How can I remove the unwanted charaters and make sure what I am left with is Numerical and Alphabet charaters?
Thank you very much and have a great day.
Khoramdin
|
|
|
|
|
Use the Regex.Replace method.
-----
You seem eager to impose your preference of preventing others from imposing their preferences on others. -- Red Stateler, Master of Circular Reasoning and other fallacies
If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
God is the only being who, to rule, does not need to exist. -- Charles Baudelaire
|
|
|
|