# Control Initialization

> Fix initialization problems of TX Text Control .NET for WPF: code that runs before the TextControl is loaded, ResourceProvider calls and ServerTextControl.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Troubleshooting > Control Initialization
- **820 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/initialization/llms-full.txt

---

The WPF `TextControl` is ready to work with a document once WPF has loaded it into the window. Code that runs earlier, for example in the constructor of the window, finds a control that isn't ready yet. The samples and tutorials of TX Text Control therefore run their startup code in the `Loaded` event of the control, and the problems below come from code that doesn't.

### Code in the Constructor Has No Effect or Fails

Code that loads a document, inserts text or reads the version of the control fails or has no effect when it runs in the constructor of the window, while the same code works later, for example when a button is clicked.

When the constructor runs, WPF hasn't loaded the control into the window yet. Many members of the [TextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/llms-full.txt), such as [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/selection-property/llms-full.txt), are available at run time only, and the tutorials and samples of TX Text Control use them from the `Loaded` event of the control.

1. Connect a handler to the `Loaded` event of the `TextControl`, in XAML with `Loaded="TextControl1_Loaded"` or in the constructor after `InitializeComponent`.
2. Move the startup code into this handler.

[Control Initialization and Document Access](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/initialization/llms-full.txt) explains the lifecycle of the control, the members that exist only at run time and the difference between `Loaded` and [DocumentLoaded](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/documentloaded-event/llms-full.txt), with a complete example.

### A File Passed at Start Isn't Loaded

Your application opens a file that is passed on the command line or from another window, but the `TextControl` stays empty.

The file is loaded before the control is ready. The TX Words samples open such a file only after the `Loaded` event of the control.

1. Keep the name of the file in a field of the window.
2. In the `Loaded` handler of the `TextControl`, call [Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/load-method/llms-full.txt) with it, as [Open a Document When the Window Opens](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/initialization/#open-a-document-when-the-window-opens) shows.

### GetLargeIcon or GetSmallIcon Throws an Exception

[ResourceProvider.GetLargeIcon](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/resourceprovider/getlargeicon-method/llms-full.txt) or [GetSmallIcon](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/resourceprovider/getsmallicon-method/llms-full.txt) throws an exception with this message:

```text
The specified framework element has not been loaded for presentation.
```

The overloads that take a `FrameworkElement` need the element to be loaded. [GetLargeIcon or GetSmallIcon Throws an Exception](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/dpi-scaling/#getlargeicon-or-getsmallicon-throws-an-exception) describes the fix.

### A ServerTextControl Fails

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt), which processes documents without a user interface, throws this message:

```text
The TXTextControl object must have been completely loaded to use this method.
```

A `ServerTextControl` isn't loaded into a window: it must be initialized with [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) before any other member is used. [The TXTextControl Object Must Have Been Completely Loaded](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/conversion/#the-txtextcontrol-object-must-have-been-completely-loaded) lists the fixes.

### Related Pages

- [Control Initialization and Document Access](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/integration/initialization/llms-full.txt): when the control is ready and how to initialize it.
- [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt): a first window with the `Loaded` event.
- [Licensing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/licensing/llms-full.txt): license errors when the control is created.

---

## Nearby

- **Previous:** [XAML Namespaces and Assembly References](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/xaml-namespaces/llms-full.txt)
- **Next:** [Data Binding and Commands](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/troubleshooting/data-binding/llms-full.txt)
