|
Graphics.DrawString isn't going to tell you that. However, there is a similar Graphics.MeasureString which, when given a maximum width, will return the rectangular hull of the text Graphics.DrawString would paint. Make sure you give DrawString the same string and font, and a rectangle with the same width and sufficient height.
Mind you, there are some anomalies, MeasureString and DrawString don't always match up to the last pixel. Google may be able to provide more details on this.
|
|
|
|
|
Thanks Luc! I have not done it yet, if MeasureString can return the Rectangle for the text, then I believe it will really help. Just about to continue.
modified 28-Nov-11 7:12am.
|
|
|
|
|
You are going to have to keep a record of how many characters have been entered and in what fonts, then you can use the Graphics.MeasureString [^] method to find whereabouts you are on the panel.
My plan would be to use the width and height of the panel to create a Rectangle , which I would use with the DrawString method (has an overload which takes a rectangle to draw in), and just keep adding the user input to the end of the string, calculating beforehand how many characters the rectangle could hold, so that I could just use the number of characters entered to tell if I need to create a new page.
Hope this makes sense.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Wayne Gaylard wrote: Hope this makes sense.
Hmm. Counting characters doesn't make sense when using proportional fonts (where iiiiiiiiii and wwwwwwwwww have different width).
|
|
|
|
|
I din't really mean actually counting the characters, just meant to use MeasureString with the font used to calculate the space used. My bad, I guess my English is not up to scratch today
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Thanks Wayne! I think this is a great way to tell whether I need another page or not. But I'm wondering how I can calculate the number of characters for a page given the font. The problem is knowing the number of words that can fit onto one line since the words will not have the same width.
If the words making up a line is known, it will also be possible to know the number of lines for the page but I don't know how.
|
|
|
|
|
AS Luc mentioned, proportional fonts is always going to be your nemesis here. The only way to tell is by having the actual string to measure. If you need to know the number before the user enters anything, maybe you could get an average to work with if you use MeasureString to find the length of a string of i's and then find the length of a string of w's and get the average of the 2, but that seems a bit of a hack.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
A question that needs to be answered, imho, is whether you are drawing every character on the page in the same font, at the same font-size, and in the same font-style.
If you are varying font, font-size, font-style per page, I think you are going to "go nuts" using MeasureString, and there are other, better ways you could approach what you are trying to do.
You should also consider possible quality-of-rendering issues that could affect readability, if you are using DrawString to render fonts less than 12 points in size.
A suggestion: WinForms: put a RichTextBox on a Form, set 'MultiLine' to 'false (make it fairly short in width); define a ContentsResized Event handler for the ContextResized Event. Put a regular TextBox on the page into which you write out the width of the .NewRectangle property exposed by the parameter 'e in the ContextResized EventHandler.
Put a button on the Form which when clicked changes the Font of the RichTextBox. Run this project, type away, and then, at some point, click on the button that changes the RichTextBox's font. Observe what happens as your typed content exceeds the width of the RichTextBox.
Kind of like this:
private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
{
textBox1.Text = e.NewRectangle.Width.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Font = new System.Drawing.Font("Arial", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
} And observe what happens to the reported measurement.
Now, also, consider that at any moment you can render a bit-map from the contents of any Control.
A little more information about your scenario, please.
best, Bill
"Anyone who shows me my 'blind spots' gives me the gift of sight." ... a thought from the shallows of the deeply shallow mind of ... Bill
modified 30-Nov-11 12:35pm.
|
|
|
|
|
I am trying to add around 3000 columns to a dataset table.But it fails to add after a limit.
Is there any limitation to number of columns that can be added.If yes then what?
|
|
|
|
|
If you have to hit that limit, you've got bigger issues with your application...
It's System.Int32.MaxValue , or 2,147,483,647. But since you wont be able to reach that limit without generating an OutOfMemory Exception, it's limited by available memory.
Can't find the max columns in Datatable[^]
|
|
|
|
|
kgaurav.kumbhawat wrote: add around 3000 columns to a dataset table
I got stuck with one of these requirements a few years ago, needed to export a shed load of stress data and my favourite tool exports a datatable to CSV. I had to rewrite the method to write a CSV directly. You can find the limitations in MSDN but I don't have a link.
If you are wanting a database table with 3k columns then take a serious look at your requirements there should be a better solution than 3k of columns!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: You can find the limitations in MSDN but I don't have a link. I But there is no details about MAX columns only MAX rows there.
To add rows to a DataTable, you must first use the NewRow method to return a new DataRow object. The NewRow method returns a row with the schema of the DataTable, as it is defined by the table's DataColumnCollection. The maximum number of rows that a DataTable can store is 16,777,216. For more information, see Adding Data to a DataTable.
|
|
|
|
|
The DotNet helpfile does not explicitly state a limit on the number of columns. Some time ago I had the same questions 'how many columns can I add' so I wrote a simpel loop.
I was able to add more than 10,000 columns with unique names and of object type, so your 3,000 should be possible.
So the question becomes, how is it failing? What is the error message you are getting? Without this information we are just playing a guessing game. So please let us know...
Here are a few simple things to check:
Are you sure all your columnnames are unique?
Are you running out of memory? (How much memory are you consuming when the addition fails.)
Do you have any rows in the table already is is the table empty?
Thanx.
(EDIT: So I was a little slow typing this response, not changing the text though)
|
|
|
|
|
I would like to update the progress bar of form2 from MyClass.. please help me.. Thank in advance.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public ProgressBar ProgressBar
{
get { return this.progressBar; }
set { this.progressBar = value; }
}
}
class MyClass
{
void DoaLengthyJob()
{
int i=0;
while ( {
Form2 f = new Form2;
f.ProgressBar =i;
i++
}
}
}
|
|
|
|
|
You should make DoALengthyTask a public method with a Parameter that takes an instance of a progressbar, then when you call the method just pass in the progressbar property you created :-
public partial class Form1 : Form
{
public ProgressBar Progress { get; set; }
public Form1()
{
InitializeComponent();
MyClass myClass = new MyClass();
myClass.DoALengthyTask(Progress);
}
}
class MyClass
{
public void DoALengthyTask(ProgressBar progress)
{
int i=0;
while (true)
{
progress.Value = i;
i++;
}
}
}
Hope this helps
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Sorry .. here is more info.
<pre lang="cs">public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public ProgressBar ProgressBar
{
get { return this.progressBar; }
set { this.progressBar = value; }
}
}
class MyClass
{
void InitLengthyJob()
{
private void BackgroundWorker1_DoWork(
object sender,
System.ComponentModel.DoWorkEventArgs e)
{
DoaLengthyJob();;
}
}
void DoaLengthyJob()
{
int i=0;
DoaLengthyJob()
Form2 f = new Form2;
f.ProgressBar =i;
i++
}
}
|
|
|
|
|
ptr_Electron wrote:
void DoaLengthyJob()
{
int i=0;
while ( {
Form2 f = new Form2;
f.ProgressBar =i;
i++
}
}
There are a couple of issues here:
1. You are not showing the form at all (maybe you ar showing another instance of the Form2 class, but that doesn't help, they are distinct, and so are the progress bars)
2. a lengthy job should not run on the main thread as it would block the GUI, resulting in poor behavior: no window repainting, unable to move/resize the window, etc. You should use a different thread for long operations, a BackgroundWorker could help here.
3. And then, another thread shouldn't directly create or touch WinForm Controls. You need some kind of Invoke, see here[^].
|
|
|
|
|
Sorry .. here is more info.
<pre lang="cs">public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
public ProgressBar ProgressBar
{
get { return this.progressBar; }
set { this.progressBar = value; }
}
}
class MyClass
{
void InitLengthyJob()
{
private void BackgroundWorker1_DoWork(
object sender,
System.ComponentModel.DoWorkEventArgs e)
{
DoaLengthyJob();;
}
}
void DoaLengthyJob()
{
int i=0;
DoaLengthyJob()
Form2 f = new Form2;
f.ProgressBar =i;
i++
}
}
|
|
|
|
|
OK you fixed one issue, now deal with #1 and #3.
And read up on BackgroundWorker and how it helps you in reporting progress.
|
|
|
|
|
Hi
Not sure if this is the right forum for this as its more of a general question, but it didnt seem to fit any other category, and since I am looking for a c# solution....
I am looking for ideas to handle possessive case for a multi-lingual site.
e.g
John Smith's photo's (the 's after smith).
Since the name is what the user entered, and in this case IN ENGLISH it requires an 's to make it possessive, but in some cases it will just require an apostrophe, without the (s).
Doing this for a single language is easy enough when you know the rules, but how would you implement this on a site using Localization?
Is there a better way than hard-coding the language rules per culture code?
Thanks
|
|
|
|
|
I don't think you can do that a generic solution. In English it's easy, but in Polish you would have something like this:
The name: "Jan Kowalski".
Possessive case: "Jana Kowalskiego".
You see, in Polish we have 7 (seven) cases. And those are applied to both first name and last name.
Similar situation also applies to Russian, Czech and other Slavic languages. Don't know many others, but generally, you can't create generic solution for all languages.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Thanks, I figured that would be the case.
I was just wondering if there was a better way than a whole bunch of "ifs" for each language we implement.
|
|
|
|
|
Well... Another way could be to do with String.Format.
<br />
string.Format(formattingString, "John Smith");<br />
And now for English you can have resource: "{0}'s photos".
For Polish you could have: "Zdjęcia należące do użytkownika {0}".
And although it is quite long, it is grammatically valid.
But you have to create a version for each language you support
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
I am creating an organizer with basic add/delete/edit functions. I need to create an animated agent which talks to the user. I am stuck.
Any help guys? I'm really stuck
|
|
|
|
|
What is it that you are stuck with exactly? Have you tried one of the CodeProject articles[^] on the subject?
Bastard Programmer from Hell
|
|
|
|