Yes, I have already researched this question. I've found this: How to display remaining textbox characters in a label in C#? and many others just like it. That's how I managed to get this following code pieced together:
protected void rtdDisclaimer(object sender, EventArgs e)
{
lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}
I've never coded in c# before, but am working on a group project and that's the language the group lead chose. I'm new to programming and have minimal experience with java. This program is being done in visual studio. I'm trying to make the label show the number of characters remaining depending upon what's typed in the richtextbox. There are no errors, but the label isn't displaying anything at all.
You must associate the method to the textchanged event of the control.
protected void rtbDisclaimerTextChanged(object sender, EventArgs e)
{
lblCharCount.Text = "Characters Remaining:" + (700 - rtbDisclaimer.Text.Length).ToString(); // char count limit set to 700
}
On the constructor, after InitializeComponent(), yo must add this line:
rtbDisclaimer.TextChanged += new EventHandler(rtbDisclaimerTextChanged);
Your code will work, but you need to attach the method to the correct event on your RichTextBox, so it will invoke that code when the event fires.
Add this to the constructor of your Form:
rtbDisclaimer.TextChanged += rtdDisclaimer;
The function you mentioned in your question seems a server side and you can call it on a an event like a button's click.
If you want to display characters length while typing then use JavaScript/jQuery
like
$('textarea').keyup(updateCount);
$('textarea').keydown(updateCount);
function updateCount() {
var cs = $(this).val().length;
$('#characters').text(cs);
}
Check this too. JsFiddle example (by Dreami)
Hepe this helps!
Related
I'm trying to make a simple incremental game.
I have a button to "build a hut". Basically, add 1 to an int value every time it's pressed. I want to then show in a label how many huts were built. But the label1.Text only accepts string values.
However, when I convert the integer .ToString, it doesn't work. It keeps the number at 1 and doesn't up it.
public void Button1_Click(object sender, EventArgs e)
{
int numberofhuts = 0;
numberofhuts++;
label1.Text = numberofhuts;
}
That's what it looks like. Any help would be super appreciated.
Right now, you reset the variable numberofhuts to zero, every time you click the button (as others have pointed out). So you need to do one of the following two things:
Move the variable to a broader scope (i.e. move it outside the button click.function)
Use the label text as a starting point for your increment.
The second approach is probably not the best, as this requires some sort of mechanism to ensure that the label text is always numeric. So you could do something like this:
public partial class MyForm : Form
{
// Constructor (normally generated by Visual Studio)
public MyForm()
{
InitializeComponent();
}
// Create, and initialize the variable outside the method.
private int _numberOfHuts = 0;
// When clicking the button, the variable is incremented
// and the label is updated with the new value.
private void Button1_Click(object sender, EventArgs e)
{
_numberOfHuts++;
label1.Text = numberOfHuts.ToString();
}
}
Also, you should consider your naming. Names like Button1 and Label1 are poor choices, because they in no way indicate what they objects function are. Instead use something like IncrementHutCount for the button and NumberOfHuts for the label.
Edit:
Note that the scope change I made, might not be broad enough. I just made the assumption that you only have a single form, which lives for the entire life span of the program. If this is not the case, you need to move it somewhere else.
I am creating a rs232 dial pad in a WPF. The problem I am running into is with the star character. I don't know how to ask the question correctly because all I am getting results for are "password characters only".
Ultimately I'm trying to add the "*" programmatically. It does not matter if I used the keyboard or the virtual button. Whenever the character is added the program freezes. The debugger isn't pointing to anything and seems like its still waiting for instruction.
Any ideas?
Im adding the star with
textBox.text="*";
Honestly I am not sure what happened. I started a new WPF and it worked. So i removed auto generated method all together and recreated it. Now it works. The entire method looked like this
private void button_Click(object sender, RoutedEventArgs e)
{
textBox.Text = "*";
}
No clue but it works. Thank you for the replies
I am using ScintillaNET to make a basic IntelliSense editor. However, I have a problem when I call _editor.CallTip.Show("random text") in the AutoCompleteAccepted Event.
If I type pr for example, and scroll and select printf in the drop-down list, it goes to my AutoCompleteAccepted event and when I call the CallTip.Show, the rest of the word does not get added (however, without that CallTip code, the rest of the word is filled).
So, if I typed pr then it stays pr and I get my CallTip. How do I make sure the rest of the word gets inserted AND the CallTip shows?
Is the AutoCompleteAccepted Event not the right place to call it? If so, where should I call the CallTip.Show so that it works side-by-side with my AutoComplete?
Finally figured it out! The AutoCompleteAccepted Event isn't the right place to put CallTip.Show
What is going on is the fact that when AutoCompleteAccepted Event is called and you add text to the ScintillaNET control, it takes time for the UI to update and so, when you call to show the CallTip, it interferes with the text being inserted into the control.
The better way to do it is to call CallTip.Show in the TextChanged event, as they you know that the text has been inserted when the AutoCompleteAccepted Event was called.
It would now look something like this:
String calltipText = null; //start out with null calltip
...
private void Editor_TextChanged(object sender, EventArgs e)
{
if (calltipText != null)
{
CallTip.Show(calltipText); //note, you may want to assign a position
calltipText = null; //reset string
}
...
}
...
private void Editor_AutoCompleteAccepted(object sender, AutoCompleteAcceptedEventArgs e)
{
if (e.Text == "someThing")
{
/* Code to add text to control */
...
calltipText = "someKindOFText"; //assign value to calltipText
}
}
That is essentially what can be done to ensure the AutoComplete fills correctly and you get the CallTip to show.
Just note, that the CallTip MAY end up in unintended places, so it is recommended to set the value of where you want the CallTip to show up
Long time listener, first time caller here. I'm having a strange issue with the TextBox in WinRT C#/XAML that I hope someone may be able to help me with.
Basically, I'm working on creating a Custom Control that essentially requires a second TextBox to be a copy of the first, including showing the same Text, and showing the same Selected Text. Obviously for the Text requirement I simply respond to the TextChanged event on the first TextBox and set the Text of the second TextBox to the Text from the first, which works great.
For the Selected Text requirement I started with a similar solution, and my code for this is as follows:
void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
this.TextBox2.Select(this.TextBox1.SelectionStart, this.TextBox1.SelectionLength);
}
This seemed to work pretty well when initially used with a mouse:
But I'm having a problem when selecting text with Touch. I double-tap within the TextBox to create the first "anchor" as you do in Touch, then drag to begin the selection; but I only ever manage to select a single character normally before the selection stops. The TextBox doesn't lose focus exactly, but the behaviour is similar to that; the selection anchors disappear and I can't continue selecting anything unless I re-double-tap to start a new selection. If I remove the code to select text in TextBox2 then the Touch selection behaves perfectly in TextBox1.
I've been trying to fix this for a while and cannot, I'm not sure if I can get the desired behaviour with WinRT TextBoxes. Does anyone have any ideas? Or perhaps another way to implement a solution with two TextBoxes with this behaviour?
Thanks a lot.
So this is far from an answer, but discovered a few things that maybe will help you or others come up with a potential workaround. Apologies if these are things you've already seen and noted.
First, it's not the call to TextBox2.Select() that's the problem per se. This for instance, works fine for me
private void txt1_SelectionChanged(object sender, RoutedEventArgs e)
{
var start = TextBox1.SelectionStart;
var length = TextBox1.SelectionLength;
TextBox2.Select(3, 5);
}
unfortunately, using start and length versus the hard-coded 3 and 5, that is, the following, DOES NOT WORK:
private void txt1_SelectionChanged(object sender, RoutedEventArgs e)
{
var start = TextBox1.SelectionStart;
var length = TextBox1.SelectionLength;
TextBox2.Select(start, length);
}
I also discovered that I could select TWO characters if I started from the end, but only one from the beginning. That got me to thinking about dispatching the call to set the second selection:
private void txt1_SelectionChanged(object sender, RoutedEventArgs e)
{
var start = TextBox1.SelectionStart;
var length = TextBox1.SelectionLength;
Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low,
() => TextBox2.Select(start, length));
}
Now I can select 2 from the front and 3 and sometimes 4 from the back. Took it a step further, and was able to select as many as six or seven with a really fast swipe.
private void txt1_SelectionChanged(object sender, RoutedEventArgs e)
{
var start = TextBox1.SelectionStart;
var length = TextBox1.SelectionLength;
Dispatcher.RunIdleAsync((v) => Highlight());
}
public void Highlight()
{
TextBox2.Select(TextBox1.SelectionStart, TextBox1.SelectionLength);
}
Seems like the trick to working around this is not setting TextBox2 until whatever vestiges of the TextBox1 SelectionChanged event have completed.
This may be worth registering on Connect.
Mine is only a partial solution as well.
I did some debugging and noticed that the SelectionChanged event is fired throughout the text selection process. In other words, a single finger "swipe" will generate multiple SelectionChanged events.
As you found out, calling TextBox.Select during a text selection gesture affects the gesture itself. Windows seems to stop the gesture after the programmatic text selection.
My workaround is to delay as long as possible calling the TextBox.Select method. This does work well, except for one edge case. Where this method fails is in the following scenario:
The user begins a select gesture, say selecting x characters. The user, without taking their finger off the screen, pauses for a second or two. The user then attempts to select more characters.
My solution does not handle the last bit in the above paragraph. The touch selection after the pause does not actually select anything because my code will have called the TextBox.Select method.
Here is the actual code. As I mentioned above, there are multiple selection changed events fired during a single selection gesture. My code uses a timer along with a counter to only do the programmatic selection when there are no longer any pending touch generated selection changed events.
int _selectCounter = 0;
const int SELECT_TIMER_LENGTH = 500;
async private void TextBox1_SelectionChanged(object sender, RoutedEventArgs e)
{
// _selectCounter is the number of selection changed events that have fired.
// If you are really paranoid, you will want to make sure that if
// _selectCounter reaches MAX_INT, that you reset it to zero.
int mySelectCount = ++_selectCounter;
// start the timer and wait for it to finish
await Task.Delay(SELECT_TIMER_LENGTH);
// If equal (mySelectCount == _selectCounter),
// this means that NO select change events have fired
// during the delay call above. We only do the
// programmatic selection when this is the case.
// Feel free to adjust SELECT_TIMER_LENGTH to suit your needs.
if (mySelectCount == _selectCounter)
{
this.TextBox2.Select(this.TextBox1.SelectionStart, this.TextBox1.SelectionLength);
}
}
Is there a way to make a label automatically update itself so that I don't have to use a button to send out the command. What I have setup is a subtotal textbox, discount textbox, tax label, shipping textbox, and total label. So, when people fill in the subtotal, discount, and shipping, I want the tax label to be calculated, but only if previously a certain state was selected in another part of the form. So then, with all those filled in, I want the total label to be filled in. All of these I know I can do with a button, but I was wondering if there is a way to automate it using C# in Visual Studio.
Thanks.
I use the TextChanged Event to update such values between pairs of textboxes. Here are some extracts of my code:
private void onLongitudeTextChanged(object sender, EventArgs e) {
updateDistanceAndBearing();
}
updateDistanceAndBearing does some error checking - this can be a good idea if the user can put invalid values in and then updates the Text property of the other TextBoxes
I have text boxes but update the label.Text property instead.
It gets more messy (at least I found it so) if you have numeric updowns to get values
You can call a method to update the label in the change events for the controls.
For more detail, please supply more detail.
this is off the top of my head but should get you pretty close...
private void taxChanged(object sender, EventArgs e)
{
updateTax();
}
private void updateTax()
{
// the rest of your logic, checking state, etc.
//
this.Tax.Text = aValueCalculatedInYourLogicAbove;
updateTotal()
}
private void updateTotal()
{
// sum up whatever fields need to be summed
//
this.Tax.Text = aTotalValueCalculatedAbove;
}