Posts

Showing posts with the label SwiftUI

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 create a basic button in SwiftUI

  There are various ways to create a button in SwiftUI, let's start with a button that consists of text: Button("Print") { print("Button pressed") } Here, we specify the text for the button and inside the curly braces is the code we want to run when the button is press. In this case we print "Button pressed" but you can put any action you want there. The button will appear blue by default but you can change its colour with a modifier: Button("Print") { print("Button pressed") }.foregroundColor(.red) You may have noticed in iOS buttons don't have to be text, but they can consist of a range of different views to customise their look. For example you can give the button a background like this: Button(action: { print("Button pressed")}, label: { Text("Print") .foregroundColor(.white) .padding() .background(Color.blue) .cornerRadius(5) } Buttons don't require text either, they could be ...