Detect a jump in track bar - c#

I am trying to detect if the user did not click directly on the slider but somewhere on the track bar where it makes the slider value jump by LargeChange property. I am trying to get the value the slider was on before the jump occurred. So for example, if the slider is on the value 40 and the user makes it jump to 140, I need to know the initial value before it was changed.
I know I can use variables to keep track of the previous values, but I have a lot of TrackBars so I will end up having a lot of variables which I am trying to avoid.
Is there some kind of event for the TrackBar I can use to detect the jump or get the initial value before it has been changed by the user? Right now, I'm using the MouseDown event, but that gives me the value of wherever I click on the TrackBar instead of where it was at.
Talking about this TrackBar (the slider being the thing you can move left and right):

A solution that jumps to mind is to use a dictionary. You don't want to have many variables, but a dictionary is only one variable. And I don't believe you having so many trackbars that the memory for the dictionary should be any problem.
So declare a member in your Form class like
private readonly Dictionary<TrackBar, int> trackBarValue = new Dictionary<TrackBar, int>();
And in the handler for the ValueChanged event of the TrackBars you can do
private void TrackBarsValueChanged(object sender, EventArgs e)
{
TrackBar clickedBar = sender as TrackBar;
if (clickedBar == null) return;
if (!trackBarValues.ContainsKey(clickedBar))
trackBarValues[clickedBar] = clickedBar.Value;
if (Math.Abs(trackBarValues[clickedBar] - clickedBar.Value) > clickedBar.LargeChange)
{
// yes, it was a big change
}
// store the current value
trackBarValues[clickedBar] = clickedBar.Value;
}
I don't know if you currently have a different handler for each single trackbar. Since that would mean to repeat that code in every handler, it is possibly a good idea to add this single handler to the events of all trackbars.

See here for how to execute code when a variable changes.
Try something like that, but have the code set something like OldVar equal to TrackVar2, TrackVar2 being the value of TrackBar1 (your actual trackbar int) but only update it after OldVar has been updated first.
This should give you what you want, there is likely a way better solution but this should work.

Related

C# Increment for loop every time button is pressed

I googled a few things before posting, but I couldn't find anything like this. Basically, I want to take text from a textbox, save as a variable (say history1) to then be able to call that in the future to display the text. I can do that, but what I'm stuck with is that I want 3 variables (history1, history2 and history3, for example) and each time the button is pressed the string is moved to the next variable.
For example, the button is pressed, the text is saved as variable history1. The text is changed and the button is pressed again, the text from history1 is moved to variable history2, and the new text is saved as history1. This would only need to work for 3 instances though, not infinitely, so when text is stored in history3 and the button is pressed the text is just overwritten.
The way I had thought of approaching this was:
string history1;
string history2;
string history3;
for (int i = 1; i < 4; i++)
{
history1 = txtOutput.Text;
btnToFile_Click()
{
history2=history1;
btnToFile_Click()
{
history3=history2;
}
}
}
However, this isn't going to work because the btnToFile_Click doesn't take any arguements. Is there an easier way to go about this or just a way to fix the method not taking arguements ?
Thanks in advance!
Make sure that you delcare history1, history2, and history3 on the form level (not inside any method).
Then, have the following code inside the handler of the click event of the button:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
You don't need to call the btnToFile_Click() method multiple times in your loop, just move the text from end textbox to another in reverse order. Nor do you need a loop because you only have three textboxes.
Why reverse order? So you move the value to the next textbox before it is overwritten by the new value.
So:
history3 = history2;
history2 = history1;
history1 = txtOutput.Text;
btnToFile_Click() is a Click event handler for btnToFile (a button). You're not supposed to call that method yourself, it's called by the UI framework (say WPF or WinForms etc.). By the way, it does receive a parameter, then event source (since you can assign the same event handler to multiple buttons and do something based on which one sent the event)
You can try saving in a string array and move the strings within it when you call the button clicked event

How to detect if the mouse is directly over an object type in WPF

I need to detect (true or false) if my mouse is over a specific type of object. There is one data template that the many objects use. I do not need anything from an instance of an object, I just need to detect if my mouse is above this type of element on the UI.
It would be something along the lines of:
If(mouse.DirectlyOver == StorageElementWrapper)
{
...
}
EDIT: my error is that I am using my type like a variable
It's important to note that DirectlyOver will very likely find something inside your element rather than the element you're actually looking for. To use this property, you'd want to look at the parent tree of the DirectlyOver element. Something along these lines, with FindAncestorOrSelf coming from this blog post:
if (Util.FindAncestorOrSelf<StorageElementWrapper>((DependencyObject)mouse.DirectlyOver) != null)
{
...
}
Or if you have code references to your StorageElementWrappers, (in this example, in a collection named myWrappers) and they derive from UIElement, this would probably be a better approach, using the IsMouseOver property:
if (myWrappers.Any(x => x.IsMouseOver))
// do something
I was able to put the MouseEnter event in the border of my data template. This template is bound to my objects. Instead of trying to determine if the mouse was hovering over the object before performing an action, the
object_MouseEnter(object sender, MouseEventArgs e)
{
if(....)
else
}
event fired each time an object was "entered into by the mouse" and I used conditional statements to decide how to handle the event.
Thanks for the previous suggestions regarding mouse events.
A concept if you are interested in a particular item: create an OnMouseOver handler for that particular object (alternatively OnMouseEnter). Generally in WPF things work this event handling way rather than imperatively enumerating and discovering where is the mouse pointer. So this way the item itself can tell you if the mouse is over it. The item can have a public readonly property which exposes that, and your code can just get the value of that property.

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

