I currently have an TChart where I want to introduce a draggable horizontal line which changes the color of the points below the line. I have chosen to use ColorLine for this purpose but the line does not appear in the TChart. Am I using the right TChart tool for the job or am I missing something?
Below is the stripped down version of my current code.
public class testClass
{
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public testClass()
{
line = new Colorline();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
}
Code below works fine for me here. If your problem persists please send a Short, Self Contained, Correct (Compilable), Example project to reproduce the problem here. You can post your files at www.steema.net/upload.
using Steema.TeeChart;
using Steema.TeeChart.Styles;
using Steema.TeeChart.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
testClass();
//existing code which assigns colors
tChart1.Series.Add(new Steema.TeeChart.Styles.Bar()).FillSampleValues();
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
tChart1.Tools.Add(line);
line.Active = true;
line.Axis = tChart1.Axes.Left;
line.Value = lineYVal;
}
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public void testClass()
{
line = new ColorLine();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
if (savedChart != null)
{
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
else
{
foo(tChart1);
}
}
}
}
It turns out that while the line was being added correctly, the code I was using to display the chart was not updating the chart properly and was displaying a version of the chart from before it was passed into my highlight function.
Related
I have a customTextBox1 on a Form.
customTextbox1 multiline is false and TextAlign is set to center.MaxLength is 23. And the customTextBox1 width is 92.customTextBox1 Font is set to "MS ゴシック", 12F.
When I type "12345678901234567890123" in the TextBox, the text is scrolling to the last character.Also,when I click on the text , the text is highlighted blue and I can drag to the left and right of the text.
.NetFramework 3.5
What I want is 2 things:
1)when the text is longer than TextBox width, I don't want to scroll to the last character.I want to stop scrolling at the right margin of the TextBox.
for example,
when I type "1234567890123456", I want to show "12345678901"and the rest of the overflow text should not be shown.
2)when I Click and Drag the text, I want to show "12345678901" only
And want to get rid of the blue highlighted selection too.
1)overflow text is showing
2)i can click and drag to the end of the text and beginning of the text
here is my code
CustomTextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class CustomTextBox : TextBox
{
public override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
public CustomTextBox()
{
InitializeComponent();
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
customTextBox1.AutoSize = true;
customTextBox1.Size = new Size(92,21);
customTextBox1.Multiline = false;
customTextBox1.TextAlign = HorizontalAlignment.Center;
customTextBox1.MaxLength = 23;
customTextBox1.Font = new Font("MS ゴシック", 12F);
}
}
}
Update1 :
I want to do this strange behavior of textBox because I am making a exact replica of an application written in other language which is not supported anymore. So we have to write it in C#. Both application will run on windows.
In the old application, there is a textBox in which user can type in ID numbers.
1)That textBox does not show overflow text.
If I type ("12345678901234567890123") ,it only show "12345678901" but if I click backspace [13]times, the text begins "1234567890". so I know the overflow text are there just not showing.
2)I can't click and drag the text right and left as in C# textBox.
I have manage to replicate No.1 behavior though.
Here is my code
CustomTextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.InteropServices;
namespace DisabledTextSelectForm
{
public partial class CustomTextBox : TextBox
{
public override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
public bool DisabledScrolling { get; set; }
int caretPos = 0;
public CustomTextBox()
{
InitializeComponent();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
var isDigit = char.IsDigit(e.KeyChar);
var isBackSpace = e.KeyChar == (char)Keys.Back;
var diffWidth = 0;
if (Text.Length >= 2)
{
var firstChar = TextRenderer.MeasureText(Text[0].ToString(), Font);
var secondChar = TextRenderer.MeasureText(Text.Substring(0, 2).ToString(), Font);
diffWidth = secondChar.Width - firstChar.Width;
caretPos = Width / diffWidth;
}
if (caretPos != 0 && Text.Length >= caretPos && DisabledScrolling)
{
if (isDigit)
{
Text = Text.Length < MaxLength ? Text + e.KeyChar.ToString() : Text;
}
else if (isBackSpace)
{
Text = Text.Substring(0,Text.Length - 1);
}
ScrollTo(caretPos - 1);
e.Handled = true;
}
base.OnKeyPress(e);
}
private void ScrollTo(int scrollPosition)
{
if (Text.Length >= scrollPosition)
{
Select(scrollPosition, 0);
ScrollToCaret();
}
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
customTextBox1.DisabledScrolling = true;
customTextBox1.AutoSize = true;
customTextBox1.Size = new Size(92,21);
customTextBox1.Multiline = false;
customTextBox1.TextAlign = HorizontalAlignment.Center;
customTextBox1.MaxLength = 23;
customTextBox1.Font = new Font("MS ゴシック", 12F);
}
}
}
I know how to disable clicking and draging of Text in TextBox.
Add a Timer then set it Enable with 10 ms interval and in Tick event of your Timer put this code:
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
In MouseMove event of your TextBox put this code:
private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
In KeyDown event of your TextBox put this code:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
}
Add this code in initialize block
public Form1()
{
InitializeComponent();
textBox1.HideSelection = true;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyTest
{
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private GroupBox lastGB = null;
public Form1()
{
InitializeComponent();
GroupBox gb = new GroupBox();
gb.Location = new Point(100, (lastGB == null ? startingY : lastGB.Bounds.Bottom));
gb.Size = new Size(1220, 400);
gb.BackColor = SystemColors.Window;
gb.Text = "";
gb.Font = new Font("Colonna MT", 12);
this.Controls.Add(gb);
}
It's creating a small line on the top of the groupbox and I don't want this line to show.
And I want to write some text on it in the middle of it.
How can I make it just complete white ? And how to write some text in the middle on the groupbox ?
The main idea is to create over the form a white sheet or box with text inside that's it.
It looks like your intention is to put subsequent boxes just below the last box. As mentioned, a Label would probably be best. I'd also move that code to a method you can call over and over to keep from repeating code elsewhere. You could pass a message to display in the Label. Also, don't forget to update the reference to the "last box" when ever you create a new one:
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private Label lastLbl = null;
public Form1()
{
InitializeComponent();
AddLabel("Hello World");
}
private void button1_Click(object sender, EventArgs e)
{
AddLabel(DateTime.Now.ToString());
}
private void AddLabel(String msg)
{
Label lbl = new Label();
lbl.Location = new Point(100, (lastLbl == null ? startingY : lastLbl.Bounds.Bottom));
lbl.Size = new Size(1220, 400);
lbl.BackColor = Color.White;
lbl.Text = msg;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Font = new Font("Colonna MT", 12);
this.Controls.Add(lbl);
lastLbl = lbl;
}
}
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;
public class Manager : EditorWindow
{
private static bool red;
[MenuItem("Tools/Manager")]
static void Managers()
{
EditorWindow.GetWindow<Manager>();
Init();
}
private static void Init()
{
red = true;
}
private void OnGUI()
{
GUIContent c = new GUIContent();
var style = new GUIStyle(GUI.skin.button);
if (red)
{
style.normal.textColor = Color.red;
}
else
{
style.normal.textColor = Color.green;
}
style.fontSize = 18;
string[] Paths = new string[2];
string[0] = "testing1";
string[1] = "testing2";
for (int i = 0; i < Paths.Length; i++)
{
if (Paths[i].Contains("test")) // or .js if you want
{
var x = assetPaths[i].LastIndexOf("/");
var t = assetPaths[i].Substring(x + 1);
GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50));
if (Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
{
OnMouseOver(i);
}
}
}
}
private void OnMouseOver(int buttinIndex)
{
UnityEngine.Debug.LogFormat("OnMouseOver {0}", buttinIndex);
red = !red;
}
}
The problem is when I move the mouse cursor over one of the buttons it's coloring all the buttons in green then back to red.
I want that when I click on specific button or move the mouse cursor over a specific button color only this button in green and leave the button in green.
You should record the style for each button use List or something else.
At current you code lead all button use one same style so they change together.
I supply a tip. I hope this can help you.
private void OnMouseOver(int buttinIndex)
{
UnityEngine.Debug.LogFormat("OnMouseOver {0}", buttinIndex);
//red = !red;
//try to record style or color flag for each button use index.
}
and when you draw button
GUILayout.Button(t, GetStyleFromList(i), GUILayout.Width(1000), GUILayout.Height(50));
I need to draw a graph in C# by ZedGraph in VS2013.
I have created a windowns application project and added Form1.cs as win forms file.
But, when I ran the code, only an empty form was created but no graph. In debug mode, I found that Form1_Load() is not executed. Why ?
This is the C# code.
using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// it is not executed.
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
CreateChart(zg1);
}
// definition of CreateChart
...
}
Any help would be appreciated.
UPDATE
I have tried the solution but I only got an empty form and no charts displayed in the form.
This is code of From1.cs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace zedgraph_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
CreateChart(zg1);
SetSize();
}
private void Form1_Resize(object sender, EventArgs e)
{
SetSize();
}
private void SetSize()
{
zg1.Location = new Point(10, 10);
// Leave a small margin around the outside of the control
zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 20);
}
// Call this method from the Form_Load method, passing your ZedGraphControl
public static void CreateChart(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
// Set the title and axis labels
myPane.Title.Text = "Vertical Bars with Value Labels Above Each Bar";
myPane.XAxis.Title.Text = "Position Number";
myPane.YAxis.Title.Text = "Some Random Thing";
PointPairList list = new PointPairList();
PointPairList list2 = new PointPairList();
PointPairList list3 = new PointPairList();
Random rand = new Random();
// Generate random data for three curves
for (int i = 0; i < 5; i++)
{
double x = (double)i;
double y = rand.NextDouble() * 1000;
double y2 = rand.NextDouble() * 1000;
double y3 = rand.NextDouble() * 1000;
list.Add(x, y);
list2.Add(x, y2);
list3.Add(x, y3);
}
// create the curves
BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);
// Fill the axis background with a color gradient
myPane.Chart.Fill = new Fill(Color.White,
Color.FromArgb(255, 255, 166), 45.0F);
zgc.AxisChange();
// expand the range of the Y axis slightly to accommodate the labels
myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;
// Create TextObj's to provide labels for each bar
BarItem.CreateBarLabels(myPane, false, "f0");
//Console.ReadLine();
}
private void zg1_Load(object sender, EventArgs e)
{
}
}
}
More update
I have added zedgraphControl by adding zedgraph.dll under object relational design in toolbox of VS2013.
But, all components in my toolbox are all grayed out in VS2013.
When I ran the code, I still got an empty form.
I tried the project at http://www.codeproject.com/Articles/5431/A-flexible-charting-library-for-NET
I can get the chart even though all components in my toolbox are also all grayed out in VS2013.
Remove the following lines from Form1_Load and you are good to go:
Console.WriteLine("Form1_Load is called");
ZedGraph.ZedGraphControl zg1 = new ZedGraphControl();
I guess you didn't add the event handler to Form1_Load event in Form1's Properties window (at the right bottom of the designer window), you can either do that or move CreateChart(zg1) into Form1's constructor, like this:
public Form1()
{
InitializeComponent();
CreateChart(zg1);
}
I'm trying to make a simple text editor that colors text in real time. I also must use DLL and Reflection for this.
I want to color the text while user typing. For that reason I have a checkbox. If it checked the text will be colored while user is typing (Real Time).
I've wrote a DLL file to do that.
Anyway, I'm very new to reflection thing.
The question:
I would want to ask you guys for your professional advice whether what I've wrote can be called "using reflection" or not? and if it's not, can point me what is wrong?
Here is my code (I've removed many things from it so the code will reflect the question but there might be leftovers)
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
namespace Editor
{
public class MainForm : Form
{
//Declaration of Controls
private RichTextBox EditRichTextBox;
private CheckBox chkBox;
private int flag = 0;
private Button[] PlugButton;
public string[] PluginNames;
private int NumofPlugins;
public MainForm()
{
//Initialization of Controls
this.EditRichTextBox = new RichTextBox();
this.ErrorTextBox = new RichTextBox();
this.chkBox = new CheckBox();
//Form
this.ClientSize = new Size(700, 500);
this.Name = "MainForm";
this.Text = "C# Editor";
//EditRichTextBox
this.EditRichTextBox.Location = new Point(20, 20);
this.EditRichTextBox.Name = "EditRichTextBox";
this.EditRichTextBox.Size = new Size(this.Width - 150, 300);
this.EditRichTextBox.AcceptsTab = true;
this.EditRichTextBox.Multiline = true;
//Controls on the Form
this.Controls.Add(this.ButtonCompilelib);
this.Controls.Add(this.ButtonCompile);
this.Controls.Add(this.ButtonRun);
this.Controls.Add(this.EditRichTextBox);
this.Controls.Add(this.ErrorTextBox);
this.Controls.Add(this.chkBox);
//CheckBox
this.chkBox.Location = new Point(600,300);
this.chkBox.Name = "chkBox";
this.chkBox.Text = "Color";
};
//My checkbox handler
this.chkBox.Click += (sender,e) =>
{
if(flag == 0)
{
flag = 1;
MessageBox.Show("Coloring Text");
}
else
flag = 0;
};
//My TextBox handler
this.EditRichTextBox.KeyPress += (sender,e) =>
{
try
{
string tmp = Environment.CurrentDirectory + "\\" + "mydll" + ".dll"; Assembly a = Assembly.LoadFrom(tmp);
Type t = a.GetType("MyPlugIn.colorclass");
MethodInfo mi = t.GetMethod("color");
Object obj = Activator.CreateInstance(t);
Object[] Params = new Object[5];
Params[0] = EditRichTextBox.Text;
Params[1] = EditRichTextBox.Handle;
Params[2] = ErrorTextBox.Handle;
Params[3] = EditRichTextBox;
Params[4] = flag;
mi.Invoke(obj, Params);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
};
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new MainForm());
}
}
}
And this is the DLL file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace MyPlugIn
{
public class colorclass
{
public void color(string Text, Object Hndl_Text, Object Hndl_Err, RichTextBox box,int flag)
{
if (flag == 1)
{
int start = box.TextLength;
int end = box.TextLength;
//Textbox may transform chars, so (end-start) != text.Length
box.Select(start, end - start);
{
box.SelectionColor = Color.Blue;
}
box.SelectionLength = 0; // clear
}
}
}
}
Yes, your code uses Reflection. These lines are an example:
Type t = a.GetType("MyPlugIn.colorclass");
MethodInfo mi = t.GetMethod("color");
Object obj = Activator.CreateInstance(t);
Whether is the best approach or not, or whether it's necessary for this task, it's a different topic.