How to set the blinking cursor to WPF Editable ComboBox? - c#

In my WPF program I have a standard ComboBox (Editable). My desired behavior is that upon FormLoad, I would like the user to be able to type into the ComboBox and select from the list. (At FormLoad I've already populated it with some strings.) So I set comboBox.Focus(), and since I have IsTextSearchEnabled="True", which is the default behavior, this works fine.
My problem is, when comboBox.Focus() is set, it does indeed focuses the control, but I do not get the blinking cursor inside it. This is what it looks like:
This in theory gets the job done, but I believe it's not very user-friendly. I would like the ComboBox to be focused, and also have the blinking cursor.
I get the blinking cursor when I click on the editable ComboBox, so I looked up what event gets fired when I do that. Turns out it's PreviewMouseLeftButtonDown, so I tried programmatically firing this event (although this is something I usually try to avoid) to see if that will be an option. This is my code:
comboBox.RaiseEvent
(
new MouseButtonEventArgs
(
Mouse.PrimaryDevice,
Environment.TickCount,
MouseButton.Left
)
{
RoutedEvent = PreviewMouseLeftButtonDownEvent
}
);
I used a Console.WriteLine() to print a simple message to verify in Output Window if the event gets fired, and indeed it does. However, I still do NOT get the blinking cursor in my ComboBox.
Then I looked through SO, and found this question and the OP's edited fix seems to work. However this seems like quite a bit round-about way to get a seemingly simple task done, so I'm wondering if there's a rather straightforward or simpler way that I can achieve the desired result.

Calling the Focus() method once the window has loaded seems to work just fine for me:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
this.Loaded += (s,e) => comboBox.Focus();
}
}
If it doesn't work for you, then please provide a reproducible sample of your issue: https://stackoverflow.com/help/mcve

Related

C# WPF add KeyUp event for window with a handler in another page

