Change font colour of DevComponents AdvTree node - c#

I'm looking to change the font colour of specific nodes on a DevComponent AdvTree. I found the following information, relating to changing style, on the DevComponents 'Knowledge Base':
// Create new style
ElementStyle styleRightAligned = new ElementStyle();
// Set text alignment to right
styleRightAligned.TextAlignment = eStyleTextAlignment.Far;
advTree1.Styles.Add(styleRightAligned);
// Add new cell to the AdvTree
Cell cell = new Cell();
cell.Text = "Right";
cell.StyleNormal = styleRightAligned;
// Assign style to cell, same style can be assigned to any number of cells
node1.Cells.Add(cell);
I can't understand what object is being referred in eStyleAlignment.Far.
Does anyone have experience with changing styles within the DevComponents DotNetBar?
Thanks,
Andy

I figured out how to do this. The AdvTree control has a Styles property. This is a collection; styles can be added to it at design time.
Then the code to change the style of a particular node is:
void ChangeNodeStyle(AdvTree tree, int node, int style)
{
tree.Nodes[node].Style = tree.Styles[style];
}
Thanks,
Andy

Related

Change color in RichTextBox

I am using this simple example from MSDN
to insert lines in a RichTextBox.
FlowDocument myFlowDoc = new FlowDocument();
Run myRun = new Run("This is flow content and you can ");
Bold myBold = new Bold(new Run("edit me!"));
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun);
myParagraph.Inlines.Add(myBold);
myFlowDoc.Blocks.Add(myParagraph);
RichTextBox myRichTextBox = new RichTextBox();
myRichTextBox.Document = myFlowDoc;
I want to apply a chosed color to the lines of text, but how to do it?
The Paragraph or Run classes doesn't have any direct method to change the color.
EDIT
I don't want to use all the awkard SelectionStart, SelectionEnd stuff as posted on the linked post!.
My case is different and is much more simple: the solution posted from mm8 explains it and is very elegant.
One single line of code and that is!
Please see the answer!
The Paragraph or Run classes doesn't have any direct method to change the color.
The Run class inherits from TextElement and this class has a Foreground property that you can set to a Brush:
Run myRun = new Run("This is flow content and you can ") { Foreground = Brushes.Red };
Bold myBold = new Bold(new Run("edit me!") { Foreground = Brushes.Gray });
You can get/set text color via Foreground property of the rich text box. As bellow example, I changed the text color of rich text box to blue:
myRichTextBox.Foreground = Brushes.Blue;
Happy coding!

Can't apply style in WPF via C# code

I've discovered what the problem was, It is nothing to do with the C# code itself, But it's in the XAML instead, The issue was the default colors that I've set in the XAML were overriding my style's colors.
So in conclusion, when you are setting any property by XAML it always overrides later styles set by C# code at runtime, this seems strange to me but at least that is how it worked for me.
The default Background colors in the XAML code avoided the C#'s style to apply on the panels (At-least avoided the new Background to be applied over the default ones).
I used your code and modified little bit for verification. Seems to be working fine. Have a look:
Style Style_Panel = new Style(typeof(Panel));
public void Init_Style()
{
// Create Styles :
#region "Create Styles"
Style_Panel.Setters.Add(new Setter()
{
Property = Panel.BackgroundProperty,
Value = new SolidColorBrush(Colors.Red)
});
Resources.Add(Style_Panel.TargetType, Style_Panel);
#endregion
// Apply Styles :
#region "Apply Styles"
List<Visual> List_Visual = new List<Visual>();
List_Visual.Add(new StackPanel() { Name = "btn" });
//Enum_Visual(Panel_Main, List_Visual);
foreach (Visual visual in List_Visual)
{
if (visual is Panel)
{
Panel panel = visual as Panel;
//if (Tagged(panel, "titlebar"))
//{
//}
//else if (Tagged(panel) == false)
{
// panel.Background = new SolidColorBrush( Colors.Red ); // <- WORKS .
panel.Style = Style_Panel; // <- DOES NOT WORKS !
}
}
}
#endregion
}
You haven't posted the creation of your style, maybe something is missing there?
There is another similar answer on StackOverflow which is a very good and short example of creating and setting a style in code:
Q: Does anyone know how to create a wpf Style in code behind, I can't find anything on the web or MSDN docs. I have tried this but it is not working:
A: You need to add setters to the style rather than using RegisterName. The following code, in the Window_Loaded event, will create a new TextBlock style which will become the default for all instances of a TextBlock within the Window. If you'd rather set it explicitly on one particular TextBlock, you can set the Style property of that control rather than adding the style to the Resources dictionary.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Style style = new Style(typeof (TextBlock));
style.Setters.Add(new Setter(TextBlock.ForegroundProperty, Brushes.Green));
style.Setters.Add(new Setter(TextBlock.TextProperty, "Green"));
Resources.Add(typeof (TextBlock), style);
}

