|
-
July 20th, 2005, 06:31 AM
#1
characters upto control width
how should we restrict characters typed in a control to its width only.
-
July 20th, 2005, 07:02 AM
#2
Re: characters upto control width
Put an event handler in for the "TextChanged" event.
Then, get the text out of the text box and measure it.
I'd have an inherited class to do this :
Code:
public class WidthLimitTextBox : TextBox
{
private string m_sOldText = "";
private bool m_fHandleTextChanged = true;
protected override OnTextChanged(EventArgs e)
{
if (m_fHandleTextChanged)
{
m_fHandleTextChanged = false;
using (Graphics graphics = this.CreateGraphics())
{
Size sizeText = graphics.MeasureString(this.Text, this.Font).ToSize();
if (sizeText.Width > this.ClientRectangle.Width)
{
this.Text = m_sOldText;
}
else
{
m_sOldText = this.Text;
}
}
m_fHandleTextChanged = true;
}
}
}
I've not tried this out, but it should give you a rough idea of how to do it.
Darwen.
-
July 20th, 2005, 07:06 AM
#3
Re: characters upto control width
There is a maxlength attribute for the html textboxes. I am not sure about the server side controls but u might need to write some javascript validations to restrict the user from doing this. The server side control has a size property though, that sets or returns the expected length of the specified text box, in characters. The default value for the size of a text box is 0, which means that the browser will use its default text box length.
 Originally Posted by MSDN
This information is not used to perform validation checks against the actual input value. To perform a validation check against an input value, use the validator controls.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|