J
Jax
Custom control problem.
I'm modding a textbox so that it will always have a "%"
sign at the end of it.
I have overrided the Text property to account for the "%"
value within the textbox and have add BaseText to give
access to the baseText method too.
I have the text_changed event handler wired up to this
method, problem is that it works ONLY every other time.
The statement that works every other time is this:
string s = ResultOfOtherThings();
this.BaseText = s + "%";
In debugger I can see it doesn't add the "%";
I've tried:
s = s+"%";
But in the debugger s appears without the "%".
So therefore I ask is there anything "special" about '%'
or "%" because it's making little sense at best.
Full code follows:
public class PercentBox : System.Windows.Forms.TextBox
{
//Characters that are allowed to be typed into the box
private ArrayList allowedChars;
public ArrayList AllowedChars
{
get{return allowedChars;}
set{allowedChars = value;}
}
// my override of text
public override string Text
{
get
{
if(base.Text.Length>1)
{
return base.Text.Substring(0, base.Text.Length-1);
}
else
{
return "";
}
}
set{base.Text = value + "%";}
}
// to give access to the base still
public string BaseText
{
get{return base.Text;}
set{base.Text = value;}
}
public PercentBox()
{
base.Text = "%";
this.TextChanged += new EventHandler
(PercentBox_TextChanged);
this.KeyPress += new
System.Windows.Forms.KeyPressEventHandler
(PercentBox_KeyPress);
allowedChars = new ArrayList(10);
allowedChars.Add('0');
allowedChars.Add('1');
allowedChars.Add('2');
allowedChars.Add('3');
allowedChars.Add('4');
allowedChars.Add('5');
allowedChars.Add('6');
allowedChars.Add('7');
allowedChars.Add('8');
allowedChars.Add('9');
allowedChars.Add((char)8);
allowedChars.Add('.');
allowedChars.Add('%');
}
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
// Calling the base class OnPaint
base.OnPaint(pe);
}
private void PercentBox_TextChanged(object sender,
System.EventArgs e)
{
PercentBox pBox = (PercentBox) sender;
if(pBox.BaseText.Length>0)
{
// to make sure there only 1 % mark so the next if
//statement will work
string s = OnlyOneCharOfType(pBox.BaseText, '%');
if(s[s.Length-1] != '%')
{
// remove the eventhandler during the operation
this.TextChanged -= new EventHandler
(PercentBox_TextChanged);
string victim = pBox.BaseText;
// removes all '%' from victim
s = RemoveCharFromString(victim, '%');
// make sure there is only one decimal place
s = OnlyOneCharOfType(s, '.');
pBox.BaseText = s+"%";
this.TextChanged += new EventHandler
(PercentBox_TextChanged);
}
}
else
{
pBox.BaseText = "%";
}
}
private string OnlyOneCharOfType(string victim, char type)
{
bool found = false;
char[] result = new char[victim.Length];
int charCtr = 0;
foreach(char c in victim)
{
if(c == type)
{
if(found != true)
{
found = true;
result[charCtr] = c;
charCtr++;
}
else
{
continue;
}
}
else
{
result[charCtr] = c;
charCtr++;
}
}
StringBuilder sb = new StringBuilder();
foreach(char ch in result)
{
try
{
sb.Append(ch);
}
catch(Exception)
{
break;
}
}
string theResult = sb.ToString();
theResult = theResult.Trim();
return theResult;
}
private string RemoveCharFromString(string victim, char c)
{
char[] result = new char[victim.Length];
int charCtr = 0;
foreach(char ch in victim)
{
if(ch != c)
{
result[charCtr] = ch;
charCtr++;
}
}
StringBuilder sb = new StringBuilder();
foreach(char ch in result)
{
try
{
sb.Append(ch);
}
catch(Exception)
{
break;
}
}
string theResult = sb.ToString();
theResult = theResult.Trim();
return theResult;
}
/// <summary>
/// // Ensures that no letters can be added to a control
that has the key press event linked to this
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PercentBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
char c = e.KeyChar;
if(!ValidatePressEntry(c))
{
e.Handled = true;
}
}
/// <summary>
/// // Ensures that no letters can be
added to a control that has the key press event linked to
this save the '.' character for decimal places
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private bool ValidatePressEntry(char c)
{
bool bob = false;
foreach(char okChar in allowedChars)
{
if(okChar == c)
{
bob = true;
}
}
return bob;
}
}
I'm modding a textbox so that it will always have a "%"
sign at the end of it.
I have overrided the Text property to account for the "%"
value within the textbox and have add BaseText to give
access to the baseText method too.
I have the text_changed event handler wired up to this
method, problem is that it works ONLY every other time.
The statement that works every other time is this:
string s = ResultOfOtherThings();
this.BaseText = s + "%";
In debugger I can see it doesn't add the "%";
I've tried:
s = s+"%";
But in the debugger s appears without the "%".
So therefore I ask is there anything "special" about '%'
or "%" because it's making little sense at best.
Full code follows:
public class PercentBox : System.Windows.Forms.TextBox
{
//Characters that are allowed to be typed into the box
private ArrayList allowedChars;
public ArrayList AllowedChars
{
get{return allowedChars;}
set{allowedChars = value;}
}
// my override of text
public override string Text
{
get
{
if(base.Text.Length>1)
{
return base.Text.Substring(0, base.Text.Length-1);
}
else
{
return "";
}
}
set{base.Text = value + "%";}
}
// to give access to the base still
public string BaseText
{
get{return base.Text;}
set{base.Text = value;}
}
public PercentBox()
{
base.Text = "%";
this.TextChanged += new EventHandler
(PercentBox_TextChanged);
this.KeyPress += new
System.Windows.Forms.KeyPressEventHandler
(PercentBox_KeyPress);
allowedChars = new ArrayList(10);
allowedChars.Add('0');
allowedChars.Add('1');
allowedChars.Add('2');
allowedChars.Add('3');
allowedChars.Add('4');
allowedChars.Add('5');
allowedChars.Add('6');
allowedChars.Add('7');
allowedChars.Add('8');
allowedChars.Add('9');
allowedChars.Add((char)8);
allowedChars.Add('.');
allowedChars.Add('%');
}
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
// Calling the base class OnPaint
base.OnPaint(pe);
}
private void PercentBox_TextChanged(object sender,
System.EventArgs e)
{
PercentBox pBox = (PercentBox) sender;
if(pBox.BaseText.Length>0)
{
// to make sure there only 1 % mark so the next if
//statement will work
string s = OnlyOneCharOfType(pBox.BaseText, '%');
if(s[s.Length-1] != '%')
{
// remove the eventhandler during the operation
this.TextChanged -= new EventHandler
(PercentBox_TextChanged);
string victim = pBox.BaseText;
// removes all '%' from victim
s = RemoveCharFromString(victim, '%');
// make sure there is only one decimal place
s = OnlyOneCharOfType(s, '.');
pBox.BaseText = s+"%";
this.TextChanged += new EventHandler
(PercentBox_TextChanged);
}
}
else
{
pBox.BaseText = "%";
}
}
private string OnlyOneCharOfType(string victim, char type)
{
bool found = false;
char[] result = new char[victim.Length];
int charCtr = 0;
foreach(char c in victim)
{
if(c == type)
{
if(found != true)
{
found = true;
result[charCtr] = c;
charCtr++;
}
else
{
continue;
}
}
else
{
result[charCtr] = c;
charCtr++;
}
}
StringBuilder sb = new StringBuilder();
foreach(char ch in result)
{
try
{
sb.Append(ch);
}
catch(Exception)
{
break;
}
}
string theResult = sb.ToString();
theResult = theResult.Trim();
return theResult;
}
private string RemoveCharFromString(string victim, char c)
{
char[] result = new char[victim.Length];
int charCtr = 0;
foreach(char ch in victim)
{
if(ch != c)
{
result[charCtr] = ch;
charCtr++;
}
}
StringBuilder sb = new StringBuilder();
foreach(char ch in result)
{
try
{
sb.Append(ch);
}
catch(Exception)
{
break;
}
}
string theResult = sb.ToString();
theResult = theResult.Trim();
return theResult;
}
/// <summary>
/// // Ensures that no letters can be added to a control
that has the key press event linked to this
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PercentBox_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
char c = e.KeyChar;
if(!ValidatePressEntry(c))
{
e.Handled = true;
}
}
/// <summary>
/// // Ensures that no letters can be
added to a control that has the key press event linked to
this save the '.' character for decimal places
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private bool ValidatePressEntry(char c)
{
bool bob = false;
foreach(char okChar in allowedChars)
{
if(okChar == c)
{
bob = true;
}
}
return bob;
}
}