In a very simple WPF application, I have the following code in the button1_click() method:
textBlock1.Inlines.Add(new Run("TextBlock is designed to be lightweight."));
canvas.Children.Add(textBlock1);
Debug.Print("textBlock1.Text: {0}", textBlock1.Text);
The Debug.Print statement prints nothing for the Text property, but the actual text (i.e. "TextBlock is designed...") is visible in the TextBlock control on the canvas. Why doesn't the Text property depict an exact copy of the Inlines?
PS: There is no data binding, etc. used. The project is very simple, with a TextBlock and a Button on a Canvas inside MainWindow, and minimal XAML; everything is handled in the code-behind.
You can just directly set the Text property of the Textblock. If you have a text that needs special formatting like bold, italic, underline, etc in for each words then that's the time you want to use the Inlines.
If you want to debug the actual Text you want to extract the Run into a separate variable and access its Text property.
var run = new Run("TextBlock is designed to be lightweight");
Debug.Print(run.Text);
Related
I want to edit a property "Word Wrap" in the richtextbox, which is in the tabpage. How to edit a property of this richtextbox in code?
More details:
I made two forms. One with tab control and other things and one without borders, only with RichTextBox. On load I added richTextBox from Form2 to empty TabPage in Form1, later I changed DockStyle of richTextBox to Fill, at the end I added this TabPage to the TabControl. And now I want to make Word Wrap button in Menu Strip, so I want to access to richTextBox of selectedTab (yes, I added "new tab" button ) and modify it's property, but I don't know how.
Everything you can see in the properties grid is, in some way, a Property of the object instance (the properties grid is populated by examining the property members of the object
If your rich text box is called richTextBox1 then the property in code is richTextbox1.WordWrap
Sometimes the properties grid has a custom editor, like the Dock property, you don't simply choose a value from a combobox, there's a dedicated editor device that has buttons (a north / south / east / west / centre button arrangement) - don't be confused by his, all it's doing is setting some normal enumeration value like Fill or Top.. it's just that Microsoft decided to build a custom editor for it
If you take a look at the auto generated code file YourFormName.Designer.cs you'll see all the code that the forms designer has created: if you set wordwrap in the properties grid, you'll find he relevant line of code in there, and you can copy it out if you want (handy for more complex stuff like event handlers )
In summary, just remember, the windows forms designer doesn't do anything other than write normal c# code, just like you can! Take a look at the .Designer file to see what I mean. If it doesn't show up in e solution explorer then just take a look in the folder on disk where all your code is..
I have created a text box in run time using XAML. I need to make the text box as read only. So I have used a code Browsable[True] & ReadOnly[True] to achieve this.
So now the style of the text box is changed. Initially, the data in the text box were in bold. But now it is bit lite not in bold text. Now how to make it as bold. Thanks much.
[DisplayName("Name"), Browsable(true), ReadOnly(true)]
Setting the textBox as readonly does change it´s design and once you have set it as readonly you cant modify it´s content. What about using a Label instead, the user cant modify a Label´s content ever and that´s bascically what you achieve by setting the textBox as readonly.
Adding the Browsable property to the read only property makes the text box as Read only while generated on run time in WPF.
[Browsable(true), ReadOnly(true)]
Is it possible to change the forecolor of substring in element text in GridControls's Tileview in devexpress winforms?
What's going to happen is I have a textbox and tileview
In event while textbox textchanged fire, the text matched in tileview's element text will be highlighted
The initial color of tileview's text will be blue and if its match with textbox's text it will be highlighted as gray.
See picture to check the illustration:
Is it possible? or will i need another component to attain this output?
As far I can see there is property group called Appearance (TileView.Appearance) and there you have property called ForeColor. Moreover, you can acces via code by calling
titleViewName.Appearance.ForeColor = <RGB value goes here>
Finally, there is possibility for dynamic customization via event ItemCustomize.
For further information please check official documentation and DevExpress forum. There already existis questions similiar to yours.
You can try to use HTML Text formating for displayed string:
Text = "This is some <color=red>red or <color=black><b>bold</b> text!"
I have seen a lot of answers on the web stating that a Label's text can't be selected/copied in the way that a TextBox's contents can,
but what is the underlying reason that a Label's text can't be copied?
Windows itself can find text under the cursor position, so why can't the WinForm Label control?
In order for a user to select or copy a control's text, the control must allow you to set focus to it, either by clicking or tabing to the control.
A Label doesn't allow this, by design.
Label controls are typically used to provide descriptive text for a control. For example, you can use a Label to add descriptive text for a TextBox control to inform the user about the type of data expected in the control.
So while Labels and TextBoxes both inherit from System.Windows.Control they are different things, intended for different purposes. In the same way that oranges and apples are both fruit, but are different.
However, if you're creating an application and want to have something that looks like a label, but allows the user to select (but not edit) the text, then you can use a TextBox with the following properties set:
Backcolor = Control
ReadOnly = true
BorderStyle = none
As shown below...
Alternatively, if you have an application and want to get text from something like a label, you can use the Win32 API function GetWindowText, if you know the handle to the window that contains the text. In a Win32 context a "window" means just about anything distinct that is on the screen, not just the windows that you can drag around with your mouse. WinForms is an abstraction on top of all this.
As for getting the handle to the window that is under the mouse cursor, see this question.
Is it possible to add a "default" text value to a TextBox in WPF, with the text disappearing when the user clicks on the control or types anything in it ?
If it's not possible, how would I create a template which allows a user to define a text box with distinct properties for the content of the control and its initial value?
This is certainly possible. You can either:
1) Set the initial text normally, then on an event such as OnClick() or OnTextSelected() you can delete that text, then it will be blank before they start typing. You can also go one step further and add something to OnTextChanged() to see whether the text is blank and every time there is no content, your initial content can be set again.
2) Alternatively, you can create a custom style for your text block, with a text block inside of it, a property to set the text block text and an event hooked up to clear the text block text. This is nicer, but more work. I would recommend this if you wish to re-use this control.