|
|
To add to what Richard has said, there are several CSV reading packages - this one reads into a DataTable, so provided the columns match up with your database, they can be INSERTed with no other changes: A Fast CSV Reader[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
The article I linked to also reads into a DataTable.
|
|
|
|
|
I have the following treeview class whose purpose is to draw an overlay on the icons in a node. Notice it overrides "OnDrawNode":
public delegate Image GetTreeIconOverlayMethod(TreeNode node);
public class OverlayTreeView : TreeView
{
bool drawOverlay;
int iconHeight;
int iconWidth;
public GetTreeIconOverlayMethod GetIconOverlay { get; set; }
public OverlayTreeView() { }
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);
if (ImageList != null)
{
drawOverlay = true;
iconHeight = ImageList.ImageSize.Height;
iconWidth = ImageList.ImageSize.Width;
}
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
base.OnDrawNode(e);
Image overlay = null;
if (drawOverlay)
if (GetIconOverlay != null)
if (!((e.Node.ImageIndex == -1) && (e.Node.ImageKey == null)))
overlay = GetIconOverlay(e.Node);
if (overlay != null)
{
int x = e.Node.Bounds.X + iconWidth - overlay.Width;
int y = e.Node.Bounds.Y + iconHeight - overlay.Height;
e.Graphics.DrawImage(overlay, x, y);
}
}
}
The overlay is not being drawn and it seems this is because the override is not being called at all. Any breakpoint put in this method does NOT gte triggered even though a breakpoint in OnHandleCreated is. It doesn't matter where I put the breakpoint within the method. (And yes... symbols are loaded).
I have no idea why this would occur...
|
|
|
|
|
The OnDrawNode method is used by the base class to raise the DrawNode event.
According to the documentation[^], that event is only raised if "the DrawMode property is set to a TreeViewDrawMode value other than Normal ."
It's therefore safe to assume that the OnDrawNode method won't be called if DrawMode is set to Normal .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So what method would I override to add the overlay no matter what draw mode is being used? I can't find one, except perhaps OnPaint... but I would think Microsoft would have included a default drawnode method to use regardless of the draw method. Since the point of my class is to add the overlay, I wouldn't want a potential user of the class to have to specify a draw mode. (I know there are workarounds... but to me forcing us to use them doesn't make a whole lot of sense to me, especially considering the complexity of drawing a tree node).
|
|
|
|
|
There isn't one. The method is called in response to an unmanaged WM_ message, which will only be sent to the control if it's set to be owner-drawn.
Windows Forms controls are thin wrappers around native Windows controls. They can't add functionality that the native controls don't support.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Richard Deeming wrote: There isn't one. See my answer below for a solution.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
modified 5-Nov-21 6:03am.
|
|
|
|
|
Works for me:
Override 'OnCreateControl and set the 'TreeViewDrawMode Property ... with either 'DrawMode == 'OwnerDrawText or 'OwnerDrawAll: 'OnDrawNode will be called.
protected override void OnCreateControl()
{{
if (DrawMode != TreeViewDrawMode.OwnerDrawText)
{
DrawMode = TreeViewDrawMode.OwnerDrawText;
}
base.OnCreateControl();
}
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Hi,
I need to do a "cron mechanism" that does not block the entire program. It has to be performed at the time set by the user
int time_stop;
Task.Delay(time_stop).Wait(); .
time_stop is provided by the user. But this approach is not entirely consistent with my assumptions. Can someone guide me.
Sorry, but I'm just learning to code
|
|
|
|
|
You either need to set up a Timer Class (System.Timers) | Microsoft Docs[^] or a BackgroundWorker Class (System.ComponentModel) | Microsoft Docs[^]
Either will do, but without one or the other, you will block the UI thread waiting for the timeout to elapse - which will make it look like the whole application is locked up!
Me? I'd use a background worker and use the Progress reporting mechanism to update the UI thread when necessary:
private void FrmMain_Shown(object sender, EventArgs epp)
{
BackgroundWorker work = new BackgroundWorker();
work.DoWork += Work_DoWork;
work.ProgressChanged += Work_ProgressChanged;
work.RunWorkerCompleted += Work_RunWorkerCompleted;
work.WorkerReportsProgress = true;
work.RunWorkerAsync("A parameter of any type you want to pass it");
}
private void Work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MyTextBox.Text = "Done.";
}
private void Work_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MyTextBox.Text = ($"Progress: {e.ProgressPercentage} - {e.UserState as string}");
}
private void Work_DoWork(object sender, DoWorkEventArgs e)
{
if (sender is BackgroundWorker work)
{
for (int i = 0; i < 1000000; i++)
{
Thread.Sleep(1000);
work.ReportProgress(i / 10000, $"An object of any type you want to pass back to the main thread: {i}");
}
}
}
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If I use a loop
while (1)
{
Thread.Sleep (1000);
work.ReportProgress (i / 10000, $ "An object of any type you want to pass back to the main thread: {i}");
}
will I suspend the program?
The program is to be executed e.g. every day at 7:00 am
|
|
|
|
|
Not if it's running on a different thread, that's the whole idea.
You app starts with a single thread - the UI thread - which is responsible for updating the display and handling all user inputs: keyboard, mouse, touch screen - all of it.
The whole idea of a BackgroundWorker class is that it starts a new thread on which the long running task executes, leaving the UI thread free to handle user interactions in a timely manner. The Progress reporting mechanism moves the "update" data back to the UI thread so it can be displayed.
Try it: you'll start to see how it works!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
To be honest, if you want to execute a program at the same time every day, I wouldn't code for it at all: I'd add it to the Windows task scheduler to be run at that time, or (if it doesn;t need any user interaction) make it a service so the user doesn't have to be logged in.
What are you trying to automate?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Adding posts to wordpress is to be a desktop program not from the page dashboard
|
|
|
|
|
Then why run it at a time when it's likely to be unattended?
I'd break it in two parts: a UI app which "collates" the data, and a service which updates at a specific time.
That way, your users don't have to remember not to close the app, or to open it before they knock off for the day and the service just sits in the background sleeping most of the time.
Use a DB for communication between them for simplicity and it's a relatively painless task?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sorry but i don't know what is DB
EDIT
database, yeah?
modified 4-Nov-21 7:15am.
|
|
|
|
|
Yep!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I will have to use the Windows service. This will be the best solution.
|
|
|
|
|
For how to enable .NET 5 in VS 2019: [^]. Also, see the weird hack at the end of this post.
Note: the two 'records defined here use positional arguments rather than Property get/set for parameters. that is supposed to confer immutability, and non-destructive mutation (whatever that is). The code below runs without errors.
public record Cat(string Name, int HowHungry);
public record AlleyCat(string Name, int HowHungry, string Color) : Cat(Name, HowHungry);
public void TaleOfTwoCats()
{
Cat cat1 = new Cat("Bonita gatita", 2);
AlleyCat alleycat1 = new AlleyCat("Anhela cabezas de pescado", 99, "stale orangina");
bool iscat1 = cat1 is Cat;
bool isalleycat1 = cat1 is AlleyCat;
bool iscat2 = alleycat1 is AlleyCat;
bool iscat3 = alleycat1 is Cat;
bool issamecat1 = cat1 == alleycat1;
bool issamecat2 = cat1.Equals(alleycat1);
cat1 = alleycat1;
bool iscat4 = cat1 is Cat;
bool isalleycat2 = cat1 is AlleyCat;
bool iscat5 = alleycat1 is AlleyCat;
bool iscat6 = alleycat1 is Cat;
bool issamecat3 = cat1 == alleycat1;
bool issamecat4 = cat1.Equals(alleycat1);
} Note: to make this work in a WinForm 4.8 program, i had to include this:
namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
} Weird: there is no reference in the Project to CompilerServices, or top=level using statement invoking it.
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
State1 & state2 will run parallel or State1 will run first and later State2 will run ?
please guide me. thanks
var stage1 = Task.Run(() =>
{
});
var stage2 = Task.Run(() =>
{
});
Task.WaitAll(stage1, stage2);
|
|
|
|
|
Mou_kol wrote: State1 & state2 will run parallel or State1 will run first and later State2 will run ?
Yes.
Or perhaps 2 will run first and 1 later.
There is no way to control what happens with tasks of equal priority: they exist on separate threads, and will be executed when the OS decides it is appropriate , and as a core becomes available. That means that the execution order of the tasks is indeterminate: it cannot be predicted or relied upon.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Task.Run will execute the contained code on the ThreadPool. The TreadPool will allow a number of tasks to run in parallel. So as long as you have not reached this limit and there is a CPU core available, they will execute in parallel.
If you have reached the limit, they might run one by one - and they might delay execution until a thread becomes available. This is typically not something you need to think about, but worth knowing if you have long running operations or if you are queueing a lot of operations on the thread pool - for example looping over a collection and running something for each item. If you need to do the latter, look into "Task Parallel Library"[^]
|
|
|
|