I'm working on a WPF project and I'm new to it. I organize my project like this:
MainWindow.xaml (Contains a NavigationWindow)
MainWindow.xaml.cs
HomePage.xaml (Contains a series of function)
Homepage.xaml.cs
The thing is that I want to add a KeyUp event to my program, and I want it to call a function in HomePage.xaml.cs when a key is pressed. But I found that it is impossible to add a KeyUp event to a Page object, thus I decided to add it for NavigationWindow. However, I cannot reach the function in HomePage.xaml.cs inside NavigationWindow, so I came up with an idea.
var window = Window.GetWindow(this);
((MainWindow) window).KeyUp += new system.Windows.Input.KeyEventHandler(this.Window_KeyUp);
However, this doesn't work and throw a System.NullReferenceException. I don't know why it happens. Maybe it wants to say that this is null in here? But why?
Though it is silly, I would like to answer to this question myself. (BTW, I haven't received many answers which upsets me. Luckily I found the problem)
The problem is about Window.GetWindow(this). It returns null, which clearly doesn't have a KeyUp event. The solution is to change it to Application.Current.Windows, which is an array of windows hierarchy. Thus Application.Current.Windows[0] can return the MainWindow object, which can be attached with the handler.

ToolStrip with custom ToolStripControlHost makes tab order focus act weird

I'm experiencing some strange behavior. Let me try to explain, I stripped my code down to the bare minimum and I'm still having the problem. So first of all, I'm using VS2013 with .NET 4.0 and I'm on Windows 8.1.
So I have a custom UserControl with a TextBox that's being used through a ToolStripControlHost, if I focus on this textbox and hit TAB, it only cycles through the controls to the LEFT of this textbox. If I have it focused and hit SHIFT+TAB, it cycles through the buttons to the right of it.
So this is an example of my form. The textbox in the middle is a custom control. My code (as simplified as possible) looks like:
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class ToolStripTestControl : ToolStripControlHost
{
public ToolStripTestControl() : this(new TestControl()) { }
public ToolStripTestControl(Control c) : base(c) { }
}
public class TestControl : UserControl
{
private TextBox _textBox = new TextBox();
public TestControl()
{
_textBox.Dock = DockStyle.Fill;
this.Controls.Add(_textBox);
}
protected override Size DefaultMinimumSize { get { return new Size(100, 22); } }
}
Simply creating a new WinForms (.NET4) project and following these steps will allow you to replicate the problem:
Add new class file and paste the code above.
Build
Add a ToolStrip to your form
On the ToolStrip add a Button, my custom control, and another Button (through the designer is how I've been doing it)
Run
Once running...
Focus in the custom control
Hit TAB a few times, it should only focus on controls to the left.
Hit SHIFT+TAB a few times and it will only focus to the right.
Does anyone know what the problem is - or how I can fix this? I've been tearing my hair out all day trying to fix this. I finally stripped my code down and I can't seem to get it to work. I even tried overriding much of the OnEnter/OnGotFocus functionality and doing it myself, but that became a nightmare.
Thanks!
Update1: So a few extra tid-bits.
If I change the custom control to inherit from TextBox instead of UserControl, tabbing/focus works as expected.
If I change it to be a Control instead of a UserControl the tabbing works fine, as well, however the focus never gets inside my inner TextBox - the focus seems to be lost (or presumably on the outer parent control but not being passed down to the inner TextBox).
I do see a MS Connect item added that describes this problem from 2009, but this link only seems to work if I'm NOT logged in to Microsoft Connect. Which means, I can't vote on it or comment... http://connect.microsoft.com/VisualStudio/feedback/details/472592/tab-cycling-through-controls-with-usercontrol-on-toolstrip-doesnt-perform-as-expected
The .NET 2.0 ToolStripItem classes have been a major bug factory. They are window-less controls, but reproducing the exact behavior of a Windows window isn't that easy. There is an enormous amount of code underneath, most of it internal so you can't tinker with it. And with quirks when they don't emulate the behavior of a window perfectly. You could call them "airspace" issues, pretty similar to the kind of problems that WPF has.
The airspace issue here is focus, entirely unambiguous for a true window but it needs to be faked for a ToolStripItem. It is actually the item's parent that has the focus, it needs to be emulated for the item. It is the transition that bytes, ToolStrip expects a window-based control to have a reliable Focus property.
Trouble is, your custom host doesn't. It is the inner control that has the focus. This could arguably be blamed on an omission in the ToolStripControlHost class. Probably. The trouble with emulating a window, there's never enough code :)
Anyhoo, fix your problem by adding this sliver of code to your host:
public override bool Focused {
get { return _textBox.Focused; }
}

C# WPF Frame->UserControl->Usercontrol Loaded event doesn't fire

I'm new into WPF and have a problem I can't seem to find a solution for.
I'm writing a G19 (Keyboard) applet. This Keyboard has a 320x240 display attached, which you can access with C#.
I'm using WPF for this, because I don't need to do any GDI drawing anymore and use the normal controls instead.
So. It works as I wish. Everything draws properly except one UserControl. I have downloaded this control -> http://marqueedriproll.codeplex.com/
In the designer, the control works, the Loaded event get's fired and the animation is good.
When I run my application, I just see the label and the text. The animation does not work, and the Loaded event does not fire anymore.
Any help is appreciated.
The main function is my wrapper. The wrapper is already a Usercontrol and displays plugins which are switchable. This wrapper has the Frame Control(Wrapper1). I replace the content of this frame every time I switch the plugin.
public void SetPlugin(IPlugin plugin)
{
if (this.MainPlugin != null)
{
this.MainPlugin.OnHide();
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Hidden;
}
this.MainPlugin = plugin;
((UserControl)this.MainPlugin).Visibility = System.Windows.Visibility.Visible;
this.MainPlugin.OnShow();
this.Wrapper1.Content = this.MainPlugin;
}
I think it's the right approach to handle a plugin system that way. The plugin get's drawed on my keyboard.
What I don't understand is why the usercontrol only works in the designer view and not in the running application.
The basic code of the scrolling label is so:
public MarqueeText()
{
this.Loaded += new RoutedEventHandler(MarqueeText_Loaded);
InitializeComponent();
canMain.Height = this.Height;
canMain.Width = this.Width;
}
void MarqueeText_Loaded(object sender, RoutedEventArgs e)
{
StartMarqueeing(_marqueeType);
}
I don't see a reason why it doesn't work. Actually Ive always found a way to fix a problem but this time I see nothing.
Thanks in advance. Your help is really required today.
Have a great saturday! :)
I am guessing you are rendering to a bitmap target, rather than onscreen. If you are using RenderTargetBitmap, you have a couple of responsibilities. You need to set both a presentation source, and make sure you run events on the dispatcher.
Normally, App.xaml or Application.Run does this for you, but if you are not using a Window, you are on your own.
See this related question for details.

WPF TextBox Focus on constructor

I'm having a problem focusing on a Control within a Window in WPF.
On the constructor of the Window I add a TextBox as follows:
TextBox tb = new TextBox();
tb.Text = input;
tb.SelectAll();
tb.Focus();
I also call the Focus() method again on the Window loaded event.
The problem is that sometimes the window focuses and other times it does not! This is very strange behavior and I was wondering if someone has had the same problem and might be able to give me some guidance.
-- UPDATE
I have found what is causing the problem but no solution yet. I am using WIA to scan a document, this brings up a ProgressBar which makes my entire application lose focus.
I have tried calling Application.Current.MainWindow.Focus() but this does not restore focus, any suggestions?
From memory, I think the last call on window creation is the Windows.Activated event. You could try running your focus code on that event?
Like I mentioned in my updated question my entire application loses focus due to another dialog being displayed, what I ended up using was:
Application.Current.MainWindow.Activate();
This returns focus to my application and when my Window with the TextBox is shown, Focus is applied to this control.
Hope it helps someone!

ClickEvent in message

I am trying to write a tooltip which is clickable. Currently i am thinking about a usercontrol which derives from IButtonControl and ToolTip.
public partial class JLToolTip : ToolTip, IButtonControl
As far as I can see it does everything a normal tooltip should do but if I click it
, the window loses shortly the focus until it gets it after a half second or so back.
The first thing i am trying to accomplish is this.
public void PerformClick()
{
MessageBox.Show("CLICK");
}
I know it looks dead simple but I can't find the proper solution or why it won't work
I hope anybody of you does have an answer.
If all you want is clickable and composable tooltip control, I recomment Simple Popup Control. You can easily extend it and adopt it to your needs.
I don't fully understand your question, but I think this is what you are looking for.

Categories

Resources