WPF window won't appear when using Show or ShowDialog - c#

I'm a junior programmer.
A window of my WPF program won't show itself anymore. I can't seem to find out why.
My program has three different windows. When the first one shows up and I click on the button to start the rest of the program, it does run, it also goes through the code of the window that I want to see, but it does not show anything.
I hope that any of you can help.
Below are parts of my code. The first one should already call the window
private void SortingPartOne(){
Function rename = new Function();
rename.wrkLocTree.CurrentDirectory = StartingPath;
rename.wrkLocTree.GiveUpdatePerFolder();
rename.wrkLocTree.Show();
The second part creates a new class and also should 'create' the window.
public class Function{
public WorkLocationTree wrkLocTree = new();
The third part is for updating the window that won't show up
public void GiveUpdatePerFolder(){
//variable declaration
int dirCounter;
string directoryToAdd = Path.GetFileName(CurrentDirectory);
if(String.IsNullOrEmpty(BeginPath)) BeginPath = MainWindow.StartingPath;
string dirs = Path.GetFileName(BeginPath) + CurrentDirectory.Replace(BeginPath, "");
List<string> directories = dirs.Split('\\').ToList();
dirCounter = directories.Count-1;
// End variable declaration */
if(!oldDirectories.Any()){
if(_textBoxes == null || _textBoxes[0] == null) _textBoxes = new TextBox[1];
_textBoxes[0] = new TextBox{ Text = directoryToAdd, Width = 250 };
AddTextBox(0);
}
else{
if(directories == oldDirectories) return;
if(directories.Count > oldDirectories.Count){
if(_textBoxes == null || _textBoxes[0] == null) _textBoxes = new TextBox[1];
else{
List<TextBox> txtBxConverter = _textBoxes.ToList();
txtBxConverter.Add(new TextBox());
txtBxConverter.Add(new TextBox());
_textBoxes = txtBxConverter.ToArray();
}
_textBoxes[dirCounter] = new TextBox{ Text = directoryToAdd, Width = 100 };
if(_lines == null || _lines[0] == null) _lines = new Line[2];
else{
List<Line> converter = _lines.ToList();
converter.Add(new Line());
converter.Add(new Line());
_lines = converter.ToArray();
}
int lineCounter = _lines.Length > 0 ? _lines.Length : 0;
_lines[lineCounter] = new Line{ VerticalAlignment = VerticalAlignment.Center, X1 = 0, X2 = 7.5, Stroke = Brushes.Chocolate, StrokeThickness = 3 };
_lines[lineCounter + 1] = new Line{ VerticalAlignment = VerticalAlignment.Center, Y1 = -14, Y2 = 1.5, Stroke = Brushes.Chocolate, StrokeThickness = 3 };
AddLine(dirCounter, lineCounter);
AddTextBox(dirCounter);
}
else if(directories.Count < oldDirectories.Count){
ClearLayer(dirCounter, 2);
_textBoxes[directories.Count - 1].Text = directoryToAdd;
AddTextBox(dirCounter);
}
else if(directories.Count == oldDirectories.Count){
if(directories[directories.Count-1] == oldDirectories[directories.Count-1]){
return;
}
ClearLayer(dirCounter, 1);
_textBoxes[dirCounter].Text = directoryToAdd;
AddTextBox(dirCounter);
}
}
oldDirectories.RemoveRange(0, oldDirectories.Count);
for(int i = 0; i < directories.Count; i++){
oldDirectories.Add(directories[i]);
}
InitializeComponent();
}
There's also a small part where the textboxes get added to the Stack panels. But I don't think that it would be necessary to share

Related

C# Chart growing outside of panel

My goal is to create a chart that will sit inside of a panel restricting it's size.
I managed to achieve this some time ago but today I noticed that the chart was growing inside of the panel, not allowing the data to be seen.
I have attached a picture bellow which should help understand the issue.
UPDATE
I noticed that if I rmeove 'bottom' from the Anchor property of the panel the chart does not exceed the parent panel but it does not increase with the change of the form which is what I'm looking for.
I also noticed that there was also another chart on the form that was exceeding the parent form, this time the chart would extend to the right not allowing to see the data.
This is the code that generates this second chart and places is inside of the parent panel.
panel_chart.Controls.Clear();
chart1 = new Chart();
chart1.MouseMove += chart1_MouseMove;
chart1.ChartAreas.Add(new ChartArea("chartArea1"));
chart1.Series.Clear();
chart1.Titles.Clear();
var serieOEE = new Series("OEE");
serieOEE.ChartType = SeriesChartType.Line;
serieOEE.XValueType = ChartValueType.String;
var serieProd = new Series("Prod");
serieProd.ChartType = SeriesChartType.Column;
serieProd.XValueType = ChartValueType.String;
var serieDisp = new Series("Disp");
serieDisp.ChartType = SeriesChartType.Column;
serieDisp.XValueType = ChartValueType.String;
var serieQual = new Series("Qual");
serieQual.ChartType = SeriesChartType.Column;
serieQual.XValueType = ChartValueType.String;
DateTime DataReg = DateTime.MinValue;
List<AreaOEE> listaChart = new List<AreaOEE>();
foreach (var item in ListaGrafico) //listaOEE
{
if (item.Designacao == DesignacaoLista)
{
listaChart.Add(item);
}
}
listaChart = listaChart.OrderBy(a => a.IDReg).ToList();
DateTime DataUltimoReg = DateTime.MinValue;
int j = 0;
foreach (var item in listaChart)
{
string HoraGraf = Convert.ToDateTime(item.Hora).ToString("HH:mm");
if (j == 0 || j == listaChart.Count - 1 ||
Math.Abs(Convert.ToDateTime(item.Hora).Subtract(DataUltimoReg).TotalMinutes) >= 30)
{
serieOEE.Points.AddXY(HoraGraf, item.OEE);
serieProd.Points.AddXY(HoraGraf, item.Produtividade);
serieQual.Points.AddXY(HoraGraf, item.Qualidade);
serieDisp.Points.AddXY(HoraGraf, item.Disponibilidade);
DataUltimoReg = Convert.ToDateTime(item.Hora);
if (j == listaChart.Count - 2)
{
break;
}
}
j++;
}
//Adicionar o ultimo
foreach (var item in listaOEE)
{
if (item.Designacao == DesignacaoLista)
{
string sHora = "";
try
{
sHora = item.Hora.Substring(1, 5);
}
catch (Exception ex)
{
string sEx = ex.Message;
}
foreach (var itemOee in serieOEE.Points)
{
if (itemOee.AxisLabel == sHora)
{
itemOee.YValues[0] = item.OEE;
}
}
foreach (var itemP in serieProd.Points)
{
if (itemP.AxisLabel == sHora)
itemP.YValues[0] = item.Produtividade;
}
foreach (var itemD in serieDisp.Points)
{
if (itemD.AxisLabel == sHora)
itemD.YValues[0] = item.Disponibilidade;
}
foreach (var itemQ in serieQual.Points)
{
if (itemQ.AxisLabel == sHora)
itemQ.YValues[0] = item.Qualidade;
}
}
}
chart1.Series.Add(serieProd);
chart1.Series.Add(serieQual);
chart1.Series.Add(serieDisp);
chart1.Series.Add(serieOEE);
serieOEE.BorderWidth = 4;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 90;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 140;
chart1.Legends.Clear();
chart1.Legends.Add(serieOEE.Legend);
chart1.Titles.Add(DesignacaoLista + " " + DataTitulo.ToString("dd-MM HH:mm"));
chart1.Titles[0].Font = new Font("Arial", 13, FontStyle.Bold);
chart1.Visible = true;
chart1.Dock = DockStyle.Fill;
panel_chart.Controls.Add(chart1);
you can change the size of Chart with Chart.Size:
Chart1.Size = new Size(1000, 200); //1000px * 200px

C# Form/Control Width Not Updating All the Time

I've made a small tool bar that sits in a transparent form, it loads a variable sized menu from a text file and can be changed on the fly. Each button is a type of Label, the bar is just a list of buttons and adds/removes them in the correct spots. Width of the form is only a little bigger than the menu bar so that sub menu isn't cut off
Everything is working sweet except, when I reload everything part of the toolbar is lost. I've attempted to change the width so many ways, I've cleared and removed the controls from the form, refreshing the form/menu, updating it etc however nothing seems to make it work as intended EXCEPT if I call the reload function twice in a row, it works. I can't see why calling it once doesn't work but calling it twice works.
I'm fine with calling reload twice in a row as it would only be called a couple times a week.
Question: what on earth is causing this?
photo of issues first photo shows what it should look like, second is after removing a menu button and reloading, third is after adding a button and reloading
//calling this.reload() doesn't work
//calling this.reload();this.reload() works
void reload(Object o = null, EventArgs e = null)
{
this._menuBar.clear();
this.loadFromFile();
}
void loadFromFile(Object o = null, EventArgs e = null)
{
try
{
if (File.Exists("kpi.txt"))
{
string cline = "", cmenu = "", lhs = "";
menuList mb = null;
StreamReader sr = new StreamReader("kpi.txt");
while (!sr.EndOfStream)
{
cline = sr.ReadLine(); //get current line
if (cline.Length > 0 && cline[0] != ';')
{
//check if main menu/command
if (cline[0] == '[')
{
cmenu = Regex.Match(cline, #"(?<=^\[)[a-zA-Z -\#_{-~\^\r\n]+(?=\])").Value;
if (cmenu != "")
{
mb = this._menuBar.addMenuButton(cmenu);
mb.data["options"] = Regex.Match(cline, #"\/\w+$").Value;
var match = Regex.Match(cline, #"(?<=<)([^>\[\]\r\n]+)(?=>)");
mb.data["count"] = (match.Success ? match.Value : "0");
mb.data["copy"] = "";
applyMenuOptions(mb, false);
}
}
//just a standard line
else
{
cline = cline.Trim();
lhs = Regex.Match(cline, #"^[^\;\<\[\]\r\n]+(?=$|\<|\;)").Value;
if (mb.getSubMenuItem(lhs) == null)
{
var newButton = mb.addSubMenu(lhs);
if (newButton != null)
{
newButton.parent = mb;
newButton.data["options"] = mb.data["options"];
newButton.data["copy"] = Regex.Match(cline, #"((?<=\;)[^\[\]\<\r\n]+(?=<|$))").Value;
var matches = Regex.Match(cline, #"(?<=<)([^>\[\]\r\n]+)(?=>)");
int intout = 0;
if (int.TryParse(matches.Value, out intout))
{//no description
newButton.data["description"] = "";
newButton.data["count"] = intout.ToString();
}
else
{
newButton.data["description"] = matches.Value;
newButton.data["count"] = (matches.NextMatch().Success ? matches.NextMatch().Value : "0");
}
applyMenuOptions(newButton);
newButton.addMiddleClick(this.addcopy);
if (newButton.data["options"].Contains("i"))
{
newButton.addRightClick(this.appendInfo);
newButton.addRightClick(this.increment);
}
}
}
}
}
}
sr.Close();
this._menuBar.squish();
this.Width = this._menuBar.Width+50;
}
else
{
menuList mb = this._menuBar.addMenuButton("menu");
mb.data["options"] = "\\m";
mb.data["count"] = "0";
mb.data["copy"] = "";
mb.data["description"] = "";
applyMenuOptions(mb, false);
saveDictonary();
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to load data " + ex);
//ILog log = LogManager.GetLogger(typeof(Program));
//log.Info(ex);
}
}
public menuList addMenuButton(string s, int w = 0, int h = 0, int x = -1, int y = -1)
{
menuList mb = new menuList(this._form, s);
if (this.menuItems.Exists(z => z.Text == s)) return null;
mb.Width = (w==0?settings.intOf("ButtonWidth"):w);
mb.Height = (h==0?settings.IntOf("ButtonHeight"):h);
if (x == -1 || y == -1)
mb.Location = new Point(this.menuItems.Count > 0 ? this.menuItems.Last().Location.X + this.menuItems.Last().Width : padding);
else mb.Location = new Point(x, y);
mb.BringToFront();
mb.Show();
this.menuItems.Add(mb);
// this.Refresh();
return mb;
}
internal void clear()
{
foreach(var i in this.menuItems)
{
this._form.Controls.Remove(i);
i.clear();
i.Dispose();
}
this.menuItems.Clear();
this._form.Controls.Remove(this);
this.menuItems = new List<menuList>();
this._form.Controls.Add(this);
}
internal void squish()
{
try
{
this.Width = (this.menuItems.Count * this.menuItems.First().Width) + (2 * padding);
}
catch(Exception ex) { MessageBox.Show(""+ex); }
}
Found the culprit, bother the button class and the tool bar class were both adding themselves to the form control instead of button class adding to the tool bar (picture box) controls!
Removing transparency showed the buttons not moving when the tool bar was moved!

Check if specified element is already the logical child of another element

I a beginner in C# and WPF. I'm programming plugin for a node based software called vvvv. I have implemented sliders, buttons and other simple ui elements. The following code shows how a sliders node look in c# :
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;
using VVVV.PluginInterfaces.V2;
namespace VVVV.Packs.UI.Nodes.WPF
{
[PluginInfo(Author = "lecloneur", Category = "WPF", Help = "WPF Slider", Name = "Slider", AutoEvaluate = false)]
public class WPFSlider : GenericNode, IPluginEvaluate
{
[Input("SizeX", DefaultValue = 120, Order = 9, MinValue = 0)]
public IDiffSpread<int> SizeX;
[Input("SizeY", DefaultValue = 120, Order = 9, MinValue = 0)]
public IDiffSpread<int> SizeY;
[Input("Orientation", Order = 1, DefaultEnumEntry = "Horizontal")]
public IDiffSpread<Orientation> OrientationIn;
[Output("Value", Order = 2)]
public ISpread<double> ValueOut;
int elements_count = 0;
public void Evaluate(int SpreadMax)
{
UIElementOut.SliceCount = SpreadMax;
ValueOut.SliceCount = SpreadMax;
for (int i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[0] is Slider) || elements_count < SpreadMax || OrientationIn.IsChanged || SizeX.IsChanged || SizeY.IsChanged)
{
CreateElement(i);
}
OutputData(i);
Transformation(i, (Slider)UIElementOut[i]);
}
elements_count = SpreadMax;
}
private void CreateElement(int i)
{
UIElementOut[i] = new Slider();
var uiElement = (Slider)UIElementOut[i];
uiElement.Minimum = 0;
uiElement.Maximum = 1;
uiElement.Orientation = OrientationIn[i];
uiElement.IsMoveToPointEnabled = true;
uiElement.Width = SizeX[i]; ;
uiElement.Height = SizeY[i];
uiElement.VerticalAlignment = VerticalAlignment.Center;
uiElement.HorizontalAlignment = HorizontalAlignment.Center;
XmlReader XmlRead = XmlReader.Create("Styles/SliderStyle.xaml");
ResourceDictionary myResourceDictionary = (ResourceDictionary)XamlReader.Load(XmlRead);
XmlRead.Close();
Style uiElementStyle = myResourceDictionary["SliderStyle"] as Style;
uiElement.Style = uiElementStyle;
}
private void OutputData(int i)
{
var uiElement = (Slider)UIElementOut[i];
ValueOut[i] = uiElement.Value;
}
}
}
Now I'm trying to implement a tabcontrol where I could dynamically create tabitem and input UIElement into it. As far as I understand, I can only add one things to a tabitem. So I was thinking about creating a grid everytime I need to and fill it with all the incoming UIElement.
public void Evaluate(int SpreadMax)
{
SpreadMax = 1;
UIElementOut.SliceCount = 1;
for (var i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[i] is TabControl))
UIElementOut[i] = new TabControl();
var uiElement = (TabControl)UIElementOut[i];
uiElement.Height = 200;
uiElement.Width = 500;
}
Grid grid;
int[] _elementCounts = new int[_elementInputs.SliceCount];
for (var i = 0; i < _elementInputs.SliceCount; i++)
{
if (_elementInputs[i] == null || !(_elementInputs[i] is UIElement))
{
grid = new Grid();
for (var j = 0; j < _elementInputs[i].IOObject.SliceCount; j++)
{
if (_elementInputs[i].IOObject[j] != null)
{
UIElement test = new UIElement();
test = _elementInputs[i].IOObject[j];
grid.Children.Add(test);
}
}
_elementCounts[i] = _elementInputs[i].IOObject.SliceCount;
ValueOut[i] = _elementCounts[i];
if (((TabControl)UIElementOut[0]).Items.Count <= i)
{
((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = _nameInputs[i].IOObject[0], Content = grid });
}
if (_nameInputs[i].IOObject.IsChanged)
{
((TabItem)((TabControl)UIElementOut[0]).Items[i]).Header = _nameInputs[i].IOObject[0];
}
if (_elementInputs[i].IOObject.IsChanged)
{
((TabItem)((TabControl)UIElementOut[0]).Items[i]).Content = grid;
}
}
}
for (var i = ((TabControl)UIElementOut[0]).Items.Count - 1; ((TabControl)UIElementOut[0]).Items.Count > _elementInputs.SliceCount; i--)
{
((TabControl)UIElementOut[0]).Items.RemoveAt(i);
}
}
I searched a lot but can't find any idea how to solve the error. Apparently adding elements to a the grid throw "specified element is already the logical child of another element";
Hi please try the next method on a several visual objects (child) and check if the resulted object is the same reference. Here is a usefull link with explanations and more...
Extension class code:
public static class VisualTreeHelperExtensions
{
public static T FindParent<T>(this DependencyObject child) where T : DependencyObject
{
while (true)
{
//get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
return parent;
child = parentObject;
}
}
}
Example:
var dataGrid1 = dependencyObject1.FindParent<DataGrid>();
var dataGrid2 = dependencyObject2.FindParent<DataGrid>();
var isSameObject = dataGrid1 == dataGrid2;
Updates
The grid can contains a large number of elements but the only last will be visible to user.
The error is coming from the elemnt you want to add itself, that elemnt is belong to another control (some another control has the element as a child).
Find the parent element of the element you want to add, remove the element from the current parent's children collection, and add this element as the new child of your grid.
Try to use snoop to figure out who is the parent of your element (containing in _elementInputs).
Here are some useful links (first, second).
Update 2
As I can understand you have a third party infrastructure in your project because I can't resolve the type of _elementInputs and UIElementOut collections.
Since the _elementInputs is a field, I still can't understand where is _elementInputs came from (can't see that in code).
The code to add a completely new element is wrong:
Correct code(in my opinion)
//here is in my opinion the correct algorithm in this case
var elementFromInputs = _elementInputs[i] as UIElement;
if (elementFromInputs == null) continue;
//try to find parent of type Panel
var parentPanel = elementFromInputs.FindParent<Panel>();
//try to find parent of type ContentControl
var parentContentControl = elementFromInputs.FindParent<ContentControl>();
if (parentPanel == null && parentContentControl == null)
{
//add if there is no any parents
grid.Children.Add(elementFromInputs);
}
if (parentPanel != null)
{
//remove element from parent's collection
parentPanel.Children.Remove(elementFromInputs);
}
if(parentContentControl != null)
{
//reset parent content to release element
parentContentControl.Content = null;
}
grid.Children.Add(elementFromInputs);
Update 3
You've pasted the code (from the correct code section) inside the if which condition is the _elementInputs[i] == null || !(_elementInputs[i] is UIElement) that means filtering all your UIElements out of the scope. Since I'm not familiar with the vvvv concepts I don't know what do you have inside the _elementInputs array, but if you have the UIElements there you need past the code I gave you before the if with condition _elementInputs[i] == null || !(_elementInputs[i] is UIElement).
Please update your question with the next clarifications:
1. What is inside the _elementInputs[i]?
2. What is inside the _elementInputs[i].IOObject?
3. What are UIElements you want to add to the grid?
4. Please run the next method and write me the comment what do you have in your grid and TabControl controls.
Test code
public void Evaluate(int SpreadMax)
{
SpreadMax = 1;
UIElementOut.SliceCount = 1;
for (var i = 0; i < SpreadMax; i++)
{
if (UIElementOut == null || !(UIElementOut[i] is TabControl))
UIElementOut[i] = new TabControl();
var uiElement = (TabControl)UIElementOut[i];
uiElement.Height = 200;
uiElement.Width = 500;
}
Grid grid = new Grid();
var listOfElements = new List<UIElements>
{
new Button {Background = Brushes.Tomato, Content = "Click Me"},
new Button {Background = Brushes.Yellow, Content = "Click Me"},
new Button {Background = Brushes.Green, Content = "Click Me"},
new Button {Background = Brushes.Blue, Content = "Click Me"}
};
listOfElements.ForEach(button => grid.Children.Add(button));
((TabControl)UIElementOut[0]).Items.Add(new TabItem { Header = "Objects", Content = grid });
}
I'll be glad to help if you will have problems with the code.
Regards.

How to disable MSChart candlestick's automatic "PriceUp/Down" colors and fill when needed?

Is there ANY ways possible to manipulate, maybe even override the way the candlestick charts are colored?
What I'm trying to do, should look like this:
What happens here, is that the candles are filled with its color when the "Open" prices are higher than "Close" prices, and black when it's the other way around.
But this isn't possible in MS Chart? I was thinking about creating another candlestick series on top of the other, and get filled boxes from that, but it wouldn't work, since MS Chart automatically uses the "PriceDown/Up" colors. ...See my answer below.
Here's the code I use and which doesn't work the way I would like to:
DataPointCollection dpcCandle = Series[Candle].Points;
DataPointCollection dpcPrice = Series[Price].Points;
int candleCount = dpcCandle.Count;
dpcCandle.AddXY(time, high);
dpcCandle[candleCount].YValues[1] = low;
dpcCandle[candleCount].YValues[2] = open;
dpcCandle[candleCount].YValues[3] = close;
if( candleCount > 1 )
{
Color col;
float oldClose = (float)dpcCandle[candleCount-1].YValues[3];
float oldOpen = (float)dpcCandle[candleCount-1].YValues[2];
dpcCandle[candleCount].BorderWidth = 1;
// Determine color
if( oldClose <= close) // Price up!
col = Color.ForestGreen;
else // Price down...
col = Color.Red;
// Determine fill
if( open < close )
{
dpcCandle[candleCount].Color = Color.Black;
dpcCandle[candleCount].BorderColor = col;
}
else
{
dpcCandle[candleCount].Color = col;
dpcCandle[candleCount].BorderColor = col;
}
if( oldOpen.Equals(close) )
{
dpcCandle[candleCount].Color = dpcCandle[candleCount-1].Color;
dpcCandle[candleCount].BorderColor = dpcCandle[candleCount-1].BorderColor;
}
}
Well, it works, but the result looks like this:
...with these settings:
Series[Candle]["PriceUpColor"] = "Black";
Series[Candle]["PriceDownColor"] = "Black";
And when I change those to these:
Series[Candle]["PriceUpColor"] = "ForestGreen";
Series[Candle]["PriceDownColor"] = "Red";
...I get this ugly mess for a result:
Thanks in advance.
Ok, so I was able to do workaround which includes a total of three two Candlestick Serieses... It's kinda ugly, but it does what I wanted.
The setup:
// ****************************
// Candlestick setup
Series[Candle].ChartType = SeriesChartType.Candlestick;
Series[Candle]["OpenCloseStyle"] = "Triangle";
Series[Candle]["ShowOpenClose"] = "Both";
Series[Candle]["PriceUpColor"] = "Black";
Series[Candle]["PriceDownColor"] = "Black";
Series[Candle]["PointWidth"] = "0.75";
Series[CandleGreen].ChartType = SeriesChartType.Candlestick;
Series[CandleGreen]["OpenCloseStyle"] = "Triangle";
Series[CandleGreen]["ShowOpenClose"] = "Both";
Series[CandleGreen]["PriceUpColor"] = "ForestGreen";
Series[CandleGreen]["PriceDownColor"] = "Red";
Series[CandleGreen]["PointWidth"] = "0.75";
Series[CandleGreen].BorderColor = Color.Transparent;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Then I did this:
DataPointCollection dpcCandle = Series[Candle].Points;
DataPointCollection dpcCandleG = Series[CandleGreen].Points;
DataPointCollection dpcPrice = Series[Price].Points;
// Point count
int candleCount = dpcCandle.Count;
// Add the new point
dpcCandle.AddXY(time, high);
dpcCandle[candleCount].YValues[1] = low;
dpcCandle[candleCount].YValues[2] = open;
dpcCandle[candleCount].YValues[3] = close;
// Add the hacks...
int g = dpcCandleG.AddXY(dpcCandle[candleCount].XValue, 0);
dpcCandleG[g].YValues[1] = 0;
dpcCandleG[g].YValues[2] = 0;
dpcCandleG[g].YValues[3] = 0;
if( candleCount > 1 )
{
float oldClose = (float)dpcCandle[candleCount-1].YValues[3];
float oldOpen = (float)dpcCandle[candleCount-1].YValues[2];
dpcCandle[candleCount].BorderWidth = 1;
// Determine color
if( oldClose <= close) // Price up!
{
dpcCandle[candleCount].Color = Color.ForestGreen;
dpcCandle[candleCount].BorderColor = Color.ForestGreen;
}
else // Price down...
{
dpcCandle[candleCount].Color = Color.Red;
dpcCandle[candleCount].BorderColor = Color.Red;
}
// Determine fill
if( open > close )
{
if( dpcCandle[candleCount].Color == Color.Red )
{
dpcCandleG[g].YValues[2] = open;
dpcCandleG[g].YValues[3] = close;
}
else if( dpcCandle[candleCount].Color == Color.ForestGreen )
{
dpcCandleG[g].YValues[2] = close;
dpcCandleG[g].YValues[3] = open;
}
}
if( oldOpen.Equals(close) )
{
dpcCandle[candleCount].Color = dpcCandle[candleCount-1].Color;
dpcCandle[candleCount].BorderColor = dpcCandle[candleCount-1].BorderColor;
}
}
Result:
Setting PriceUpColor and PriceDownColor is necessary and correct. Why do you set any other colors?? All candle coloring should happen automatically!
If you want to style the vertical bar in a Series "price" of a Chart chart1 you can do it by setting the DataPoint.Color like this:
chart1.Series["price"].BorderWidth = 4;
foreach(DataPoint dp in chart1.Series["price"].Points)
{
dp.Color = (dp.YValues[2] > dp.YValues[3]) ? Color.CadetBlue : Color.Pink;
}
I took the code and the data from this post by zeFrenchy. Here is the result:
I added the loop after adding the data. But you can do it while adding points as well..

RichTextBox syntax highlighting

I am working with this code. It is for syntax highlighting in a RichTextBox. I am specifically looking at the function ProcessLine() and OnTextChanged(), which I have modified as such:
protected override void OnTextChanged(EventArgs e)
{
// Calculate stuff here.
m_nContentLength = this.TextLength;
int nCurrentSelectionStart = SelectionStart;
int nCurrentSelectionLength = SelectionLength;
m_bPaint = false;
// Find the start of the current line.
m_nLineStart = nCurrentSelectionStart;
while ((m_nLineStart > 0) && (Text[m_nLineStart - 1] != '\n'))
m_nLineStart--;
// Find the end of the current line.
m_nLineEnd = nCurrentSelectionStart;
while ((m_nLineEnd < Text.Length) && (Text[m_nLineEnd] != '\n'))
m_nLineEnd++;
// Calculate the length of the line.
m_nLineLength = m_nLineEnd - m_nLineStart;
// Get the current line.
m_strLine = Text.Substring(m_nLineStart, m_nLineLength);
// Process this line.
ProcessLine();
m_bPaint = true;
}
// Process a line.
private void ProcessLine()
{
// Save the position and make the whole line black
int nPosition = SelectionStart;
SelectionStart = m_nLineStart;
SelectionLength = m_nLineLength;
SelectionColor = Color.Black;
/*// Process the keywords
ProcessRegex(m_strKeywords, Settings.KeywordColor);
// Process numbers
if(Settings.EnableIntegers)
ProcessRegex("\\b(?:[0-9]*\\.)?[0-9]+\\b", Settings.IntegerColor);
// Process strings
if(Settings.EnableStrings)
ProcessRegex("\"[^\"\\\\\\r\\n]*(?:\\\\.[^\"\\\\\\r\\n]*)*\"", Settings.StringColor);
// Process comments
if(Settings.EnableComments && !string.IsNullOrEmpty(Settings.Comment))
ProcessRegex(Settings.Comment + ".*$", Settings.CommentColor);*/
SelectionStart = nPosition;
SelectionLength = 0;
SelectionColor = Color.Red;
m_nCurSelection = nPosition;
}
My first question is, when I enter into the ProcessLine() in OnTextChanged(), will I always have a newline character at the end of m_strLine? Will the smallest value or m_strLine be "\n" and the largest "any#ofchars+\n"?
And just so I have this right, SelectionStart is my caret position if SelectionLength is zero, and if SelectionLength is greater than zero my caret is at SelectStart+SelectionLength?
I am trying to modify this code to color a whole lot of different syntax expressions, and I plan to go through it one character at a time, for each line. How might this fair when pasting or loading a file of 20k+ lines?
All I can suggest you right now is to use something stable, more powerful and less error prone such as Scintilla for .NET and Color Code. These controls are free and open source. Try them out:
ScintillaNETColorCode - Syntax Highlighting/Colorization for .NET
RichTextBox is extremely inefficient for working with large text. Even if you get some decent highlighting, the performance issues will start to pop up pretty soon.
This is going to scale really badly. You should do what DelegateX suggests if your goal is simply a functioning application; if you're in this to learn how, start by figuring out ways to lower the amount of work that gets done. To that end, here are some general pointers:
Only highlighting text that's inside the window will be a massive improvement that doesn't have any visual side-effects - its probably also workable to break the text into blocks (by function, method, class, etc) and only highlight visible blocks, even the occluded portions, to avoid issues where an offset starting position affects the highlight. If you don't do this, you will run into situations where the first rendered line is partway through an if or parenthesis block, and you have an unbalanced syntax tree as a result.
You still won't be able to handle 20k lines with a RichTextBox control, but a few thousand should be speedy.
Facing the same issue and failed to find "5 minutes ready to go" solution, I have developed my own RichTextBox extension to highlight XML.
I developed it really quick because of time pressure and didn't have the time to revised it - so feel free to refine it.
Just copy & paste the extension code to use with your RichTextBox or copy the whole
application code including synchronous & asynchronous usage
Extension Method
// Use for asynchronous highlight
public delegate void VoidActionOnRichTextBox(RichTextBox richTextBox);
// Extension Class
public static class RichTextBoxExtensions
{
public static void HighlightXml(this RichTextBox richTextBox)
{
new StandardHighlight().HighlightXml(richTextBox);
}
public async static void HighlightXmlAsync(this RichTextBox richTextBox)
{
var helper = new StandardHighlight();
var win = new MainWindow();
await Task.Factory.StartNew(() =>
{
richTextBox.Dispatcher.BeginInvoke(new VoidActionOnRichTextBox(helper.HighlightXml), richTextBox);
});
}
}
// You can extent it with more highlight methods
public class StandardHighlight
{
public void HighlightXml(RichTextBox richTextBox)
{
// Collect Text-Box Information
var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
XDocument xDocument;
try
{
xDocument = XDocument.Parse(textRange);
}
catch
{
return;
}
var documentLines = xDocument.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
// Get the Longest Line Length
int? maxVal = null;
for (int i = 0; i < documentLines.Length; i++)
{
int thisNum = documentLines[i].Length;
if (!maxVal.HasValue || thisNum > maxVal.Value) { maxVal = thisNum; }
}
// Set Text-Box Width & Clear the Current Content
if (maxVal != null) richTextBox.Document.PageWidth = (double)maxVal * 5.5;
richTextBox.Document.Blocks.Clear();
#region *** Process Lines ***
foreach (var documentLine in documentLines)
{
// Parse XML Node Components
var indentSpace = Regex.Match(documentLine, #"\s+").Value;
var xmlTags = Regex.Matches(documentLine, #"(<[^/].+?)(?=[\s])|(<[^/].+?>)|(</.+?>)");
if (documentLine.Contains("<!--")) xmlTags = Regex.Matches(documentLine, #"(<[^/].+?>)"); // Parse comments
var nodeAttributes = Regex.Matches(documentLine, #"(?<=\s)(.+?)(?=\s)");
// Process XML Node
var nodeAttributesCollection = new List<Run>();
if (nodeAttributes.Count > 0)
{
for (int i = 0; i < nodeAttributes.Count; i++)
{
if (!(nodeAttributes[i].Value.Length < 2) && !(documentLine.Contains("<!--")))
{
var attributeName = $"{Regex.Match(nodeAttributes[i].Value, #"(.+?=)").Value}";
if (i == 0) attributeName = $" {Regex.Match(nodeAttributes[i].Value, #"(.+?=)").Value}";
var attributeValue = $"{Regex.Match(nodeAttributes[i].Value, #"(?<=(.+?=))"".+?""").Value} ";
if (i == nodeAttributes.Count - 1) attributeValue = attributeValue.Trim();
nodeAttributesCollection.Add(new Run { Foreground = new SolidColorBrush(Colors.Green), Text = $"{attributeName}" });
nodeAttributesCollection.Add(new Run { Foreground = new SolidColorBrush(Colors.Brown), Text = $"{attributeValue}" });
}
}
}
// Initialize IndentSpace
Run run = null;
if (indentSpace.Length > 1) run = new Run { Text = indentSpace };
// Initialize Open Tag
var tagText = xmlTags[0].Value.Substring(1, xmlTags[0].Value.Length - 2);
var tagTextBrush = new SolidColorBrush(Colors.Blue);
var tagBorderBruh = new SolidColorBrush(Colors.Red);
if (tagText.StartsWith("!--"))
{
tagTextBrush = new SolidColorBrush(Colors.DarkSlateGray);
tagBorderBruh = new SolidColorBrush(Colors.DarkSlateGray);
}
var openTag = new Run
{
Foreground = tagTextBrush,
Text = tagText
};
// Initialize Content Tag
var content = new Run
{
Foreground = new SolidColorBrush(Colors.Black),
};
// Initialize Paragraph
var paragraph = new Paragraph();
paragraph.Margin = new Thickness(0);
if (run != null) paragraph.Inlines.Add(run); // Add indent space if exist
// Process Open Tag
paragraph.Inlines.Add(new Run { Foreground = tagBorderBruh, Text = "<" });
paragraph.Inlines.Add(openTag);
// Process Open Tag Attributes
if (nodeAttributesCollection.Count > 0)
{
nodeAttributesCollection.ForEach(attribute => { paragraph.Inlines.Add(attribute); });
nodeAttributesCollection.Clear();
}
paragraph.Inlines.Add(new Run { Foreground = tagBorderBruh, Text = ">" });
// Process Closing Tag
if (xmlTags.Count > 1)
{
Run closingTag = new Run();
content.Text = documentLine.Replace(xmlTags[0].Value, "").Replace(xmlTags[1].Value, "").Trim();
closingTag = new Run
{
Foreground = new SolidColorBrush(Colors.Blue),
Text = xmlTags[1].Value.Substring(1, xmlTags[1].Value.Length - 2)
};
paragraph.Inlines.Add(content);
paragraph.Inlines.Add(new Run { Foreground = new SolidColorBrush(Colors.Red), Text = "<" });
paragraph.Inlines.Add(closingTag);
paragraph.Inlines.Add(new Run { Foreground = new SolidColorBrush(Colors.Red), Text = ">" });
}
richTextBox.Document.Blocks.Add(paragraph);
}
#endregion
}
}
Fixed version - with handling JSON as inner text and better elements extraction
public static void HighlightXml(this RichTextBox richTextBox)
{
// Collect Text-Box Information
var textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;
XmlDocument xmlDocument = new XmlDocument();
try
{
xmlDocument.LoadXml(textRange.Trim());
}
catch
{
return;
}
var documentLines = xmlDocument.OuterXml.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
// Get the Longest Line Length
int? maxVal = null;
for (int i = 0; i < documentLines.Length; i++)
{
int thisNum = documentLines[i].Length;
if (!maxVal.HasValue || thisNum > maxVal.Value) { maxVal = thisNum; }
}
// Set Text-Box Width & Clear the Current Content
if (maxVal != null) richTextBox.Document.PageWidth = (double)maxVal * 10;
richTextBox.Document.Blocks.Clear();
#region *** Process Lines ***
foreach (var documentLine in documentLines)
{
// Parse XML Node Components
var indentSpace = Regex.Match(documentLine, #"\s+").Value;
var xmlTags = Regex.Matches(documentLine, #"(?<=<)[^>\s+]*");
if (documentLine.Contains("<!--")) xmlTags = Regex.Matches(documentLine, #"(<[^/].+?>)");
var nodeAttributes = Regex.Matches(documentLine, #"(?<=\s)[^><:\s]*=*(?=[>,\s])");
// Process XML Node
var nodeAttributesCollection = new List<Run>();
if (nodeAttributes.Count > 0)
{
for (int i = 0; i < nodeAttributes.Count; i++)
{
if (!(nodeAttributes[i].Value.Length < 2) && !(documentLine.Contains("<!--")))
{
var attributeName = $"{Regex.Match(nodeAttributes[i].Value, #"(.+?=)").Value}";
if (i == 0) attributeName = $" {Regex.Match(nodeAttributes[i].Value, #"(.+?=)").Value}";
var attributeValue = $"{Regex.Match(nodeAttributes[i].Value, #"(?<=(.+?=))"".+?""").Value} ";
if (i == nodeAttributes.Count - 1) attributeValue = attributeValue.Trim();
nodeAttributesCollection.Add(new Run { Foreground = new SolidColorBrush(Colors.Green), Text = $"{attributeName}" });
nodeAttributesCollection.Add(new Run { Foreground = new SolidColorBrush(Colors.Brown), Text = $"{attributeValue}" });
}
}
}
// Initialize IndentSpace
Run run = null;
if (indentSpace.Length > 1) run = new Run { Text = indentSpace };
// Initialize Open Tag
var tagText = xmlTags[0].Value;//.Substring(1, xmlTags[0].Value.Length - 2);
var tagTextBrush = new SolidColorBrush(Colors.Blue);
var tagBorderBruh = new SolidColorBrush(Colors.Red);
if (tagText.StartsWith("!--"))
{
tagTextBrush = new SolidColorBrush(Colors.DarkSlateGray);
tagBorderBruh = new SolidColorBrush(Colors.DarkSlateGray);
}
var openTag = new Run
{
Foreground = tagTextBrush,
Text = tagText
};
// Initialize Content Tag
var content = new Run
{
Foreground = new SolidColorBrush(Colors.Black),
};
// Initialize Paragraph
var paragraph = new Paragraph();
paragraph.Margin = new Thickness(0);
if (run != null) paragraph.Inlines.Add(run); // Add indent space if exist
// Process Open Tag
paragraph.Inlines.Add(new Run { Foreground = tagBorderBruh, Text = "<" });
paragraph.Inlines.Add(openTag);
// Process Open Tag Attributes
if (nodeAttributesCollection.Count > 0)
{
nodeAttributesCollection.ForEach(attribute => { paragraph.Inlines.Add(attribute); });
nodeAttributesCollection.Clear();
}
paragraph.Inlines.Add(new Run { Foreground = tagBorderBruh, Text = ">" });
// Process Closing Tag
if (xmlTags.Count > 1)
{
Run closingTag = new Run();
content.Text = documentLine.Replace($"<{xmlTags[0].Value}>", "").Replace($"<{xmlTags[1].Value}>", "").Trim();
closingTag = new Run
{
Foreground = new SolidColorBrush(Colors.Blue),
Text = xmlTags[1].Value.Substring(1, xmlTags[1].Value.Length - 1)
};
paragraph.Inlines.Add(content);
paragraph.Inlines.Add(new Run { Foreground = new SolidColorBrush(Colors.Red), Text = "<" });
paragraph.Inlines.Add(closingTag);
paragraph.Inlines.Add(new Run { Foreground = new SolidColorBrush(Colors.Red), Text = ">" });
}
richTextBox.Document.Blocks.Add(paragraph);
}
#endregion
}

Categories

Resources