I want fill my treeview with >30000 nodes in multi level.
I use this code for fill it, but treeview get over 30 seconds time for fill it.
How can I use multi thread for increase speed to fill it ?
//in my windows form :
private void ThesaurusFrm_Load(object sender, EventArgs e)
{
TreadPopulateTree();
}
private void TreadPopulateTree()
{
System.Threading.Thread TrPopulateTree = new System.Threading.Thread(new System.Threading.ThreadStart(PopulateTree));
TrPopulateTree.Start();
}
private void PopulateTree()
{
FillTree.TvTrees = TvThesaurusNodes;
FillTree Ft = new FillTree();
var TreeItems = Db.LoadThesaurusTrees();
if (TvThesaurusNodes.InvokeRequired)
TvThesaurusNodes.BeginInvoke(new MethodInvoker(() =>
{
Ft.LoadTrees(TreeItems);
}));
}
//in my FillTree class
public static TreeView TvTrees;
List<TreesFieldSet> TreeItems = new List<TreesFieldSet>();
public void LoadTrees(List<TreesFieldSet> TreeItems)
{
this.TreeItems = TreeItems;
TvTrees.Nodes.Clear();
TvTrees.ShowPlusMinus = true;
TvTrees.FullRowSelect = true;
TvTrees.Nodes.Clear();
TvTrees.BeginUpdate();
LoadBaseNodes();
TvTrees.EndUpdate();
TvTrees.Refresh();
}
private void LoadBaseNodes()
{
if (TreeItems != null)
{
int BaseParent = 0;
TreeNode Node;
foreach (TreesFieldSet Tfs in TreeItems)
{
if (Tfs.ParentId < BaseParent)
BaseParent = Tfs.ParentId;
}
foreach (TreesFieldSet Tfs in TreeItems)
{
if (Tfs.ParentId == BaseParent)
{
Node = TvTrees.Nodes.Add(Tfs.Title);
Node.Tag = Tfs.TreeId;
GetChildren(Node);
}
}
}
}
private void GetChildren(TreeNode Node)
{
TreeNode ChNode = null;
Int32 nodeCat = (Int32)Node.Tag;
foreach (TreesFieldSet Tfs in TreeItems)
{
if (Tfs.ParentId == nodeCat)
{
ChNode = Node.Nodes.Add(Tfs.Title);
ChNode.Tag = Tfs.TreeId;
GetChildren(ChNode);
}
}
}
No never do multi-threading with UI, Reading is not a problem in some case but writing is always a problem.
You can defer the loading, i.e Load only what user can see, then load the children only when user clicks the node. I've answered a similar question here see if that helps.
Typically you load only a "Node" and its "Immediate children" no more no less.
Hope this helps.
Related
I'm making a retro-style game with C# .NET-Framework, and for dialogue I'm using a for-statement, that prints my text letter by letter (like a typewriter-effect):
I'm working with different scenes, and I have a skip button (bottom right) that skips the current dialogue and passes to the next scene. My typewriter-effect automatically stops when all the text is displayed, but when I click on the skip button, it automatically skips to the next scene.
I would like it, when the typewriter is still active, and if I click on the skip button, that it first shows all the text, instead of skipping to the next scene.
So that it only skips to the next scene when all the text is displayed (automatically or manually).
This is the (working code) that I'm using for my typewriter method (+ variables):
public string FullTextBottom;
public string CurrentTextBottom = "";
public bool IsActive;
public async void TypeWriterEffectBottom()
{
if(this.BackgroundImage != null) // only runs on backgrounds that arent black
{
for(i=0; i < FullTextBottom.Length + 1; i++)
{
CurrentTextBottom = FullTextBottom.Substring(0, i); // updating current string with one extra letter
LblTextBottom.Text = CurrentTextBottom; // "temporarily place string in text box"
await Task.Delay(30); // wait for next update
#region checks for IsActive // for debugging only!
if(i < FullTextBottom.Length + 1)
{
IsActive = true;
Debug1.Text = "IsActive = " + IsActive.ToString();
}
if(CurrentTextBottom.Length == FullTextBottom.Length)
{
IsActive = false;
Debug1.Text = "IsActive = " + IsActive.ToString();
}
#endregion
}
}
}
And this is the code that I want to get for my skip button (named Pb_FastForward):
private void PbFastForward_Click(object sender, EventArgs e)
{
if( //typewriter is active)
{
//print all text into the textbox
}
else if( //all text is printed)
{
// skip to the next scene
}
}
But I don't know how to formulate the 2nd part of code. I've tried many different approaches, like using counters that increase on a buttonclick (and using that to check in an if-statement), and many different types of if-statements to see if the typewriter is still active or not, but I haven't got anything to work yet.
Edit
This is the sequence in which different components need to be loaded (on button click), which is related to the way different variables are updated:
Gamestate_Cycle() --> called for loading new scene.
FullTextBottom = LblTextBottom.Text --> called to refresh variables for typewriter.
TypeWriterEffectBottom() --> called to perform typewriter effect.
Avoid async void. Otherwise you can get an Exception that will break your game and you will not able to catch it.
Then use as less global variables in async methods as possible.
I suggest CancellationTokenSource as thread-safe way to stop the Type Writer.
public async Task TypeWriterEffectBottom(string text, CancellationToken token)
{
if (this.BackgroundImage != null)
{
Debug1.Text = "TypeWriter is active";
StringBuilder sb = new StringBuilder(text.Length);
try
{
foreach (char c in text)
{
LblTextBottom.Text = sb.Append(c).ToString();
await Task.Delay(30, token);
}
}
catch (OperationCanceledException)
{
LblTextBottom.Text = text;
}
Debug1.Text = "TypeWriter is finished";
}
}
Define CTS. It's thread-safe, so it's ok to have it in global scope.
private CancellationTokenSource cts = null;
Call TypeWriter from async method to be able to await it.
// set button layout as "Skip text" here
using (cts = new CancellationTokenSource())
{
await TypeWriterEffectBottom(yourString, cts.Token);
}
cts = null;
// set button layout as "Go to the next scene" here
And finally
private void PbFastForward_Click(object sender, EventArgs e)
{
if (cts != null)
{
cts?.Cancel();
}
else
{
// go to the next scene
}
}
I pondered on your task a bit more and it occurred to me that it is a good job for the Rx.Net library.
An advantage of this approach is that you have less mutable state to care about and you almost don't need to think about threads, synchronization, etc.; you manipulate higher-level building blocks instead: observables, subscriptions.
I extended the task a bit to better illustrate Rx capabilities:
there are two pieces of animated text, each one can be fast-forwarded separately;
the user can fast-forward to the final state;
the user can reset the animation state.
Here is the form code (C# 8, System.Reactive.Linq v4.4.1):
private enum DialogState
{
NpcSpeaking,
PlayerSpeaking,
EverythingShown
}
private enum EventKind
{
AnimationFinished,
Skip,
SkipToEnd
}
DialogState _state;
private readonly Subject<DialogState> _stateChanges = new Subject<DialogState>();
Dictionary<DialogState, (string, Label)> _lines;
IDisposable _eventsSubscription;
IDisposable _animationSubscription;
public Form1()
{
InitializeComponent();
_lines = new Dictionary<DialogState, (string, Label)>
{
{ DialogState.NpcSpeaking, ("NPC speaking...", lblNpc) },
{ DialogState.PlayerSpeaking, ("Player speaking...", lblCharacter) },
};
// tick = 1,2...
IObservable<long> tick = Observable
.Interval(TimeSpan.FromSeconds(0.15))
.ObserveOn(this)
.StartWith(-1)
.Select(x => x + 2);
IObservable<EventPattern<object>> fastForwardClicks = Observable.FromEventPattern(
h => btnFastForward.Click += h,
h => btnFastForward.Click -= h);
IObservable<EventPattern<object>> skipToEndClicks = Observable.FromEventPattern(
h => btnSkipToEnd.Click += h,
h => btnSkipToEnd.Click -= h);
// On each state change animationFarames starts from scratch: 1,2...
IObservable<long> animationFarames = _stateChanges
.Select(
s => Observable.If(() => _lines.ContainsKey(s), tick.TakeUntil(_stateChanges)))
.Switch();
var animationFinished = new Subject<int>();
_animationSubscription = animationFarames.Subscribe(frame =>
{
(string line, Label lbl) = _lines[_state];
if (frame > line.Length)
{
animationFinished.OnNext(default);
return;
}
lbl.Text = line.Substring(0, (int)frame);
});
IObservable<EventKind> events = Observable.Merge(
skipToEndClicks.Select(_ => EventKind.SkipToEnd),
fastForwardClicks.Select(_ => EventKind.Skip),
animationFinished.Select(_ => EventKind.AnimationFinished));
_eventsSubscription = events.Subscribe(e =>
{
DialogState prev = _state;
_state = prev switch
{
DialogState.NpcSpeaking => WhenSpeaking(e, DialogState.PlayerSpeaking),
DialogState.PlayerSpeaking => WhenSpeaking(e, DialogState.EverythingShown),
DialogState.EverythingShown => WhenEverythingShown(e)
};
_stateChanges.OnNext(_state);
});
Reset();
}
private DialogState WhenEverythingShown(EventKind _)
{
Close();
return _state;
}
private DialogState WhenSpeaking(EventKind e, DialogState next)
{
switch (e)
{
case EventKind.AnimationFinished:
case EventKind.Skip:
{
(string l, Label lbl) = _lines[_state];
lbl.Text = l;
return next;
}
case EventKind.SkipToEnd:
{
ShowFinalState();
return DialogState.EverythingShown;
}
default:
throw new NotSupportedException($"Unknown event '{e}'.");
}
}
private void ShowFinalState()
{
foreach ((string l, Label lbl) in _lines.Values)
{
lbl.Text = l;
}
}
private void Reset()
{
foreach ((_, Label lbl) in _lines.Values)
{
lbl.Text = "";
}
_state = DialogState.NpcSpeaking;
_stateChanges.OnNext(_state);
}
protected override void OnClosed(EventArgs e)
{
_eventsSubscription?.Dispose();
_animationSubscription?.Dispose();
base.OnClosed(e);
}
private void btnReset_Click(object sender, EventArgs e)
{
Reset();
}
I adjusted your code a little bit to achieve your goal. I'm not sure it's the best way to do it, but it should work.
public async void TypeWriterEffectBottom()
{
if(this.BackgroundImage == null)
{
return;
}
IsActive = true;
for(i=0; i < FullTextBottom.Length && IsActive; i++)
{
CurrentTextBottom = FullTextBottom.Substring(0, i+1);
LblTextBottom.Text = CurrentTextBottom;
await Task.Delay(30);
Debug1.Text = "IsActive = " + IsActive.ToString();
}
IsActive = false;
}
private void PbFastForward_Click(object sender, EventArgs e)
{
if(IsActive)
{
LblTextBottom.Text = FullTextBottom;
IsActive = false;
return;
}
// IsActive == false means all text is printed
// skip to the next scene
}
UPD: Just noticed that Hans Kesting has suggested pretty much exactly this in his comment.
You write what skip / forward button does, so you control it. Just have a check if the length of written text is equal to text that supposed to be written and if yes move as usual if not just display the text in full have delay to be read and move on
So my program is generating a bunch of buttons like so:
foreach (var subdir in dir.GetDirectories()) {
var path = subdir.Name;
var button = new Button {
Text = getFlavor(path) + "\t(" + path + ")",
Width = Width,
Height = 35,
Top = y
};
button.Click += buttonClick;
Controls.Add(button);
if (button.Text.Contains("Kittens")
i++;
}
I want to try something like this
if (i == 1) {
[Button.ThatContains("Kitten)].Click;
}
"ThatContains" is not a real method. How do I get references to buttons I've created programmatically ?
You could use OfType<Button> to find all buttons in the container control where you've added them(f.e. a Panel). Then a liitle bit LINQ power gives you the correct button(s):
var kittenButtons = panel.Controls.OfType<Button>()
.Where(btn => btn.Text.Contains("Kittens"));
foreach(Button btn in kittenButtons)
btn.PerformClick();
If you just want to click the first:
Button kittenButton = panel.Controls.OfType<Button>()
.FirstOrDefault(btn => btn.Text.Contains("Kittens"));
if(kittenButton != null)
kittenButton.PerformClick();
For what it's worth, here is also an extension method that returns controls recursively via deferred execution which allows to use only the first found Buttton or consume all down the road:
public static IEnumerable<T> GetChildControlsRecursive<T>(this Control root) where T : Control
{
if (root == null) throw new ArgumentNullException("root");
var stack = new Stack<Control>();
stack.Push(root);
while (stack.Count > 0)
{
Control parent = stack.Pop();
foreach (Control child in parent.Controls)
{
if (child is T)
yield return (T)child;
stack.Push(child);
}
}
yield break;
}
Now you can use similar code as above to get for example the first matching button or all:
var kittenButtons = this.GetChildControlsRecursive<Button>()
.Where(b => b.Text.Contains("Kittens"));
// search just until the first button is found
Button firstKittenButton = kittenButtons.FirstOrDefault();
if(firstKittenButton != null) firstKittenButton.PerformClick;
// loop all
foreach(Button btn in kittenButtons)
btn.PerformClick();
Either create a subclass of Button to store the information you want and instantiate that instead or use the Tag property
public class MyButton : Button
{
public int ButtonID { get; set; }
}
public class MyApplication
{
public void DoSomething()
{
int i; // todo: loop stuff
var button = new MyButton
{
Text = getFlavor(path) + "\t(" + path + ")",
Width = Width,
Height = 35,
Top = y,
ButtonID = i
};
}
}
Or why not cast the sender parameter of the button click event as a Button and check the text?
public class MyApplication
{
public void DoSomething()
{
var b = new Button();
b.Click += b_Click;
}
public void b_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
switch (b.Text) {
case "Kittens":
return;
default:
return;
}
}
}
Something like this
var button = FirstOrDefault(y => y is Button && y.Text.Contains("Kittens"));
if(button != null)
button.PerformClick();
In order to get the references, you may need to what you would do with getting references of any other type - store them somewhere, which does not seem to be the case here at all. Normally, you would register your buttons for interaction from a user by attaching them to a Form. Assuming you're not doing this by the looks of your sample code, I'm going to recommend storing them into a Dictionary<string, Button>.
You could use a dictionary or you could use a simple recursive loop (in case you are sticking the buttons into different containers).
private bool ClickButton(string buttonName, Control control) {
if (control is Button && control.Text.Contains(buttonName) {
((Button)control)PerformClick();
return true;
}
if (control.HasChildren) {
foreach (Control childControl in control.Controls) {
if (ClickButton(buttonName, childControl)) {
return true;
}
}
}
return false;
}
Usage: ClickButton("Kittens", this);
Or you could use a dictionary, as some have suggested.
private Dictionary<string, Button> DynamicButtons = new Dictionary<string, Button>();
private void ClickDictionaryButton(string buttonName) {
var matches = DynamicButtons.Where(x => x.Key.Contains(buttonName));
foreach (var match in matches) {
match.Value.PerformClick();
}
}
Usage: ClickDictionaryButton("Kittens", this);
I have been searching this issue in the web and have gone trough the documentation,however was not successful in finding a solution.
In my code I have created a MasterPane and utilize 13 GraphPanes,The problem is that if there are many graphs, the details become indistinguishable,hence I want to select(by clicking) a graph and enlarge it.Is there a specific function to realize this goal.If not,which steps are to be followed.
Thank you in advance
Even late,i hope it'll will help others.
The idea is to use the MasterPan PaneList collection.
I added a few buttons to the Window and do the control from them, another way
is to use FindPane method in the MasterPan class and do this by clicking.
I'll show both ways.
The code follows:
// graphSystem Class
MasterPane masterPane;
PaneList plist = new PaneList();
private void InitGraphs()
{
//Zedgraph control
var zgc = Apprefs.Zedgraph;
//MasterPan
masterPane = zgc.CreateMasterPan(Title, System.Drawing.Color.White);
// CreateMultiGraph is my own API to create Graph
zgc.CreateMultiGraph("Graph1", 1, "G1xtitle", "G1ytitle", false);
zgc.CreateMultiGraph("Graph2", 1, "G2xtitle", "G2ytitle", false);
zgc.CreateMultiGraph("Graph3", 1, "G3xtitle", "G3ytitle", false);
// save the Pans
foreach (GraphPane graph in masterPane.PaneList)
plist.Add(graph);
}
//---------------------------------------------------------------------------
public void Englare(RichButton button)
{
var graph = Apprefs.Zedgraph2.graph;
if (button.Name == "Show1")
{
ShowOneGraph(0);
}
else if (button.Name == "Show2")
{
ShowOneGraph(1);
}
else if (button.Name == "ShowAll")
{
ShowAllGraphs();
}
}
//---------------------------------------------------------------------------
private void ShowOneGraph(int Graphindex)
{
if (masterPane == null) return;
var graph = Apprefs.Zedgraph.graph;
if (Graphindex >= 0 && Graphindex < plist.Count)
{
masterPane.PaneList.Clear();
masterPane.PaneList.Add(plist[Graphindex]);
Layout();
}
}
//---------------------------------------------------------------------------
private void ShowAllGraphs()
{
if (masterPane == null) return;
var graph = Apprefs.Zedgraph.graph;
masterPane.PaneList.Clear();
foreach (GraphPane gr in plist)
masterPane.PaneList.Add(gr);
Layout();
}
//---------------------------------------------------------------------------
private void Layout()
{
var graph = Apprefs.Zedgraph2.graph;
using (Graphics g = graph.CreateGraphics())
{
masterPane.SetLayout(g, PaneLayout.SingleColumn);
graph.AxisChange();
graph.Refresh();
}
}
//---------------------------------------------------------------------------
way2: englare by click On Graph:
Add this method:
//---------------------------------------------------------------------------
GraphPane lastpan;
public void UCclicked(PointF mousePt)
{
GraphPane pan= masterPane.FindPane(mousePt);
if (pan != null)
{
if (pan == lastpan)
{
ShowAllGraphs();
lastpan = null;
}
else
{
ShowOneGraph(plist.IndexOf(pan));
lastpan = pan;
}
} }
Also register to the click event:
zgcGraph.MouseDoubleClick += new MouseEventHandler(zgcGraph_MouseDoubleClick);
And finally:
void zgcGrzgcGraph_MouseDoubleClick(object source, System.Windows.Forms.MouseEventArgs e)
{
if (Apprefs.graphSystem != null)
{
System.Drawing.PointF mousePt = new System.Drawing.PointF(e.X, e.Y);
Apprefs.graphSystem.UCclicked(mousePt);
}
}
thats it!
In my application I want to ensure that a TagVisualization is only displayed if the tagged object is placed on a Ellipse. So I used this code to do that:
private void TagVisualizer_VisualizationAdded(object sender, TagVisualizerEventArgs e)
{
Console.WriteLine("Hitlist");
//Notes
if (e.TagVisualization.GetType() == typeof(NoteVisualization))
{
bool found = false;
Point pt = e.TagVisualization.Center;
hitResultsList.Clear();
VisualTreeHelper.HitTest(RootLayer, null, new HitTestResultCallback(MyHitTestResult), new PointHitTestParameters(pt));
if (hitResultsList.Count > 0)
{
foreach (DependencyObject o in hitResultsList)
{
if (o.GetType() == typeof(Ellipse))
{
Console.WriteLine("Placed on a Sourcefile");
SourceFile sf = (((o as Ellipse).Tag) as SourceFile);
GroupBox gp = e.TagVisualization.FindName("GroupHeader") as GroupBox;
gp.Header = sf.getFullName();
e.TagVisualization.Tag = sf;
SurfaceButton save = e.TagVisualization.FindName("NoteSave") as SurfaceButton;
save.Tag = sf;
found = true;
break;
}
}
}
if (!found)
{
e.TagVisualization.Visibility = System.Windows.Visibility.Collapsed;
Console.WriteLine("Placed somewhere else");
}
}
}
I'm not really sure if this is the correct way, since I don't avoid that the TagVisualization is displayed, but instead I instantly set the Visibility to collpased. I think there have to be better ways to do that?
official guidance for how to do this is shown in one of the sdk samples:
http://msdn.microsoft.com/en-us/library/ee804861(v=Surface.10).aspx
-robert (former program manager for the surface dev platform)
Or maybe google is just not so friendly to me?
What I want is this simple thing:
constructor that accepts an array of menu item objects
Value get/set property that would set all the Checked properties right
bind to all Clicked events of the supplied items and provide One event
Working DataBind facilities
If you encountered such a nice thing around, please direct me. No need for manual do-it-in-your-form1.cs-class links, please. This I can do myself.
See: http://msdn.microsoft.com/en-us/library/ms404318.aspx
Summary: You'll have to make a new ToolStripMenuItem subclass that overrides the OnCheckChanged, OnOwnerChanged, and possibly OnPaint methods.
Note that in our case, we keep the check mark for the UI rather than a radio button. But keep the exclusive tick functionality.
OKay, here's my final code. It does something the other one doesn't (Supports binding), and vice versa. Perhaps one could combine. Use at your pleasure.
// Usage example:
//
// ric = new RadioItemCoupler(new ToolStripMenuItem[] {
// neverToolStripMenuItem,
// alwaysToolStripMenuItem,
// errorsOnlyToolStripMenuItem
// });
// this.Controls.Add(ric);
// _ric.DataBindings.Add("CheckedIndex", MySettings, "SmsReplyType",
// false, DataSourceUpdateMode.OnPropertyChanged);
public class RadioItemCoupler : Control
{
private int _checkedIndex;
// Zero-based
[Bindable(true)]
public int CheckedIndex
{
get { return _checkedIndex; }
set
{
_checkedIndex = value;
_items[value].Checked = true;
}
}
public event EventHandler CheckedIndexChanged;
ToolStripMenuItem[] _items;
private delegate void ItemHandler(ToolStripMenuItem item);
public RadioItemCoupler(ToolStripMenuItem[] items)
{
_items = items;
foreach (ToolStripMenuItem tsmi in _items)
{
tsmi.CheckOnClick = true;
tsmi.CheckedChanged += new EventHandler(tsmi_CheckedChanged);
}
}
void tsmi_CheckedChanged(object sender, EventArgs e)
{
ToolStripMenuItem that = sender as ToolStripMenuItem;
// Restore check if checked out
bool nothingChecked = true;
foreach(var item in _items)
nothingChecked = nothingChecked && !item.Checked;
if (nothingChecked)
{
_items[_checkedIndex].Checked = true;
return;
}
if (!that.Checked)
return;
for (int i = 0; i < _items.Length; i++)
{
if (that != _items[i])
{
if (_items[i].Checked)
_items[i].Checked = false;
}
else
{
_checkedIndex = i;
if (CheckedIndexChanged != null)
CheckedIndexChanged(this, EventArgs.Empty);
}
}
}
}