|
Well, very often you can't depending on the font. Monospaced fonts like Courier you could, but for most you'll find a 'W' quite a bit wider than an 'I'.
If you are using a monospaced font, you'd need to get a Graphics object, then call MeasureString (I think it is - or something like that). Which will tell you how wide a string is in pixels.
Regards,
Rob Philpott.
|
|
|
|
|
One way is, create a dummy label object(with AutoSize = true), assign the text to it. Use the labels width to set the TextBox's width
|
|
|
|
|
Many thanks to everyone who replied.
I like the dummy label idea - much appreciated.
|
|
|
|
|
Haii All,
CAn any one pls tell me how to export datatable values to the Excell...and also i have to export multiple datatable to multiple sheets in Excel..
Regards,
Venkateswaran.M.S
|
|
|
|
|
Look up Microsoft.Interop.Excel, System.Data.OleDb, the JET engine and read the forums a bit more thoroughly before posting such stuff.
There is a post asking for similar stuff on the ASP.NET forum, posted yesterday.
Though it's about getting data off of an Excel spreadsheet, but yourself, being a programmer, surely can figure out a way of putting it back in there.
Regards,
Greg
var question = (_2b || !(_2b));
|
|
|
|
|
I was just working on overloading the increment operator. I was hoping to have both pre-increment and post-increment. Unfortunately I see that the same method is called regardless of whether the operator is in pre- or post-position. So I checked the spec and was surprised to find:
"
Note that the operator returns the value produced by adding 1 to the operand, just like the postfix increment and decrement operators (§7.5.9), and the prefix increment and decrement operators (§7.6.5). Unlike in C++, this method need not, and, in fact, must not, modify the value of its operand directly.
"
What the heck good is an increment operator that doesn't increment?! If I want to do x += 1 I have the addition operator for that and it works just fine. I want x++ and ++x just like with the built-in numeric types; x = x++ or x = ++x would be ridiculous, but it appears that's all we're allowed.
Am I missing something? Does anyone have a work-around?
P.S. Or is just that it's 00:34 and I should go get some sleep? It seems to be working as expected somehow?
modified on Tuesday, February 24, 2009 2:33 AM
|
|
|
|
|
When calling the increment operator, the object is assigned a new reference, that reference has the new values.
Example. We have a class IntWrapper
public class IntWrapper
{
public int IntValue
{
get;
set;
}
} and we want to use it like this
IntWrapper a = new IntWrapper();
IntWrapper b = a;
a++;
Console.WriteLine("a = {0}, b = {1}", a.IntValue, b.IntValue); Here are two possible implementations for the increment operator overload in our class, the second is the correct way
public static IntWrapper operator ++(IntWrapper instance)
{
instance.IntValue++;
return instance;
}
public static IntWrapper operator ++(IntWrapper instance)
{
IntWrapper result = new IntWrapper();
result.IntValue = instance.IntValue + 1;
return result;
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Your first sentence appears to be on-topic, but I'm not sure of the relevence of the rest.
None of the three C# books I have give guidance on overloading the increment and decrement operators. MSDN doesn't seem to either.
The Microsoft and ECMA specifications merely say it's not like in C++ (as in the quote I posted), and that's no help to me because I only dabbled in C++.
In my case, I expected it to be difficult, but because it is actually simple, it became difficult and confusing to me. "Think our minds must be too highly trained, Majikthise." -- Vroomfondel
MSDN and mainstream books should explain how simple it is rather than expecting the reader to assume it is.
"
When overloading the increment and decrement operators, do not set the value; simply return the new value, the compiler will take care of setting the value and returning the new or old value as appropriate.
return ( x + 1 ) ;
return ( x = x + 1 ) ;
"
P.S. I just checked my Rational struct and I see that I have it correct there.
|
|
|
|
|
Yeah, the compiler takes care of it, and it is unexpectedly easy if you're using value types [structs]. If you're using reference types then how to implement it so the results are what's expected gets more tricky... see the posts below.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Huh? Both a and b refer to the same instance, so shouldn't a++ affect b?
[EDIT] You're right. I can see why that's happening (x++ is compiled down to x = IntWrapper.Increment(x)), and I guess it fits in with the semantics of the increment operator (operand changes to a new value), but it certainly is confusing when dealing with reference types.
modified on Tuesday, February 24, 2009 2:32 PM
|
|
|
|
|
I know - this is a wierd one. I thought more about it straight after I posted but didn't edit the post at all because I wanted to see what the reaction was to confirm what I thought, or to flame me down!
Personally, I would never use ++ or -- on reference types. It's not that obvious how it will affect things. Those operators are about changing values so I keep them for structs only.
The main rule I leaned about operator overloading is if it's not 100% obvious what something will do and affect other objects - either document it very clearly or don't do it!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
I don't hold myself responsible for any post I make after 7 pm.
|
|
|
|
|
|
I try not to wake up before 10AM but sure, whatever
(Actually for this entire year so far I have been up at 6 on work days which sucks)
|
|
|
|
|
hi,
how i can create a 2d graph in c# like the performance graph in the task manager?
|
|
|
|
|
The Graphics class has some methods like DrawLine() that allow you draw shapes during run time.
You can get a Graphics object by using this.CreateGrahpics() or making one yourself and assigning to the form later with this.CreateGrahpics() .
If you use it with the correct values, I suppose you can make a performance graph.
Of course that's just the graph, to create the background simply us the BitMap class to read a file and then use the Graphics object's DrawImage() method to display it.
You should use double buffering, which simply means drawing to a bmp in memory and
whne it's done, drawing the content on a 2nd BitMap and passing that to the Graphics object.
You can try make it work with the other methods of the Graphics class, ike DrawRectangle() or FillRectangle() .
|
|
|
|
|
Try using ZedGraph[^]. It's pretty good and should give you more than enough.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
I have a datagrid incliding: MedicineName, Contry, Quantity, .., Total.
When I want to type a word ( ex: "B" ) then appearing a list conntaint: "B1","B2","B6" ( note that "B1 B2 B6 " exist in list) then it allow to choose a item from the list. The list is disappeared and also that all has value which chosen.
Thank you very much!!
|
|
|
|
|
Hi
Is it possible to convert String var to String[]?
|
|
|
|
|
Putting the string as an array of that string?
string[] arr = new string[] { str };
Or splitting the string?
str.Split(' ');
Eslam Afifi
|
|
|
|
|
Hi
I used the following code to list all the folders in a given path:
Here sp is a string which contains the list of all the folders...
DirectoryInfo dire = new DirectoryInfo(sss);
if (dire.Exists)
{
String[] folder;
folder = Directory.GetDirectories(sss);
foreach (string foldername in folder)
{
FileInfo fil = new FileInfo(foldername);
sp = fil.ToString();
}
}
Now to list all the files in all folders contained in sp,is it right to
convert sp to string[] and proceed this way....it still gives me the files in
in only 1 folder
String[] gi = new String[] { sp };
foreach (String pil in gi)
{
DirectoryInfo di = new DirectoryInfo(pil);
if (di.Exists)
{
String[] files;
files = Directory.GetFiles(pil);
foreach (String filename in files)
{
FileInfo fil2 = new FileInfo(filename);
listBox2.Items.Add(fil2.ToString());
}
}
}
|
|
|
|
|
What you want to do exactly???
Do you want to display all the files name in the list control or folders Names??
|
|
|
|
|
Hi
I want to list all the folders in any drives and then the files
in each folder in a listbox control
I am able to list all the folders...
I tried with the following code to list the files in the folder
The folder contains 10 files but this code lists only 1 file from
that folder...The last foreach does not work properly...
What changes should i make?
private void ShowPath_Click(object sender, EventArgs e)
{
reader = new XmlTextReader("path.xml");
Form1 f = new Form1();
String sp="";
String[] gi;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Text:
String s1;
s1 = reader.Value;
String ss = listBox1.SelectedItem.ToString();
String sss = ss + s1;
object o3 = (object)sss;
DirectoryInfo dire = new DirectoryInfo(sss);
if (dire.Exists)
{
String[] folder;
folder = Directory.GetDirectories(sss);
foreach (string foldername in folder)
{
FileInfo fil = new FileInfo(foldername);
sp = fil.ToString();
}
}
gi = new String[] { sp };
foreach(String pil in gi)
{
DirectoryInfo di = new DirectoryInfo(pil);
if (di.Exists)
{
String[] files;
files = Directory.GetFiles(pil);
foreach (String filename in files)
{
FileInfo fil2 = new FileInfo(filename);
listBox2.Items.Add(fil2.ToString());
}
}
}
break;
}
}
}
|
|
|
|
|
Are you using two list controls
one is for showing the list of folders and
the other for showing the list of files for selected folder from the folder list box??
|
|
|
|
|
Hi
I am using one listbox to list the drives in a system and a another
to list both the folders and the files..
I tried the same in 2 different listbox controls n that works well...
But my requirement is to list the files and folders in one listbox....
|
|
|
|