instead of jumping to the beginnng/end of the ListView, I would like to perform some other operation. So I would like to disable jumping through the ListView with PageUp/PageDown and perform some other operation.
I thought I could do sth. like
ListView.PreviewKeyDown
and do a
e.Handled = true,
but it is only possible to set
e.Handled = true
in
ListView.KeyUp.
But when trying to catch PageUp/PageDown in
ListView.Keyup,
the selection has already jumped to the beginning or end.
So how can I disable jumping to the beginning/end and perform instead an other operation?
Use OnKeyDown instead.
However, I have to advice against changing something basic like this. Users expect a certain thing when pressing page up / down inside a list view. Overriding this behavior makes it harder for your users to use your application.
Related
I need to find something that is the opposite of IsNullOrEmpty?
Here's the text line.
{
while (string.IsNullOrEmpty(TextBox.Text))
}
What is the opposite of this?
{
while (!string.IsNullOrEmpty(TextBox.Text))
}
In the context of a Winforms TextBox control, you don't have to test the .Text property for null at all. Sadly, I can't find the documentation on this, and I fear it was a casualty of the mdsn => docs move, but I have verified it in Visual Studio.
You can verify it, too; try it. Make a simple project with a textbox, and set it's Text to null in code. Then check in the debugger, and you'll see it's actually set an empty string. The TextBox control is just nice that way, and so are most other stock Winforms controls.
With that in mind, the opposite of this code:
while (string.IsNullOrEmpty(TextBox.Text))
can reduce down to this:
while (TextBox.Text != "")
though you may want to also consider whitespace, which would be one of these two lines:
while (TextBox.Text.Trim() != "")
while (!string.IsNullOrWhitespace(TextBox.Text))
But now I need to bring up another point: none of these options does an Invoke on the control, meaning this must be running on the UI thread. It's very bad to run a while() loop like this on your UI thread. You're counting on the user to break the loop, but in the meantime you've blocked the entire user interface, where the program will have a hard time even processing the input that would eventually unblock it.
Instead, you need to handle a keypress event, or validate the textbox at the point where the user tries to initiate an action depending on the textbox value, or move this to another thread.
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.
I've got a list of illegal positions and characters that can't move from those positions. How would I prevent those from being modified in the TextChanged event? Every solution I've come up with has been extremely hacky and unreliable, or has relied on the KeyDown event, which doesn't prevent the user from deleting the read-only text in other ways (select the text and delete it, or just press the backspace key).
I've thought of doing something like this:
//CharPos is a class with an int (CharPos.Position) and a char that
//should be at that int's position (CharPos.Ch)
foreach (CharPos p in IllegalPositions)
{
console.Text = console.Text.Remove(p.Position, 1);
console.Text = console.Text.Insert(p.Position, p.Ch.ToString());
}
But it completely messes up and goes on an infinite loop. And even if I stopped it from doing that, it wouldn't work if you changed the character count of the text, by using the delete key for example. Maybe I could use regex somehow? (I dunno, I have no experience at all with regex).
Can you combine multiple TextBoxes (for editable parts) with TextBlocks (for readonly parts)? You would just need to play with styling of the two to match
Could you please check the link below:
Disabling or making it readonly a part of the values in a Text Box (.net)
rtBox.Select(0, (rtBox.Text = "I am fixed content").Length);
rtBox.SelectionProtected = true;
rtBox.AppendText(Environment.NewLine);
Goal:
Don't want the user to use the X mark (upper right of the program's screen) to exit the program.
Problem:
Don't know how to remove that icon that allows user to exit the program?
You can set the ControlBox property to false if you don't want this to display. This will also remove the minimize and maximize buttons, mind you.
I would also ask you to consider why you want to remove this button. Sometimes it makes more sense to override the OnFormClosing method and optionally set Cancel to true under certain conditions (e.g., e.CloseReason == CloseReason.UserClosing).
myForm.ControlBox = false;
Apparently I have to have at least 30 characters in my post, so I will say that I am assuming WinForms since you do not specify yourself. Also note that setting this property to false will remove the minimize and maximize buttons as well.
myform.ControlBox = false;
does it for me
This guy seems to have figured it out:
http://blogs.msdn.com/b/atosah/archive/2007/05/18/disable-close-x-button-in-winforms-using-c.aspx
I used this and its working for me
this.ControlBox = false;
I have a dialog with loads of control in it. Each and evey control will be populated during the loading sequence and it might take a while for it to get completely filled. Mean while, I don't wanna allow the user to click on any of the controls. In other words, I wanna disable the control from receiving the events and I don't wanna disable the controls (as it might look odd).Also, I don't wanna subscribe and unsubscribe for the events regular intervals. Is there any way to stop the controls from listening to the events for a brief time ??
Sudarsan Srinivasan
The whole point of disabling controls is to communicate to the user that the control cannot be used at a particular time. This is a convention that users have learned and are used to, so I would advice to follow that. Not doing that may confuse the users.
The easiest way is to disable the container in which the controls are located in, rather than disabling each and every control. A better way (or at least the way that I prefer) is to have a method that will control the Visible and Enabled properties of controls based on which state the UI is in.
The easiest way is to move the control population out of the load event (if possible). Then in Load do something like:
private bool _LoadComplete;
void OnFormLoad(Object sender, EventArgs e)
{
_LoadComplete = true;
InitializeControls();
_LoadComplete = false;
}
void InitializeControls()
{
// Populate Controls
}
void OnSomeControlEvent()
{
if (_LoadComplete)
{
// Handle the event
}
}
Edit A Couple other Ideas:
Set the Application.Cursor = WaitCursor (typically will disallow clicking, but not a 100% guarantee)
Create a "Spinner" control to let the user know that the screen is busy. When loading bring it to the front so it sits on top and covers all other controls. Once you're done loading set it to visible = false and show your other controls
Unfortunately the only way i know of is to have a class variable (called something like _loading) and in each control handler do something like:
If (! _loading )
{
...
}
And in your loading code set _loading = true; once you have finished loading.
If you just want to disable user input, then you can set the form's Enabled property to false.
This has the effect of blocking user input to any of the form's controls, without changing the appearance of the controls; it's the technique used internally by the ShowDialog method.