touch screen clear status message on any touch - c#

Iam working on a touch screen windows form that has many checkboxes, textboxes, listboxes, date dropdown pickers etc. Depending on user action a status message is displayed at the bottom. For eg., Your profile saved successfully, From and to date cannot be same, Please select a valid ... etc
What is an elegant way to clear the status message on ANY touch.
if (statusLabel.text != string.empty )
statusLabel.text = string.empty)
Meaning if any checkbox is checked, any text is input in a textbox, any listbox or combo is selected...then I want to clear the status label. This way the last status message does not "stick" to confuse the user. I am poking around to see if I can override some event at the form level in one place that will do this.
thanks
thx Saravanan and Pedery for your suggestions. They do not solve my problem. I just discovered Reactive extensions and posting a related question which may help me. Left mouse button click detect on winform using Reactive extensions IObservable on events

Try to find an event in the statusbar itself like text changed or content changed etc. Override it to clear the content of itself.
You may write code to clear the status bar content on the change event of the control's container.
Its your choice.

You can put the message in the controls' Tag property and use a single common event to add them all up.
If you wanna be more orderly, you can subclass the checkbox with a custom property the same way.

This was the solution to my problem
protected override void WndProc(ref Message msg)
{
switch(msg.Msg)
{
case WM_LBUTTONDOWN:
//Do something here
break;
//add other cases if needed
}
// call the base class WndProc for default message handling
base.WndProc(ref msg);
}

Related

Is it possible to disable a control but still receive events for that control?

In windows forms I have a simple TextBox:
TextBox textBox = new TextBox() { Text = "text" };
textBox.Enabled = false;
textBox.MouseEnter += (object sender, EventArgs e) =>
{
MessageBox.Show("MOUSE ENTERED"); // this never fires if the control is disabled.
};
I want to disable the users ability to interact with the control and I want the control to be styled as a disabled control. But I also want to receive MouseEnter,MouseLeave, and Click events from the control so that I can change the background cover of the control on hover and respond to clicks on the control.
But as I have just discovered if you disable a windows forms control it disabled the events as well. I know with some effort I can accomplish the same thing by checking mouse coordinates globally but it would be a lot nicer if I could just have it disabled but still receive events for it. Is that possible?
Enabled doesn't really do anything in Windows Forms itself. It is a property of windows controls in general that a disabled window doesn't receive input messages (such as mouse events and keyboard events). So no, there is no way for you to disable a control and still receive those messages. Windows just don't work that way on Windows. It's not the TextBox control filtering those messages away - they don't come in the first place.
TextBox is a great wrapper around a windows common control. When you do something like tbx.Text = "Hello";, the TextBox just sends a message to that common control, saying "change the text to Hello". If you want to change that, you need to make the control essentially from scratch. You can make some hack that reverts whatever the common control does as response to a mouse event, but these usually don't work very well and tend to break down in unexpected ways.
In practice, what you really want is probably to tweak either the way ReadOnly behaves (e.g. disabling focus as well as making the control read only, but that's again just a dirty hack), or replace the TextBox with a control that can either be a control or a label - allowing you to switch between the two. If you want the text box to stop behaving as a text box, stop it from being a text box. Problem solved :)
I'd still reconsider using ReadOnly, though. Are you sure the user would not want to select text in the text box and copy it somewhere else? Or change the reading order?

How to get started with c# for testing keypressed event?

I am quite new to c#, and the first thing i want to do is being familiar with that environment by trying Key's Combination Events.
In particular, alt+k.
I'm working on Microsoft Visual c# 2010 Express.
I want to test if that code works.
If errors are found, please notify me :)
public void begin(object sender, KeyEventArgs ev)
{
if (ev.KeyCode == Keys.K && ev.Modifiers == Keys.Alt)
{
//display a message
}
}
but even if I know theoretically what are the different models of projects that are proposed when clicking on new project and what are their uses, I tried unsuccessfully several models to test that code.
In short, i don't know what models to choose and where to put code for testing that kind of simple code, and more precisely working on events(key+mouse) with a minimalist gui.
Someone could help me to tell me how to concretly get started with events stuff in c#?
Thks in advance :)
The MSDN Documentation has a good example of what you need to do. Here are a few important parts:
Start with a simple "Windows Forms" application. While there are many other types of applications, WinForms is the simplest to start with.
Wire up the KeyPress event. In the Form constructor, you need to tell it what to do when it gets a KeyPress event. If you change the name of your function begin in your question above to Form1_KeyPress (to more-accurately describe what it does), the following code should work:
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
Use KeyPressEventArgs instead of KeyEventArgs. It may or may not make a huge difference, but it is good to use the most-specific EventArgs when you can so you can use properties specific to it.
Pay attention to KeyPressEventArgs.Handled. If you have KeyPress (or some other keyboard events) on other objects on your forms, such as buttons or text boxes, you need to say whether the event was handled there or whether it should bubble up to the parent (in your case, to the Form).
EDIT (Thanks #RBarryYoung):
Set this.KeyPreview = true in the form constructor. In order for your form to receive keyboard events that happen on child controls (such as buttons and text boxes) as mentioned in the tip immediately above this one, you need to set this property to true to allow the form to get a first look at Keyboard events that happen on the child. (Note: of course, this tip and the previous one only apply if you want the form to see these events. Sometimes, you might want keyboard shortcuts to be trapped in the child control instead of being handled in the parent.)
It can help you:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.K && this.AcceptButton == null)
{
MessageBox.Show("Key K pressed");
}
return base.ProcessCmdKey(ref msg, keyData);
}
It will detect if key K is pressed in any control of your form and return msgbox
Test it and try make work your way.

