|
I am able to load a block on to a form when I click a button (the button is on the form) but I want to move my code so that when the form loads the block on the form will also load. Where would I put the code?
///
Graphics gs = this.CreateGraphics();
Pen p = new Pen(new SolidBrush(Color.Red));
gs.DrawRectangle(p, Goal);
|
|
|
|
|
Brian_TheLion wrote: I want to move my code so that when the form loads the block on the form will also load. Unclear what you are doing here:
1. you can add, or remove, the Paint EventHandler for any Form in code ... to draw on the Form.
2. the standard Load EventHandler you define is called automatically when the Form is shown.
If you create new Forms at run-time, you can install a Paint EventHandler once you have a valid reference to the new Form.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Don't just draw the block like that - partly because it'll disappear when you minimize the form or drag another form over the top, and partly because you need to Dispose of all Graphics contexts you create as soon as you are finished with them. The best way of doing that is via a using block:
using (Graphics gs = this.CreateGraphics())
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(b))
{
gs.DrawRectangle(p, Goal);
...
}
}
...
} Note that that also applies to Pens, Brushes, and other Graphics elements.
If you want to draw on your form, then handle the Form.Paint event and use the graphics context provided:
private void FrmMain_Paint(object sender, PaintEventArgs e)
{
using (Brush b = new SolidBrush(Color.Red))
{
using (Pen p = new Pen(b))
{
e.Graphics.DrawRectangle(p, Goal);
}
}
}
"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!
|
|
|
|
|
Thanks Griff.
I'll try what you suggested.
|
|
|
|
|
Use the Paint handler, it is the only place where drawing ever should occur.
Avoid calling CreateGraphics , it is an expensive operation. The Paint handler gives its Graphics for free, see the PaintEventArgs that comes with it.
For all those small but disposable objects (Pens, Brushes, Fonts, ...) consider NOT creating them all the time by either:
- using the pre-existing ones, e.g. Pens.Red , Brushes.Red , etc.
(and since you didn't create those, you are not supposed to dispose of them either);
- creating your own, possibly more specialized ones (e.g. a thicker Pen) but then create them only once and keep them alive in class variables; when doing so, there is no need to dispose of them when your program exits.
Luc Pattyn [My Articles]
If you can't find it on YouTube try TikTok...
|
|
|
|
|
|
Can you give me an example please
|
|
|
|
|
When using NET.Framework I can't find anyway of dragging objects to a form (eg textbox, button).
When using NET.Core I can't find anyway of displaying the form so I can place objects on the form. All I get is the code for the form.
Has there been some major changes to version 8.0 of C# which prevents me from doing certain things, such as dragging objects such as a button on to a form?
|
|
|
|
|
You need to provide more information. When are you doing this, where are the objects being dragged from etc.? What part of the C# language do you believe is affecting this?
|
|
|
|
|
On older version of c#. I could display the form on the screen and on the left side of the screen I had a list of tools such as button, textbox. I could then select an object from the list then drag it on the form and position it on the form where I wanted the object to appear (when I say object I mean things like buttons and textboxs).
In C# 8.0 I don't seem to be able to do this unless you know of a way or something in the setup I need to change first.
|
|
|
|
|
It is impossible to guess what you are doing. Please edit your original question above and add full details of the problem, including the failing code.
|
|
|
|
|
If you're talking about designing the from in Visual Studio, you can get the ToolBox to show up from the View menu. Just click the View menu, then Toolbox.
Or you could hit Ctrl-W, X.
|
|
|
|
|
Hi Dave.
I tried clicking on Toolbox in the View menu but when I do that the only thing I see on the screen is a list of tools. I need to have both the form and the list of tools displayed on the screen at the same time.
In past versions of C# things were put in panels on the screen so the form display was put in one panel in the middle of the screen and the list of tools was put in the left side panel.
Can panels be setup with C# 8.0 like they have been in the past for the user interface?
|
|
|
|
|
Then you double-click the form you want to open.
The Toolbox doesn't have a maximize button so it cannot be taking up the entire window. There is a small pushpin icon next to the Close (X) button in the ToolBox window. That can pin the toolbox window or allow it to autohide into a tab on the left edge of the Visual Studio window.
|
|
|
|
|
Thanks Dave.
I found I had to dock some things such as toolbox so the interface becomes panels and is the way I want things to appear.
Brian
|
|
|
|
|
I think, and I may be wrong but is winforms supported in NET.Core? I believe the visual designer may not be included yet.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
In that case I'll need to use only Net.Framework
|
|
|
|
|
If you're running the current release of Visual Studio 2019, you'll need to enable the .NET Core Windows Forms designer in the "preview features" settings.
It's still not complete, and there's a newer version available with the preview version of Visual Studio.
Windows Forms Designer for .NET Core Released | .NET Blog[^]
The .NET Framework version of the designer and toolbox should not have changed.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks Richard.
I updated Microsoft Visual Studio today and now have a form displayed when using Net.Core
Brian
|
|
|
|
|
Hi all!
Got a new question.
I have a barcode scanner that saves all scanned barcodes in the following format:
Date,Time,Barcode
Example:
2020-07-08,13:54:16,1578456329781
2020-07-08,14:07:13,1578453697235
2020-07-08,14:08:03,1548795358155
2020-07-08,14:11:09,1578453697235
2020-07-08,14:11:42,1578453697235
Now I add this to a ListView in Gridview mode with the following code:
List<List> items = new List<List>();
string[] lines = File.ReadAllLines(@"F:\BARCODE.txt");
foreach (string line in lines)
{
string Pattern = @"(?<Date>\d{4}-\d{2}-\d{2})(?:,)(?<Time>\d{2}:\d{2}:\d{2})(?:,)(?<EAN>\d*)";
Regex R = new Regex(Pattern);
Match m = R.Match(line);
items.Add(new List() { Date = m.Groups["Date"].ToString(),
Time = m.Groups["Time"].ToString(), EAN = m.Groups["EAN"].ToString() });
}
LvItems.ItemsSource = items;
Now I want to be able to count if there are any duplicates, and show the results in a new ListView like this:
(| shows new Column in the gridview)
1578456329781|1
1578453697235|3
1548795358155|1
Does anyone have a better idea than using a atleast two loops to do this?
I was thinking on using one loop to go threw the list, and then use the second loop to check for duplicates and count.
Wondering if there are a simplier solution for this.
Any help, as always, are much appreciated.
Have a nice day!
Have gotten a new IDE
modified 25-Aug-20 10:14am.
|
|
|
|
|
For starters, I wouldn't use a Regex: use string.Split instead:
string[] lines = File.ReadAllLines(filePath);
var barcodes = lines.Select(line => {string[] parts = line.Split(',');
return parts[2]; }); You can then use GroupBy to combine them and get the Key - the barcode number - and it's Count:
var x = barcodes.GroupBy(b => b);
"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!
|
|
|
|
|
Hi!
Sorry for late reply, been a bit busy.
Anyhow, I have tried your example, but how can I output it to a Listview?
I have the following code now:
string[] lines = File.ReadAllLines(@"C:\Users\<user>\Desktop\Test data.txt");
var barcodes = lines.Select(line => {string[] parts = line.Split(',');
return parts[2]; });
var x = barcodes.GroupBy(b => b);
Tried differnt mthoods, among other: Messagebox.Show(x.ToString());
That failed :P
Thankfull for any help.
Have a nice day!
I have a new IDE!
|
|
|
|
|
What does GroupBy return?
What type is x?
Why would you assume that ToString would do anything useful?
"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 have never used LinQ, and as such I am a bit unsure what GroupBy returns and what type x is.
And about the ToString, I was just testing, all (or many) does...
Here is the whole code I have, if anyone is interested:
using System;
using System.Collections.Generic;
using System.Windows;
using System.IO;
using System.Linq;
namespace Inventory
{
public partial class Window1 : Window
{
void button1_Click(object sender, RoutedEventArgs e)
{
string[] lines = File.ReadAllLines(@"C:\Users\sdadmin\Desktop\Test data.txt");
var barcodes = lines.Select(line => {string[] parts = line.Split(',');
return parts[2]; });
var x = barcodes.GroupBy(b => b);
}
public class List
{
public string Date{get; set;}
public string Time{get; set;}
public string EAN {get; set;}
}
}
}
I am pretty new to C#, I have only been at it for about 1 month or so, if I disregard time when I don't do any programming...
Have a nice day!
|
|
|
|
|
If you don't know what type something is, you can hover the mouse over it, and VS will tell you.
In this case it's a IEnumerable<IGrouping<string, string>>
Which means that it's a collection of Groups of string key / string value pairs.
Each Group in the collection has two properties you are interested in: the Key (which is the barcode number) and the Count method which tells you how many items are in each group.
So it's pretty simple to access the barcode and count:
string[] lines = File.ReadAllLines(filePath);
var barcodes = lines.Select(line => {
string[] parts = line.Split(',');
return parts[2];
});
var x = barcodes.GroupBy(b => b);
foreach (var y in x)
{
Console.WriteLine($"{y.Key} : {y.Count()}");
}
For your sample data you get:
1578456329781 : 1
1578453697235 : 3
1548795358155 : 1
"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!
|
|
|
|