C# breaking out of a loop from separate class/method

I have a block of code that repeats using a "for" loop, and each loop constructs a form to display some text. Some thing like the shorthand code below.
Main()
{
For (int x: x<=20; x++)
{
createform(string[x]);
}
}
So for each loop a different string is passed to a method that will construct a form as below.
createform void (string input_)
{
...
code to build form and add a button "cancelbutton"
form.text = intput_
....
form.cancelbutton.Click += // and I want this to cause the original loop to end....
}
No I know I could use the button to make int x greater than 20 and that would end the loop, but I don't actually know what the max value will be as this is dynamic. Again I could work this out and do he same thing but it seems a bit "messy".
Is there a neater way to cause the button click to exit the loop. How about if the Createform method is in a separate class to main, does that make any difference?
If your loop runs from 0 to 20 (or even over 9000) the user won't be able to click the cancel button in time. In fact, since it is all on one thread, the loop will finish before the UI responds to the click, but maybe I have misunderstood. Could you just have a boolean flag which you check each time you enter the loop and set it to false once the user clicks the button?
Just add a variable and code the click event:
static bool clicked;
Main()
{
clicked = false;
For (int x: (x<=20) && !clicked; x++)
{
createform(string[x]);
}
}
public static void Click_Detector(object sender, EventArgs e) {
clicked = true;
}
Your routine would need to do something like this:
createform void (string input_)
{
...
code to build form and add a button "cancelbutton"
form.text = intput_
....
form.cancelbutton.Click += MainClass.Click_Detector;
}
You shouldn't be using a loop to create each form. Assuming you have a submit button. Every time the user clicks submit, you should explicitly show the next form. That way, if a user clicks cancel, you don't have to worry about the rest of the forms.
The first solution that comes to my mind is: return value of the method True/False and in foreach check for its return value, if False=> break.
Your psuedo code isn't indicating how you are showing the form in CreateForm(). Is this going to be a modal or non modal? It would have to be modal, otherwise, you'd just keep creating forms until the cows come home. Remember, adding a handler to the click button doesn't actually execute the handler method until the click button is clicked.
Presumably your click event handler could set the value of x to be greater than 20, but since you said you're not sure what the maximum is, you could have a do loop governed by a boolean flag, and your createform() could set the state of the flag.
I'd recommend rethinking your problem space, as this overall approach seems really convoluted. I don't understand your UI, but it seems like your intent is to have the user control the exit of an infinite loop, like those old RPGs that ask you "Do you want to go on an adventure Yes/No", and it would continue asking you until you hit "Yes". These are pointless and confusing.
Add a boolean to the class and have the click event set the boolean value. Then you can break from the loop. However, if your real world use is the question above I would agree with the answer given by mikerobi.
One good solution would be to use the CancelAsync() method of the BackgroundWorker class: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx
If you don't want to work with a separate thread to improve the performance, that I don't recommend, is to set a class level boolean value when the button clicked, then the for loop checks that boolean value in each next process.

Binding between controls in Windows Forms

Is there a way to make a control dependent on another control? I have a combo box and a button, and I need the button to be enabled if and only if there is an item selected in the combo box.
I know I can set the Enabled property of the button inside the SelectedIndexChanged callback, but then it will require some code, and besides there's an issue with what initial state the button would have. So I'm looking for something that wouldn't require manually handing events, is this possible?
Thanks!
No, there is no way in winforms to do this without code. What I usually do is to collect all such state-setting code into one specific method:
private void SetControlStates()
{
theButton.Enabled = theComboBox.SelectedIndex >= 0;
// code for other controls follow here
}
Then I trigger this method from all over the place, as soon as there is an interaction that may lead to the state changing (including the last thing I do when the form has finished loading; that takes care of initial state). If you want to avoid unnecessary assignments, just add code to check the value first:
private void SetControlStates()
{
bool buttonEnabled = theComboBox.SelectedIndex >= 0;
if (theButton.Enabled != buttonEnabled) theButton.Enabled = buttonEnabled;
// code for other controls follow here
}

Categories