Posts

Showing posts from May, 2022

How to allow moving focus to and from the WPF WebView control using the Tab key in C# and VB

If you've ever used the WPF Microsoft Edge Legacy based WebView control (not to be confused with the Internet Explorer based WebBrowser control), you may have noticed that you can't use the Tab key to move focus to and from the said control and that only the mouse allows moving focus to and from it. You may also have noticed that using the WebView.Focus() method does not work. Luckily, the WebView contains both a function you can use to move focus to the WebView and an event which fires when the user attempts to move focus away from the WebView using the Tab key. How to move focus to the WebView using the Tab key We can moved focus to the WebView using the MoveFocus function of the WebView. In order to detect when the user attempts to focus the WebView, we can use a dummy button to detect the focus and then move it onto the WebView. XAML: < Button x : Name ="webViewFocusButton" Height ="0" Width ="0" Opacity ="0" GotFocus ="we

How to specify referrer URI when navigating to a page in Storyboards WebView in Swift

 When navigating a Storyboards WebView to a new URI, you may want to specify a  referrer URI . When using the WebView, we can do this using the load function. To use this function, we need to create a URLRequest. From a URLRequest, we can add a value for referrer to the request, then pass it into the load function of the WebView like so. Swift: let myURL = URL(string "https://codingguides.quinnscomputing.com" ) var myRequest = URLRequest(url: myURL!) myRequest.addValue( "https://www.quinnscomputing.com" , forHTTPHeaderField: "Referer" ) self . WebViewMain .load(myRequest) This will navigate the WebView to the specified URI with the specified referrer URI. This snippet is available in Codly. Click the appropriate link below to download the snippet. If you don't have Codly, it is available here in the Microsoft Store . Download

How to specify referrer URI when navigating to a page in UWP WebView in C# and VB

When navigating a UWP WebView to a new URI, you may want to specify a referrer URI . When using the UWP WebView, we can do this using the NavigateWithHttpRequestMessage function. To use this function, we need to create a HttpRequestMessage. From an HttpRequestMessage, we can specify a URI to navigate to and referrer URI, amongst many things. C#: Windows.Web.Http.HttpRequestMessage Request = new Windows.Web.Http.HttpRequestMessage(); Request.Headers.Referer = new Uri( "https://www.quinnscomputing.com" ); Request.RequestUri = new Uri( "https://codingguides.quinnscomputing.com" ); webViewMain.NavigateWithHttpRequestMessage(Request); VB: Dim Request = New Windows.Web.Http.HttpRequestMessage() Request.Headers.Referer = New Uri( "https://www.quinnscomputing.com" ) Request.RequestUri = New Uri( "https://codingguides.quinnscomputing.com" ) webViewMain.NavigateWithHttpRequestMessage(Request) This will navigate the WebView to the specified URI with t

How to show placeholder text in a WPF TextBox in C# and VB

Image
Sometimes, when you have a text box in a user interface, you may want to add placeholder text to that text box to give users a prompt as to what to enter into the text box. If you've ever used UWP or WinUI, you'll probably have noticed that the default text box elements provide a PlaceholderText property that provides this functionality. However, WPF and Windows Forms don't provide this functionality by default. Luckily, thanks to WPF's rendering capabilities, it is quite easy to set up placeholder text manually. If you're using Windows Forms, you can make use of the ElementHost to use a WPF TextBox. Typically, placeholder text is a slightly faded colour compared to user-entered text to help distinguish it and only shows when no text is entered into the text box. An example is below: How to show placeholder text in a WPF TextBox To show placeholder text in a WPF textbox, all we need to do is show a partially transparent, intangible Label above the TextBox like so (Y

How to enable dark title bar in WPF and Windows Forms/WinForms apps in C# and VB

Image
Windows has had a dark mode feature for a long time now, however, as a Windows Forms and/or WPF (Windows Presentation Foundation) developer, you may have noticed that the Windows dark mode does not affect your app at all automatically - in fact the Windows dark mode only automatically affects UWP, WinUI and Chromium based user interfaces - there's no support for Windows dark mode in Windows Forms and WPF. Tip: If you want to set the title bar's colours to any colours you want instead of just light or dark, follow this guide here . Luckily, it is very easy to determine if dark mode is currently enabled and be notified any time dark mode is switched on or off - from there it's just a matter of updating your UI to match which mode Windows is in. While this may work fine for the client area of your app (i.e. the area of your app that does not include the title bar and border), you don't have a lot of control over the non-client area of your app and you'll notice that by