Prevent listview items from showing in a second column

I am working with Winforms using C#.
It is easier to explain my question with a screen shot:
The list view on the left, notice how items15 and later are showing in a second column. I don't want that. I want it to have a vertical scroll bar and items15 to appear under item14, etc..not in a new column. I have set the "view" property on "List" too.
Thanks all.
You need to Set
listview.Scrollable = true;
listview.View = View.Details
listview.HeaderStyle = ColumnHeaderStyle.None;
Add a dummy column, its an important step, cause we changed the View to details:
ColumnHeader header = new ColumnHeader();
header.Text = "MyHeader";
header.Name = "MyColumn1";
listView.Columns.Add(header);

how to change background color of table cell of ppt using c# code?

I have written below piece of code to change background color of table cell of ppt using c# code but nothing is happening:
//creating powerpoint aaplication
PowerPoint.Application pptApp = new PowerPoint.Application();
pptApp.Visible = Office.MsoTriState.msoTrue;
var pptPresent = pptApp.Presentations;
var fileOpen = pptPresent.Open(#file, Office.MsoTriState.msoFalse, Office.MsoTriState.msoTrue, Office.MsoTriState.msoTrue);
// getting first slide
PowerPoint.Slide objSlide = fileOpen.Slides[1];
PowerPoint.Shapes item = objSlide.Shapes;
// getting first shape
var shape1 = item[1];
// check if shape is table
if (shape1.HasTable == Office.MsoTriState.msoTrue)
{
// change the table cell to red color
shape1.Table.Cell(2, 5).Shape.Fill.BackColor.RGB = System.Drawing.Color.Red.ToArgb();
// make it visible
shape1.Fill.Visible = Office.MsoTriState.msoTrue;
}
// saving the ppt
fileOpen.SaveAs(openFolder + subStr + ".pptx",PowerPoint.PpSaveAsFileType.ppSaveAsDefault,Office.MsoTriState.msoTrue);
// close the ppt
fileOpen.Close();
Above piece of code is not working as expected, can someone help me?
To change the cell color ForeColor needs to be used instead of BackColor. Drawing colors may not work as expected. To change that use ColorTranslator.
shape1.Table.Cell(2, 5).Shape.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
Great question! The Fill property is also available for the Shape object, wether it's a Cell or a another type of Shape.
Another example for the Fill property is mentioned in this thread:
https://stackoverflow.com/a/26253177/20337158.
Also from the Microsoft learning sites:
https://learn.microsoft.com/en-us/previous-versions/office/office-12/ff763049(v=office.12)

How-To set Height of a Textbox?

For my single line Textbox, I set is Border = None. On doing this, the height turns very small. I can't programamtically set the height of the textbox. If I set any border, then again its fine, but I don't want any border. Even the text is not visible completely - so the font size is already bigger the the textbox height.
I tried creating a custom textbox, and set the Height of it, but it has no effect. How to handle this situation? Any help is highly appreciated.
There is a simple way not to create a new class.
In Designer.cs file:
this.textBox1.AutoSize = false;
this.textBox1.Size = new System.Drawing.Size(228, 25);
And that's all.
TextBox derives from Control, which has an AutoSize property, but the designers have hidden the property from the PropertyGrid and Intellisense, but you can still access it:
public class TextBoxWithHeight : TextBox {
public TextBoxWithHeight() {
base.AutoSize = false;
}
}
Rebuild and use.
TextBox controls automatically resize to fit the height of their Font, regardless of the BorderStyle you choose. That's part of the defaults used by Visual Studio.
By changing the Multiline, you can override the Height.
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif",
26.25F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(373, 502);
// this is what makes the height 'stick'
this.textBox1.Multiline = true;
// the desired height
this.textBox1.Size = new System.Drawing.Size(100, 60);
Hope this helps.
I just created this case in an empty project and don't see the result you are describing.
When the BorderStyle is none, the display area of the Textbox auto-sizes to the font selected. If I then set Multiline = true, I can change the height portion of the Size property and the change sticks.
Perhaps another portion of your code is modifying the height? A resize event handler perhaps?
My suggestions:
Post the relevant portions of your code
Try to reproduce the issue in an empty WinForms project (as I just did)
I find the best solution is to subclass the Textbox and expose the hidden AutoSize there:
public class TextBoxWithHeight : TextBox
{
public bool Auto_Size
{
get { return this.AutoSize; }
set { this.AutoSize = value; }
}
}
Now you can set the Autosize on or off using the object inspector in the visual designer or in code, whatever you prefer.
Just select your textbox and go to properties then increase your font size.. DONE !!!

Categories