Skip to content

Commit 4764437

Browse files
dimodiradkostanev
authored andcommitted
kb(Window): Add one more example to KB
1 parent 85a0cb8 commit 4764437

File tree

1 file changed

+90
-4
lines changed

1 file changed

+90
-4
lines changed

knowledge-base/window-focus-button-textbox-on-open.md

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ page_title: How to Focus Button or TextBox on Window Open
66
slug: window-kb-focus-button-textbox-on-open
77
position:
88
tags: telerik, blazor, window, dialog
9-
ticketid: 1486212, 1513605, 1610413, 1659021
9+
ticketid: 1486212, 1513605, 1610413, 1659021, 1685761
1010
res_type: kb
1111
---
1212

@@ -30,11 +30,98 @@ This KB article answers the following questions:
3030
* How to focus a button or a textbox inside a Window when the Window is made visible?
3131
* How to focus a component after the Window opens? The JavaScript call is made before the Window actually shows, so the focusable element is `null`.
3232
* How do you set focus on an input element in a Window, so that the user doesn't have to use their mouse?
33+
* How to open (expand) a DropDownList automatically on Window open?
3334

3435

3536
## Solution
3637

37-
To focus any element or component in the [Telerik UI Window for Blazor](slug:dialog-overview), follow the steps below. They are also applicable for the [Dialog component](slug:dialog-overview).
38+
There are two ways to focus any element or component in the [Telerik UI Window for Blazor](slug:window-overview). They are also applicable for the [Dialog component](slug:dialog-overview).
39+
40+
* [Add a delay after changing the Window's `Visible` parameter value](#add-delay-after-changing-the-visible-parameter-value)
41+
* [Use a boolean flag in `OnAfterRenderAsync`](#use-boolean-flag-in-onafterrenderasync)
42+
43+
### Add Delay after Changing the Visible Parameter Value
44+
45+
1. To focus a Telerik UI for Blazor component, [set the `@ref` attribute to obtain the component's reference](slug:components/textbox/overview#textbox-reference-and-methods).
46+
1. Set the Window `Visible` parameter to `true` programmatically in some event handler.
47+
1. Use a small `Task.Delay()` to trigger an immediate UI refresh in the same event handler. The delay allows the app to wait for the Window to display and gain focus. Without a delay, the Window and the focusable component will not exist yet.
48+
1. Focus the desired button, textbox, or input component. If it's a Telerik UI for Blazor component, use the [`FocusAsync()` method](slug:inputs-kb-focus).
49+
50+
>caption Focus a component on Dialog or Window open
51+
52+
````RAZOR
53+
<TelerikButton OnClick="@ShowWindow">Show Window and Focus Button</TelerikButton>
54+
<TelerikButton OnClick="@ShowDialog">Show Dialog and Focus TextBox</TelerikButton>
55+
56+
<TelerikWindow @bind-Visible="@WindowVisible"
57+
Modal="true">
58+
<WindowActions>
59+
<WindowAction Name="Close" />
60+
</WindowActions>
61+
<WindowTitle>
62+
Window
63+
</WindowTitle>
64+
<WindowContent>
65+
<p>This Button is now focused:</p>
66+
<TelerikButton @ref="@ButtonRef"
67+
OnClick="@OnWindowButtonClick">
68+
Hit Enter or Space
69+
</TelerikButton>
70+
<p><code>StringValue</code>: @StringValue</p>
71+
</WindowContent>
72+
</TelerikWindow>
73+
74+
<TelerikDialog @bind-Visible="@DialogVisible"
75+
Title="Dialog"
76+
ButtonsLayout="@DialogButtonsLayout.End">
77+
<DialogContent>
78+
<TelerikTextBox @ref="@TextBoxRef" @bind-Value="@StringValue" />
79+
</DialogContent>
80+
<DialogButtons>
81+
<TelerikButton OnClick="@( () => DialogVisible = false )">Close</TelerikButton>
82+
</DialogButtons>
83+
</TelerikDialog>
84+
85+
@code {
86+
private TelerikButton? ButtonRef { get; set; }
87+
private TelerikTextBox? TextBoxRef { get; set; }
88+
89+
private string StringValue { get; set; } = string.Empty;
90+
91+
private bool WindowVisible { get; set; }
92+
private bool DialogVisible { get; set; }
93+
94+
private async Task ShowWindow()
95+
{
96+
WindowVisible = true;
97+
// Trigger UI refresh immediately
98+
// Wait for the Window to render and gain focus.
99+
await Task.Delay(200);
100+
101+
if (ButtonRef != null)
102+
await ButtonRef.FocusAsync();
103+
}
104+
105+
private async Task ShowDialog()
106+
{
107+
StringValue = string.Empty;
108+
DialogVisible = true;
109+
// Trigger UI refresh immediately
110+
// Wait for the Dialog to render and gain focus.
111+
await Task.Delay(200);
112+
if (TextBoxRef != null)
113+
await TextBoxRef.FocusAsync();
114+
}
115+
116+
private void OnWindowButtonClick()
117+
{
118+
var now = DateTime.Now;
119+
StringValue = $"{now.ToString("HH:mm:ss")}.{now.Millisecond}";
120+
}
121+
}
122+
````
123+
124+
### Use Boolean Flag in OnAfterRenderAsync
38125

39126
1. To focus a Telerik UI for Blazor component, [set the `@ref` attribute to obtain the component's reference](slug:components/textbox/overview#textbox-reference-and-methods).
40127
1. Raise a `bool` flag when showing the Window.
@@ -60,7 +147,7 @@ To focus any element or component in the [Telerik UI Window for Blazor](slug:dia
60147
<p>This Button is now focused:</p>
61148
<TelerikButton @ref="@ButtonRef"
62149
OnClick="@OnWindowButtonClick">
63-
Click Me
150+
Hit Enter or Space
64151
</TelerikButton>
65152
<p><code>StringValue</code>: @StringValue</p>
66153
</WindowContent>
@@ -131,7 +218,6 @@ To focus any element or component in the [Telerik UI Window for Blazor](slug:dia
131218
}
132219
````
133220

134-
135221
## See Also
136222

137223
* [Dialog Overview](slug:dialog-overview)

0 commit comments

Comments
 (0)