Language change with KeyDown event on all Form - c#

I develop an application that can change language between english and hungarian. This software work with .resx files and when the user presses F2 the Labels, Buttons, and other controls their Text properties will be changed from the .resx file.
It's ok, but when I have more than one Form open, naturally the KeyDown event changes those controls that are in the focused Form. So my question is: how can I capture the F2 keystroke in all opened Forms?

You can probably use Events to implement this. I don't know which technology you are using. WinForms or WPF.
IF you are developing this functionality in a WPF technology with MVVM then you could have declared 'SelectedLanguage' property in a Class lets say 'Langugage' implementing INotifyPropertyChanged interface. Derive your other view models with this class and then on 'SelectedLanguage' propertyChanged event you can notify each and every view model about the change of language on which you can have a method to read resource files and change values of UI.
May be I am out of context with respect to technology. But Events mechanism or Observer Pattern will resolve your problems.

Note: Based on clues in your question I am assuming Winforms rather than WPF:
how can I capture the F2 keystroke in all opened Forms? You can't, not without some ridiculously complicated code. The closest you can come is by re-dispatching events in one Form to other forms. In any case, you need a way to track your open windows and when an F2 key is hit in any of them you need to tell all of your open forms to change the language.
Somewhere in your application where you create new forms you need a collection like List<Form> for example that will hold your list of existing forms. (You can also try and use built-in collections in your Control objects) Whenever you create a new form, you add to this collection, when one closes you should remove it. And when any of those forms captures an F2 key then you loop through the collection telling each one to switch its language.
This would be a great place for you to inherit from the Form class and add your own properties and methods to make the behavior uniform across all of your forms.
Here is an example article that uses delegates for inter-form communication.

Related

Cleanest Way to Have Multiple Views/Tabs Inside Single Form/Window

My goal is to make an application where, conceptually, everything happens in a single window - a principle most applications I commonly use exhibit, at least for their main interface. Installers are a good example of what I am trying to do, where you basically page through the interface.
You can do this fairly well using a (invisible) tabcontrol. This allows you to design the individual tabpages (which are basically forms as far as controls go) in the designer, and you can manually switch between tabpages, and it all takes place inside the same outer form.
The problem is, this results in code for everything all in the main form. What I want is basically the design capabilities of separate forms (drag-and-drop components, only has code for its own controls), but that can be put inside the tabcontrol to get the intended user interface.
What is the best way to do this? I assume there is a clean solution given how common this is.
Thus far I have followed C# Multiple Screen View Single Form, which works well aside from the fact that all the code ends up in the same class (for example, you need unique names for every single element on the form).
I think you're looking for UserControls. They let you create a "form without a window" if I had to simplify it.
Once you've built the UserControl, you can then drag and drop it into any Form.
You can then replace these controls at runtime as needed.
See this link for a decent starting tutorial. I'll update if I find a better tutorial out there.
http://www.akadia.com/services/dotnet_user_controls.html

Common Ribbon Control in Windows Forms

I want to develop windows forms application in which i want same ribbon control to appear on top with same Click Functions associated. Is there any solution to add same ribbon to all forms? I tried MDI, but i dont have requirement of multiple documents to be opened at same time. I tried adding same ribbon to all forms one by one, but then , i need to create click methods for all buttons in all forms seperately, that is tedious and time waste.
Also, i need to know any solution for smoothing transition between form change. When i use code:
Form f1=new Form();
f1.show();
this.hide();
This works correctly for switching between forms, but the problem is it creates a jerk on screen. So, is there any solution for smooth transitioning of forms?
You could create a base class that implements the ribbon. Just reuse (that is: derive from) that control for every form you have.
Create a container under the ribbon where you can put your actual form content in.
A problem might be that the designer often doesn't like this, so it might be quite some work to smooth thing out.

Why do we use UserControl?

ı have been serching this for a while but I couldn't come up with a conclusion. What is UserControl? For me we can do everything with creating new windows forms instead of User Control. I know there is a reason to use but it is not clear right now. If someone illuminates the mystery that would be great.
A user control is basically a grouping of other existing control, intended as a reusable component (i.e. composite control). If you need to place the same group of controls on different windows you'd rather group them in a user control, adding things like data validation for instance, and then reuse this control whenever you need it.
Here is some more reading.
UserControls allow you to reuse your code. For example if you need a small component that displays two values (code and description), with UserControls you can design it only one time and then reuse it in other forms.
Also, you can add your custom properties\methods to the UserControl; in this way you can define simple (or even more complex) functions associated to the GUI control.
Hope this helps.
imagine you have a GridView with some new methods you create, and which you want to use on several pages. There you go. A UserControl is useful. That's just one example
As the others have explained a UserControl groups 'real' Controls and the logic that makes them work together as one component.
Imagine an application where the user can decide wether it runs in MDI mode or with separate windows or with tabbed pages. You can add the UCs of your application to any of these easily.
Think of a MP3 player with various controls, buttons, labels and sliders and user drawn-gauges. If it's in a UC you can re-use it directly. If it is all on a window, how do you re-use it?
So UCs are about flexibilty and re-using visual components.

Transistion from Winforms to WPF MVVM

I have a program that at its heart is just a fancy Keyboard hook and wedge. The keyboard hook portion is a separate library from the main program. In the main program I gather all the keys that i want to monitor from a config file and instantiate the keyboard hook. The keys that I am monitoring have tasks that they can perform when the key is pressed and/or when it is released. (Tasks such as open a program, turn on a scanner..etc) Since the project started in Winforms I felt that it would be a good idea to make a folder in this library called Controls. I made a dialog Form that a user could select what task they wanted to perform. There has been a need for me to switch from Winforms to WPF. So it is nothing to add Winform controls to a class library. From trial and error it appears that doing the same thing for WPF user controls is a different story. So I decided it would be easier to just created a new WPF Usercontrol project. (If it is possible can you please leave a comment about how to?)
So I am new to WPF, and decided that this particular library would be good to use the MVVM pattern to get my feet wet. It is small in that I only have 6 subclasses of Type AbstractTask. Part of me is itching to use Abstract Task my Model. I'm not sure how yet, but I think that it is my Model. I'll have to add a description string field to it for my View, but that shouldn't be a problem. Since atleast one of my Tasks needs extra information (such as RunProgramTask) I figure I should also put a ?Action? in to "verify" that all the correct information is given. again.. that should be easy.
So in the end I have 2 questions.
Does my theory comply with the MVVM pattern?
Should I move all my Hooks and Tasks and such over to the new WPF project and delete the old project? Or should I just delete the Winforms controls out of my library and just imagine that it is one project?

C# Multi-panel/layer winform application

I've been designing a pretty complicated avionics application. The thing is, it has many menu buttons to be clicked (12 to be exact) and each one of them perform a different action. For instance, one could be a login panel and the other one a PDF reader. How could I organize this programmatically?
Currently, I've been setting each item in a panel and setting it to visible or invisible, according to the active or clicked item.
How would you guys do this?
Thanks in advance!
You might consider a FlowLayoutPanel, although I'm not sure how flexible it would be in meeting your requirements. If you set your panels up with docking properties, you should be able to manage.
I would also recommend using a UserControl to separate code and functionality. If panels need to communicate, implement the observer/observable pattern instead of subscribing to events between user controls.
Like IAbstract says, you should consider separating the different UI elements as UserControls. You can then do things like use a factory to construct them and add them to your window as required.
I've found this sort of approach, used with a Model-View-Presenter type pattern, works really well for WinForms apps with dynamic user interfaces.

Categories

Resources