c# application with on-screen keyboard - c#

I'm making a Windows form application in c# and im creating on-screen keyboard for this application and i have some problems with that, because I don't know how to do few things.
What i have
Form1 with few textboxes and few richtextboxes.
panel1 with richtextbox and 5 buttons (A, B, C, DEL, ENTER).
What I want?
Everytime I click on any textbox or any richtextbox I want panel1 to show up.
I want the text from the textbox i have clicked to be displayed in a richtextbox on panel1.
When I click "ENTER" button on panel1 I want the text from richtextbox on panel1 to be transferred to textbox i have clicked before. (and ofcourse panel1 would hide after that).
I already made panel1 to show up when I click on textbox or richtextbox (I've used "Enter" event) and panel1 will hide when I click "ENTER" button on it. Is there maybe any other way to show panel1 everytime I click on textbox or richtextbox without coding "Enter" event for each one?
Now the hardest part is transferring text from form to panel and vice versa. To transfer text from the clicked textbox I've simply added a line of code to "Enter" event:
textBox1.Text = richTextBox1.Text;
Transferring text from richtextbox1 back to textbox is my main problem... I have solution for that but im looking for a correct and simple one... my solution is too noobish and complicated...
Will explain here what im looking for:
For example if int can remember number and string can remember text. I want something which can remember which textbox i've last clicked. I hope u can understand what I mean.
My english is not my main language so please reply if u cant understand anything and i will try my best to explain.
Thanks everyone for your time.

I would do these:
Create a base user control that derives from Textbox (if you'll be using only textboxes) or from a Control if there will be more than the textboxes used.
Handle the "Enter" event just as you do now in that base class.
Create the class that will derive from the EventArgs class and that will store a value of yout control. Let's call it "TextboxArgs" for now.
In the base user control, create an event and raise it in the "Enter" handler that you have and use a "TextboxArgs" class form step 3 to pass the text as the event argument.
Subscribe to this event in your panel so it will react properly.
Now, just use this base user control that you have for each textbox and it will raise an event and pass it's value as an argument. Panel will respond to it. If you do it that way then you won't need any additional coding no matter how many textboxes you add.

Related

Making sure that a C# form has focus when it should

I am a fairly new to Visual C# but have coded in Delphi for a long time. I have created a form that has a simple panel that is hidden unless you type in a particular key on the keyboard (I do have "KeyPreview" set for true for the form and I am using the KeyDown event to handle determining if the correct key was pressed and to make the panel visible/invisible). Just beneath the panel is a webBrowser component.
What is happening is as follows:
When my form initially starts, I have code for the "Shown" event that makes sure the form has focus initially:
private void Form1_Shown(object sender, EventArgs e)
{
if (!Focused)
{
Focus();
}
}
When the form is focused at this point, my code for detecting the proper keystroke to get the panel to appear or disappear works fine.
Here is where things get strange and I'm not sure of what to do. There are two parts I am dealing with for what is wrong:
If I click on another form and then on the caption bar of my form again to get focus on my form and try a keystroke, the keystroke detection does not work. However, if I click on another form and then back on my form one more time, the keystroke detection for the form does work. What can I do to make sure that this works each time my from has focus again?
If I click on the web browser component within my own form, the KeyDown code for the form no longer gets enacted. Even if I click on the caption bar for the form, the KeyDown event does not work. What do I need to do to assure that, if a component within my form is clicked, my form will still respond for the KeyDown event?
Thanks in advance for any advice.
I can't say why your caption bar seems to be intercepting key events. It may be that various components on the form can have focus and thus capture keyboard events. I know the web browser control works this way. You may consider capturing keyboard events globally.
I saw something on Codeproject that shows how to do this. I hope this helps.
They use UserActivityHooks.
UserActivityHook actHook;
void MainFormLoad(object sender, System.EventArgs e)
{
actHook= new UserActivityHook(); // crate an instance
// hang on events
actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp+=new KeyEventHandler(MyKeyUp);
}
As for the webbrowser control, of course it is going to interecept keyboard events. Users often have to enter text in forms and developers often code javascript on webpages to specifically hook into keyboard events. So the webbrowser control must be able to capture those.

Add Items in ComboBox using Textbox Value from another form in C#

I have 2 Window Forms, Form1 and Form2. Form1 has a ComboBox and Form2 has a Textbox and a Button.
I hope you can help me with this one. What I would like to happen is, if I input a string value in the TextBox in Form2 and hit the Button1 that is also located in my Form2, the value of that TextBox will be an item for my ComboBox that is located in my Form1.
I just would like to ask if is there any way that we can do this? Could you provide an example for me? I'm looking forward to your help.
It would help a lot if you'd include what attempt code you have so far, and/or what obstacles you're encountering, but nonetheless this is a simple problem that's a bit difficult to search for, so hopefully this will help.
First, I'm assuming you've instantiated and displayed both forms. Next you'll want to find the Designer.cs (formNameHere.Designer.cs) file in your Solution Explorer, then locate the variable declaration for your combo box (should be near the bottom). Change its access modifier from 'private' to something more appropriate.
Now go back to your Form1 code file and add this to your button click event handler:
form2.comboBox1.Items.Add(textBox1.Text);
... where 'form2', 'comboBox1', and 'textBox1' are your child form, combo box, and text box from which you are sending the new combo box item.
Let me know if you have any further questions.
I think best option is to communicate between the forms through events. So on button click event trigger an event and subscribe to it from the Form1. When the event is raised, add the text to the combobox
In Form2:
internal event EventHandler<string> NewItemToAdd;
void button1_clicked(object sender, EventArgs e)
{
if(NewItemToAdd != null)
NewItemToAdd(textbox.Text);
}
In Form1 subscribe to the event NewItemToAdd and add the text to your combobox when the event is raised

WPF TextBox stealing focus after neighboring CheckBox is clicked

I've had an annoying issue with focus for controls on a ToolBar in WPF. My toolbar has a CheckBox and a TextBox control next to eachother. If you click and edit the text in the text box, then click the check box, the text box steals the focus back after clicking the check box, and the check box state is not changed when the user clicks on it.
I have similar issues with text boxes all over my application, but I believe this is the simplest case to explain, and I hope that the problem will be a common issue across all my text boxes.
Does anyone know what might be going on here?
Hook up a handler to the checkbox's Click event (or even the PreviewMouseUp event), and set the event's Handled property to 'true'.
Did you try that?:
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
MyCheckBox.Focus();
}

How to tell when the user has clicked outside of the bounds your control?

I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}

Can I fire a Text Changed Event for an asp.net Text Box before it loses focus?

I have an asp.net TextBox in which I want to check if the text entered into the TextBox is > 0. It works once I tab out or click out of the TextBox, but if I keep focus on the TextBox, it won't fire the Text Changed Event, so I have the following scenario, I want to enable something if and only if the TextBox.Text.Length = 0. Now, if I put my caret in the TextBox and delete all the characters and then leave the caret in the TextBox so it still has focus and take my mouse and click a button, it will not do what it was supposed to do because it never fired the Text Changed Event. How would something like this be handled?
friend, keyup, keydown and keypress are your friends
The best idea is to write some client-side javascript to do what you want. The TextChanged event handler requires a postback to the server, and posting back to the server before a text box loses focus is impossible. Unless that is what you intend, I would suggest the former.
you can also use the setInterval javascript method to check for a change in the value of the textbox on a timed basis. just remember, you need to use the form name followed by control name and value to reference the control.
setInterval(MethodName, 100);
function MethodName()
{
if(formname.controlid.value.length > 0)
{
//do something here
{
}

Categories

Resources