Update Label Without Button - c#

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;
}

Related

ASP.NET c# increase date value by number days on each button click

I looked on here but I have not been able to find what I want to do.
I simply have a onclick event for a button, and I want to increase the date value of a label by any number of days each time I click the button.
So lets say that number would be 2 days. If the current value of the label is 5/1/2016 when I click the button it should be 5/3/2016, and if again 5/5/2016 and so on. I can get it to do update once on the first click but not on a second click. Here i my code
protected void NDateOn_Click(object sender, EventArgs e)
{
lblCurrentDate.Text = DateTime.Today.AddDays (2).ToString ("dd");
}
I know it has somthing to to with the "Today" but I am not sure what to do from here
I appreciate any help in advance.
The problem is that you are adding 2 days to TODAY's date every time. What you want to do is add 2 to the date already stored there. You would want to do something like this (warning: have not tried compiling this).
lblCurrentDate.Text = DateTime.Parse(lblCurrentDate.Text).AddDays(2).ToString("MM/DD/YYYY");
The parameter passed to ToString() formats the datetime. You can modify that to get different formats. If you want to only store the days and not the month/year then you might need to do some more work. Hope this helps.
You need maintain the data in viewstate, each button click increment by 2
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["datacount"] == null)
{
ViewState["datacount"] = 0;
}
ViewState["datacount"] = ((int)ViewState["datacount"]) + 2;
Label1.Text = DateTime.Today.AddDays((int)ViewState["datacount"]).ToString("dd");
}
I hope this may helpful for you
Note : viewstate data can maintain in same page only, can maintain in multiple pages

How can i make to label return values in C#?

So guys i am new in C# and i want to know how to make to label return some values.
One example :
Label1 will be 75 dollars when i click in radiobutton.
When i choose the radiobutton, will change the value of 0 to 75 dollars in the painel,and will be added to the value of the buy.
I already tried some stuffs but didn't worked,i am really freaking out with this.
Please help, I need to do that for the Course of Programming.
You can use Text property for set and get value from label, such as:
int myIntVariable1 = 934;
myLabel.Text = myIntVariable1;
int myIntVariable2 = Convert.ToInt32(myLabel.Text);
Now myIntVariable1 and myIntVariable2 will have 934 value
A label cannot return a value,
if you're using a radio button \ button group, you could need to fire an CheckedChanged event, easiest way would be get the radio buttons on your form, double click for the event to get generated, then use a switch statement to have the label changed
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Text = "your new value or text here";
}
Also, just as a note of interest for you and the course you're doing, sign up to PluralSight which has mass of courses, and will teach you all you need to know about the fundamentals of C# and much more. You can get a free 10 or 14 day trial and then you can sign up from $29 per month and cancel at any time.

Getting a value before it is changed

I am having trouble with getting a value in a text box before the value is changed either by user input or programmatically.
I figure it has to do with the _TextChanged event but once this executes how do I get the old value which was already there before the change?
For example number 3 is in the text field. The number then changes to 4 how do I save 3?
Thanks in advance!
Do not store anything in the TextBox. Use it only to display/edit value.
private string value;
private void textBox1_TextChanged(object sender, EventArgs e)
{
// at this moment value is still old
var oldValue = value;
value = ((TextBox)sender).Text; // text1.Text
// here you have oldValue and new value
}
Maybe you don't want to update value after each TextChanged. Maybe you will do it when form is closed, some button is pressed, etc.
Anyhow idea should be clear: if you need to have old value, then store it somewhere yourself (as a variable, as a Tag, as application setting, exporting, etc.).
Also, take care about how user will work. If TextBox contains 3, then user may first press 4 and only then delete 3. Or user may accidentally press 54. Which of those will be old value: 3, 34, 54 ? Simply to demonstrate you, what there could be consequent problems to having concept of old value. Mayhaps you don't want it and there is better solution to what you are trying to solve by getting old value.
Why not save the current value in the textbox in a variable during the textchanged event. That way, it is the "previous" value when the event is fired again.
You can save the value of textbox in some variable.
For example
string currentVal = txtName.Text;
string currentVal can be used as a previous value in text_changed event.
Have you tried making a timer. Every tick you write the input to a string. If the input of the textbox is not equal to the content in the string, you trigger what you wanted to do. That way you can trigger a void with the old input.
private string oldInput;
....
void TimerTick(Object source, ElapsedEventArgs e)
{
if (oldInput != MyTextBox.Text) MyVoid(oldInput);
oldInput = MyTextBox.Text;
}

ScintillaNET Autocomplete and CallTip

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

Second TextBox showing same Text Selection as first

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);
}
}

Categories