Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm making an app with the UWP and I need a way to detect the arrow keys.
I know that exist other methods like using " Window.Current.CoreWindow.CharacterReceived", but this one do'nt detect arrow keys. Some help pls?
Thanks
The solution depends on the view and the UI controls that it contains.
Basically, a UI Control has the KeyDown and KeyUp events to which you can subscribe and control the key that was pressed/released on the keyboard.
You need to keep track of the focused object on the view as depending on which one has it, the event handler may not be called:
By default, the first focusable element in the visual tree is given
focus by the system. An individual control gains focus when the user
clicks or taps directly on that control in the layout, or uses the Tab
key to step into a tab sequence within the content area. You can also
focus controls programmatically by calling Control.Focus.
So for example register to the Window's KeyDown event:
Xaml
<Window x:Class="WpfApplication25.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
KeyDown="Window_KeyDown">
</Window>
CS
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// ... Test for F5 key.
if (e.Key == Key.F5)
{
this.Title = "You pressed F5";
}
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to create a WPF App in VS which should have various features (no serperate applications!).
The MainWindow should containt several Icon's (like App Icon's on Smartphone) which represents each of my features. So the MainWindow should be as an overview of my features. How do I need to realize it?
Which toolbar object represents an Icon? (Button?)
If I click on the Icon (Button?) the new feature should be showed as fullscreen (like on a Smartphone by clicking/starting an App). Which feature of WPF should I use to show the Page/Window of the Feature? If I close the feature the MainWindow should be showed again. I would be glad about a code example
Thanks
This question is likely too broad to get you the answers you are looking for, but a good place to start is with the Window class.
Once you create your buttons, you can spawn a new Window for your distinct feature. The Window.ShowDialog method will allow the new feature to remain active until it is closed. Setting the Window.Owner property will allow the new feature to return control to MainWindow.
I'd recommend trying some of these features out then coming back with specific questions you might have.
MainWindow.xaml
<Window x:class="MainWindow">
<!-- ... -->
<Button Click="Feature1_Click" Content="Open Feature 1"/>
<Button Click="Feature2_Click" Content="Open Feature 2"/>
<!-- ... -->
</Window>
MainWindow.xaml.cs
public class MainWindow : Window
{
//...
private void Feature1_Click(object sender, RoutedEventArgs e)
{
new Feature1Window
{
Owner = this
}.ShowDialog();
}
private void Feature2_Click(object sender, RoutedEventArgs e)
{
new Feature2Window
{
Owner = this
}.ShowDialog();
}
// ...
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Specifically, I want to be able to launch my WPF (C#) application and then immediately be able to switch between two radio buttons using the arrow keys.
As it is, I need to use the Tab key or the mouse to "select" one of the radio buttons, and then I can start hopping between them using arrow keys.
Important note: I am not asking how to control which radio button is checked by default. I'm asking how to how to optimize keyboard user-friendliness of my WPF controls.
You can use the Focus () function to choose the item selected when the window opens.
public MainPage()
{
InitializeComponent();
Button1.Focus();
}
And you can define TabIndex orders to optimize the user experience.
In Xaml :
<Button x:Name="Button1" TabIndex="0"/>
<Button x:Name="Button2" TabIndex="1"/>
<Button x:Name="Button3" TabIndex="2"/>
Or in C# :
Button1.TabIndex = 0;
Button2.TabIndex = 1;
Button3.TabIndex = 2;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've just started building up a simple game in Visual Studio using c# (It is my first low difficulty game). As I open it , it shows me a background image and 2 buttons, "Play" and "Instructions."and I want the form to get changed as I click the play button and open a new menu interface , like : " Game Difficulty / Character Appearence "(those 2 are buttons) , with a new background image. When I click one of them, another menu, etc.
Can I create multiple designs( Form1.cs[Design1/2/3]) for my from and swap between them ? If not, then how can I do something similar ?
In my opinion, the easiest way would be to make each menu a "User Control" that is the size of the main form, and then you can add a user control for whichever menu you want to show up as you go along.
For example, in the solution explorer I would create a folder and name it "Menus". Then I would create a new "User Control" in there. Copy the Width and Height of the form it will be placed on and apply that to the new control. Then you can add in all the buttons and images you like. Once you're finished, in your main form, you can add the "main-menu" control you made in the designer. For any submenus you want, create user controls for them too. Then, if you want a submenu to show up, capture a click event for the button you want and programmatically add it to the parent form using FindForm().
Pretend that DifficultyScreenControl is the screen that shows up after I click a play button located in my main-menu user control.
Inside of a menu user control
private void PlayButton_Click(object sender, EventArgs e)
{
DifficultyScreenControl DiffScr = new DifficultyScreen();
this.FindForm().Controls.Add(DiffScr);
}
Also it is a good idea to either Dispose() or remove these screens one they can't be revisited again as it can free up resources being taken up by them.
Feel free to ask me questions or comment on things to change!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
My ultimate goal is to make Combobox un-editable. Currently when user makes any selection then ComboBox shows selection and highlights it, therefore user can right-click and cut and delete the text that appears. I have added keyDown method, which prevents user from command such as ctrl + c, ctrl + v and delete. But User can still modify highlighted Text using right-click 'cut', 'copy' and 'paste'. How can i prevent user from modifying the current selection?
Ultimate goal is to make Combobox un-editable.
You could subscribe to KeyDown event and set SuppressKeyPress to true for all other actions other than Ctrl+c and create new ContextMenu to disable all default behavior.
comboBox2.KeyDown += comboBox2_KeyDown;
comboBox2.ContextMenu = new ContextMenu(); //disable right click
void comboBox2_KeyDown(object sender, KeyEventArgs e)
{
if (!(e.Control && e.KeyCode == Keys.C))
{
e.SuppressKeyPress = true;
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've just written a new WPF UserControl. Now I want to try it out. What's the easiest program I can write to run it?
So far I have
static class Program
{
static void Main(string[] args)
{
var grid = new CorrelationsGrid();
}
}
But the application runs and closes immediately without showing my control on screen. Help!
In Visual Studio
Select File -> New -> Project...
Select WPF application.
Then add your user control to the project.
Finally in the XAML, add your UserControl to the MainWindow.xaml and it will handle all of the displaying for you.
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myuct="clr-namespace:namespaceofyourusercontrol"
Title="MainWindow" Height="350" Width="525">
<Grid>
<myuct:myusercontrol/>
</Grid>
Create a new WPF application, and put it in the Grid in the MainWindow.xaml. Of course this assumes "easiest" to mean "least hassle", rather than "minimalist".
The easiest way should be this.
Once you written your control and you built it, the control will be part of your 'Toolbox', that means a window where all the available controls are visible and grouped.
You can use Visual Studio to add your control, from the 'Toolbox' window, to your MainWindow, in an empty WPF project.
This will add few lines of code in your MainWindow.xaml file, about your control.
Now you can customize the properties of your user control, like any other control.