How do I add functionality to a winforms program in C#? - c#

I'm trying to make a winforms version of a program I originally wrote as a console app.
Here's a picture of my current progress. I've made the text boxes and buttons (with a read-only textbox for output). I don't know how to actually make the buttons function.
I want it to work the same as the console version did (ask for input w/ error messages for anything that isn't an positive integer, and calculate the output), but I've never done winforms before so I'm at a roadblock.

As a beginner to WinForms, you would benefit greatly from reading up on the subject. For example:
Windows-Forms-Programming-Chris-Sells (Amazon.com)

Just double click the button and it will take you to the code behind page.
You can do something as simple as add a textbox (TextBox1) add a button (Button1)
Double click button on your winform and it will take you to the click event of the button.
In the code behind type this:
TextBox1.Text = "Hello World!";
Now run your app and click the button.

Related

Get the input of the Windows emoji-picker

I made my own textbox in UWP, now I want the user to add emojis from the Windows emoji-picker window.
The problem:
When I click any of the emojis with my application focused nothing happens. I check the events that could get triggered by that but non of them got. I also tried it with the ASCII-emojis, but they also don't work.
I checked the CoreWindow_CharacterReceived event and the Paste event, but neither gets fired.
The actual question:
Is there any event I can use to get the input of the emoji-picker or is there a different way?
Github-repo:
https://github.com/FrozenAssassine/TextControlBox-UWP
When I click any of the emojis with my application focused nothing happens.
The problem looks your TextBox is not in focus state, you could focus textbox manually after show Emoji pikcer. Then it will input emoji correctlly.
CoreInputView.GetForCurrentView().TryShow(CoreInputViewKind.Emoji);
YourTextBox.Focus(FocusState.Programmatic);
Update
For customing textbox you could refer to official text editor code sample that implement textbox with CoreTextEditContext.

WPF TextBox events TextChanged PreviewTextInput do not fire

I have inherited an bigger C# Project from a college (hes gone so I can't ask him anymore). Im on VS2012 + .Net4.6
The first time in this Project I added a WPF Textbox to a nonmodal Window.
The problem is: I cant change the text in the TextBox.
What I found out are the following facts:
The TextBox is not readonly
I can use backspace- and delete key, no key else
contextmenu with Paste, Copy an Cut works like expected
If I open the window modal, I can change the text in the box
If I open den window nonmodal I can catch the events PreviewKeyUp, PreviewKeyDown, KeyUp and KeyDown
but I cant catch PreviewTextInput, TextInput (thats normal) and TextChanged.
When I create a WPF-Project from the scratch, everything works.
We have an own WPF-Window-Style, but it doesn't matter, if I use this style in the window or not.
Can anybody tell me whats the difference of the behavior of TextBoxes in modal / nonmodal Windows. Could you give me a tip, where I could look for a Modification of the behavior between the FromTheScratchProject and our project? Can I debug the eventhandling somehow?
Thank you a lot
It seems like your project is a mix of WinForms/Win32 and WPF.
If you instantiate your window in following way, your non-modal window will catch the keyboard events correctly:
Window wpfWindow = new Window();
ElementHost.EnableModelessKeyboardInterop(wpfWindow);
wpfWindow.Show();

Mouse click in c#

I want to simulate a simple left mouse click that would be "pressed" after 5 seconds I clicked a simple button form.
enter link description here
I tried that way but it didn't work. Couldn't things be simplier?(sry I'm new here)
Unfortunately for a relatively low level operation like this, you'll have import the user32 WinAPI dll and do exactly what the answers in your link suggested. The only simpler thing to do is to use a library someone else built that wraps around the WinAPI, like http://inputsimulator.codeplex.com/ as suggested in this answer:
https://stackoverflow.com/a/15146334/3063835
It's not 100% clear from your question, but this is all assuming you want an actual "click" message to be sent to your form. If you just want to simulate a click event on a control in your form, you can always just call the click event handler when appropriate!

How do I conserve program functionality and code after moving all controls into a tab control?

I had a working program (Windows Forms Project) complete with buttons, labels, textboxes e.t.c. and the underlying code.
In an attempt to shortcut my work, I decided to add tab controls and move everything in my main form into tab 1 (cut and pasted).
As you can imagine it didn't work. I then got rid of the tab conrol and pasted everything back into the main form but the program doesn't work anymore.
Can someone tell me what's wrong please?
I'm working in MS V studio 2008 express
Thanks.
I have done this many times, but I usually just drag them into the TabControl. Maybe in the cut and paste operation your controls have become unwired from the event declarations.
The event handlers that you coded are still there. However, they are not associated with the control any more. I'm not sure if you're using VB.Net or C#, but the fix is the same - it's manual and tedious if you have a bunch of controls, but not too difficult. Here are the instructions for fixing a single button control, and you'll have to apply the concepts across the board.
These instructions are specific to C#. I can give you VB instructions as well as I've done this plenty of times.
Double click on the button to generate a new event handler. If the button is named Button1, the original event handler was probably called Button1_Click. Now it should be Button1_Click1.
Delete the Button1_Click1 function and compile. You'll get errors and if you doible-click on the error in the error pane it will take you to the form,designer.cs file to a line that looks like:
this.Button1.Click += new System.EventHandler(this.Button1_Click1);
Change this to
this.Button1.Click += new System.EventHandler(this.Button1_Click);
to point to the previously existing event handler, and the event handler will be fixed.
Possibly some of the events had code lost.
If you do it again it will probably work.
For an alternative method see my message

Control.Enter event doesn't fire when switching tasks. Is there an alternative that does?

Rep steps:
create example .NET form application
put a TextBox on the form
wire a function up to the TextBox's Enter event
When you run this application, the Control.Enter event fires when focus first goes to the TextBox. However, if you click away into another application and then click back into the test application, the event will not fire again.
So moving between applications does not trigger Enter/Leave.
Is there another alternative Control-level event that I can use, which will fire in this scenario?
Ordinarily, I would use Form.Activated. Unfortunately, that is troublesome here because my component is hosted by a docking system that can undock my component into a new Form without notifying me.
What are you trying to do in the Enter event?
I can't find another control-level event that fires in your example program but when my test app does regain focus, the control that last had focus still has it.
Interesting question but it needs a little more context.
If I try your example and click outside the control on another window, desktop, etc, I can get the Got and Lost Focus events to fire, but if you're only trying to click within a form or a control with only 1 control, these event will never be fired because it is the only thing to focus on. Neither will Entered or left, unless you change the dynamics or overload the controls, you cannot get this to happen
In your example, I think you need another control. The reason being is that the first control (tabIndex 0) is the one with focus. With no other control to switch focus to, this control will always be focused, and therefore can never be entered. Switching to another application or form will not change the focus or active control in this form so when you return you will still not get the event fired.
With added controls control.entered should work fine. If this is your only control, why not call the event on formLoad, or TextChanged, when the form gets focus?
Thanks, I'll give some background.
My control is a UserControl that contains a grid and a toolbar. A user will typically launch several of these controls to view different slices of the system's data.
There are several keyboards shortcuts that can launch actions from the selected row in the current grid. However, it is a requirement that these keyboard shortcuts should apply not only to the currently focused grid. If the user is currently focused on one of the many other areas of the application, then this keyboard shortcut should still work, and it should be routed to the last focused grid.
So I wired a function to the Control.Enter event of my UserControl to basically say LastFocusedGrid = this.
And it would work, except for the docking and undocking...
See, these controls are hosted inside an application with docking features, somewhat similar to visual studio.
By default, the control launches as a tab within the main working area of the application, similar to the way a source file opens in visual studio.
However, the user can "rip out" a tab by grabbing the tab header and dragging it out of the main application. At this point, the application creates a new "float form" to host the control. Switching between the main application and this float form is the same as switching between apps, for the purposes of the Control.Enter and Form.Activated events.
At that point we have the "one control within a form" scenario simulated with the example application described in the original post.
Now, there are some ways around this. I could leverage the Form.Activated event, which DOES fire when switching between forms. If you add an event in the test application to the Form's Activated event, you will see that it works great.
The problem is that my UserControl's relationship with its parent Form is fluid, making the solution somewhat complicated. I tried wiring up to "this.ParentForm.Activated" which worked okay. The problem is when do you call this? What happens when you are undocked/redocked? I ended up with a nasty bunch of code with things like "previousParentForm" so that I could unhook from the old form, and then I was still facing the problem that the docking system doesn't notify me when my parent Form is being changed, so I was going to have to make a bunch of changes there, too.
These problems are not unsolvable, but if there is a simpler control-level "parent form was activated" event, then that would be a lot more elegant.
That's rather long, but I hope it clarifies the situation.
So when creating your grid, can you not set the KeyPressed, or KeyUp, etc. event? If so, all the grids can make use of the same event handler. Just make sure that when you get into the event handler to do something like:
Grid currentGrid = (Grid)sender;
Then you should be able to apply that block of code to any grid that gets sent in without having to worry about keeping track.
Since all the event handler really is, it's location is a mute point really as long as everything you need to execute it is accessible.
Frye, the problem is that the keyboard shortcuts should work no matter where the user is in the application. They are gloabl commands, handled at the top level, and then routed to the "last focused grid."
So handling the keystrokes at the grid level will not help.
To be more specific, assume user launches grids A, B, and C. But he also launches other controls X, Y, and Z that have nothing to do with my code.
User clicks on A, then on C. Then he clicks on Y, then on Z. With focus on Z, he hits my keyboard shortcut. In this case, grid C should respond since it was the last grid the user was focused in.
It sounds like the issue that you're having is not directly related to the Enter event and more to the point, if you have controls "that have nothing to do with your code" then you really aren't looking at a control level event.
Guess I wasn't clear.
My control lives in a container application. So do other unrelated controls by other teams. Think of it like visual studio -- my control is the code editing tab, but there is also the pending changes list and the properties window, which cohabitate with the source files but aren't directly related.
The keyboard shortcut is handled by the container application. Then it should be routed to the last one of my controls that the user was focused on.
Maintaing this "LastFocusedGrid" reference is what I do in the Enter event.
If you want to see similar functionality at work in visual studio, try this:
open a few source files
navigate to the "Start Page" tab.
Hit Ctrl-F and search "current document" for some string
Notice that the serach feature auto-navigates to the LAST FOCUSED source file to perform the search.
So even though you weren't focused in the source file, the ctrl-F command was processed by visual studio and routed to the last focused source file tab.
Now try the same thing with Ctrl-G. It doesn't work unless you are focused directly in the source file.
My keyboard commands need to work like Ctrl-F here, not like Ctrl-G. That is why I don't just capture the keyboard events directly in my control.
Does that clarify or make things worse?
Have you tried just a simple Control.GotFocus?
in this example if you toggle between clicking the textboxes neither the enter or got focus will do as expected, however if you click the child forms instead both will behave as expected.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace EnterBrokenExample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form Form1 = new Form();
Form c1 = new Form();
Form c2 = new Form();
Form1.IsMdiContainer = true;
c1.MdiParent = Form1;
c2.MdiParent = Form1;
c1.Show();
c2.Show();
TextBox tb1 = new TextBox();
c1.Controls.Add(tb1);
tb1.Enter += ontbenter;
tb1.Text = "Some Text";
tb1.GotFocus += ongotfocus;
TextBox tb2 = new TextBox();
c2.Controls.Add(tb2);
tb2.Enter += ontbenter;
tb2.Text = "some other text";
tb2.GotFocus += ongotfocus;
Application.Run(Form1);
}
static void ontbenter(object sender, EventArgs args)
{
if (!(sender is TextBox))
return;
TextBox s = (TextBox)sender;
s.SelectAll();
}
static void ongotfocus(object sender, EventArgs args)
{
if (!(sender is TextBox))
return;
TextBox s = (TextBox)sender;
s.SelectAll();
}
}
}

Categories