Confirmation Box in SPItemEventReceiver class

I want to display a confirmation box in SpItemEventReceiver class using just C# or any script or plugin (e.g. jQuery) with it.
I have researched the problem and I believe that I can do something like build a string and display it on this item adding form but I read it in this SharePoint King blog post (I am NOT advertising them) that it's not possible if you search for the text. Here's an excerpt:
Is there any way we can show a custom message box when an event is
fired and continue with the event?
No, that is not possible from the method described here.
You can put alert on button click of the form by the javascript but
can not be done by an event handler.
I wonder if someone can confirm whether can or we can't do it, as I think we can but don't know how. Here's a piece of code I'm trying:
class className: SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
try
{
this.DisableEventFiring();
//Display a Message box here
[Edit:] Will the HttpContext.Response property work in this context?
Showing a message box won't work here; if it worked the message box would be shown on the server because that's where this code runs. What you could use server-side is the SharePoint
Page Status MSDN

Hide DropDownList in ComboBox

How to hide DropDownList in ComboBox?
I want use ComboBox only for displaying text.This control look nice, for me is better than TextBox plus Button. So control must be enabled, but without any items.
When user click arrow (or alt + down key) DropDownList should'n show, because ill select value from custom DataGridView in order to fill back text in ComboBox.
Edit. Alternative solution is set DropDownHeight to 1, with show only 1 pixel line after clicking control.
Edit. Real solution. Answer below
You can intercept the messages that cause the box to drop-down, in a subclass. The following snippet defines a control NoDropDownBox, that ignores the mouse clicks that result in a drop-down of the combo box:
public class NoDropDownBox : ComboBox
{
public override bool PreProcessMessage(ref Message msg)
{
int WM_SYSKEYDOWN = 0x104;
bool handled = false;
if (msg.Msg == WM_SYSKEYDOWN)
{
Keys keyCode = (Keys)msg.WParam & Keys.KeyCode;
switch (keyCode)
{
case Keys.Down:
handled = true;
break;
}
}
if(false==handled)
handled = base.PreProcessMessage(ref msg);
return handled;
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case 0x201:
case 0x203:
break;
default:
base.WndProc(ref m);
break;
}
}
}
You will have less trouble and create a better end result by simply creating a usercontrol with a textbox and button that is styled in the way you want. If you figure out a way to remove the functionality of the combobox, all you're really doing is creating unneeded complexity.
Maybe it's better to create a custom control once and use it any time you do need this functionality.
If you are working with Windows Forms maybe the easiest way is to inherit class UserControl and create your component using the visual designer writing a little code. You can also descend ComboBox class and code your own drawing logic but it seems to be requiring more work.
[Updated]
OK, you can't set the Combo Box to Read Only, but you can set Enabled = false.
I've never tried this, but perhaps you could set the MaxDropDownItems to 0.
But, yo'd still set the Combo Box's Text to the value you want in code.
[Edit]
Another idea: Set DropDownHeight to 0 (...or 1 if it won't accept 0).
If the DropDownStyle is set to DropDown and the Text is set to "Something" then your ComboBox won't dropdown when user clicks the button.
At least, I'm getting that behaviour in WinForms (C# 4.0).
Is that what you are trying to achieve?

How can I remove the input focus from textbox?

I'm making simple C# Winform application. There is a Form having a textbox. I want to change the location of textbox by arrow key but textbox has the input focus so form's KeyDown event is not called. How can I remove that input focus?
Typing on the textbox should still be possible. I try to make a dummy label and give the focus, but It doesn't work. If I press any key, the cursor go back to the textbox. please help me. How can I solve this problem?
Handle the TextBox.KeyDown event. And set e.Handled = true; in your handler after you move the TextBox, but before you return. And, yeah, only handle the arrow keys.
Hmm, not sure if I understand. If the user can type into the edit box, then it can have focus. If he clicks outside of it, on a blank are of the form, then it loses focus.
If you want to be able to 1) type into the edit box and 2) move the edit box, then you need a separate mechanism to enter "move mode".
I would suggest either a "click here to move selected control" button, or a right-click context menu on the control with a "move control option".
You would also have to conisder how the user indicates that moving has ended.
Hope this helps.
NOTE: I just realized this is not even an in-browser C# app. I guess disregard all of this. Serves me right for not reading carefully enough.
Use Javascript, in particular, I'd personally recommend jQuery.
They have pretty nicely documented their library: http://docs.jquery.com/Main_Page
For this particular task, you are going to want to bind some sort of key event (ie. keypress) and make sure to stop event propagation (so that you prevent the default response which is to be sent to be simply handled by the textbox element's default listener).
So, to give you an idea, if you want to change the location of the textbox using keypresses (maybe arrow keys), do something like this:
/* link the jQuery source to the HTML page with script tag */
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
/* short hand for $(document).ready(function() { ... */
$(function() {
$("#textbox_id").keypress(function(e) {
var $this = $(this); // store the #textbox_id element in $this
e.preventDefault();
switch (e.keyCode) {
// find the actual integer code for the up arrow
case UP_ARROW:
$this.animate({
top : '-=10px'
}, 100); // time in milliseconds to complete the animation
/* fill in the cases */
}
}
});
Okay, I hope you get the picture. Find out more about animate() and other jQuery functions at in the documentation at the link I provided above. Hope that helps!
NOTE: Obviously, preventing the default handling of events is a terrible idea in this case for accessibility reasons. Use your best judgement when selecting keypresses to trigger these moving events -- whatever you do, don't disallow users from moving around within the text they have in input fields.

Categories

Resources