|
wrote: I think it might just be easier to place all my data input boxes in the same form.
Easier, yes. Correct, no.
wrote: seems like going in a large circle for the same end result WinForms have an expected behaviour with the end-user. Yes, it is easier to put all in one form; it is even easier to put all the input in Console.ReadLine-form and to not use Forms anyway.
Read the materials provided, it is not that complicated
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Following that logic, you will also have to add all the controls for the Save dialog, the Open dialog, maybe the Font Select dialog to each of your forms, which might make it rather hard to use!
It's not complicated to do it properly - it's less work that fitting all those controls nicely into your main form - and it means that your new form can be reused.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Assuming that Form2 is created from within Form1, then Form2 could provide a method that Form1 calls to get the data, or Form1 passes the reference to the writer. Without more information it is difficult to guess what is best.
|
|
|
|
|
|
As you can see from the responses here, there are many ways you could implement this; without understanding your UI goals in more detail, I think we're limited in terms of how helpful we can be.
Issues to consider in using multiple Forms:
1. shown modal ? allow user to cancel while incomplete ?
2. data in each field cleared with use, or persistent ?
3. validation ? allow empty fields ? required fields ?
My preference is to offload all serialization, or printing. functions, to a single static Class which might look something like this:
using System;
using System.IO;
using System.Text;
namespace YourNameSpace
{
public static class WriteManager
{
public static string FileName = "Test";
public static string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
public static string ExtensionType = ".txt";
private static string CurrentPath;
static WriteManager()
{
SetWritePath(FileName, FilePath, ExtensionType);
}
public static void SetWritePath(string name, string path, string extension)
{
FileName = name;
FilePath = path;
ExtensionType = extension;
CurrentPath = Path.Combine(path, name + extension);
}
static StringBuilder stringBldr = new StringBuilder();
public static void WriteFile(StringBuilder sb1, StringBuilder sb2)
{
stringBldr.Clear();
stringBldr.Append(sb1.Append(sb2));
File.WriteAllText(CurrentPath, stringBldr.ToString());
}
public static void ReadFile()
{
}
}
} In this model, the 'WriteFile method requires two 'StringBuilder instances as inputs. I'd implement a 'GetData method in each of the Forms that has the responsibility to read the fields and turn them into a ready to save 'StringBuilder. The call in the main Form that triggers saving might look like:
StringBuilder sb1 = new StringBuilder();
public StringBuilder GetData()
{
sb1.Clear();
sb1.AppendLine(textBox1.Text);
sb1.AppendLine(textBox2.Text);
sb1.AppendLine(textBox3.Text);
sb1.AppendLine(textBox4.Text);
return sb1;
}
private void SaveFileButton_Click(object sender, EventArgs e)
{
WriteManager.WriteFile(this.GetData(), form2.GetData());
} Keep in mind this is a sketch, but, the architecture is one that has proved useful foe me.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
hi
What is the best method to store files in the database?
Storing large files in the database takes a few seconds
now for this witch method is best to control on insert method
When the size of a file is greater, it should monitor the storage process
Which one is best for you?
backgroundworker
Async
Threading
or ?
string filePath = "";
string connectionString = "";
FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(stream);
byte[] file = reader.ReadBytes((int)stream.Length);
reader.Close();
stream.Close();
SqlCommand command;
SqlConnection connection = new SqlConnection(connectionString);
command = new SqlCommand("INSERT INTO FileTable (File) Values(@File)", connection);
command.Parameters.Add("@File", SqlDbType.Binary, file.Length).Value = file;
connection.Open();
command.ExecuteNonQuery();
|
|
|
|
|
Large files probably shouldn't be stored in a DB at all: instead, store them on an accessible file server, and store the path to the file in the DB.
The way I do it is to use a GUID file name for the actual file, store that path to that in eth DB, and store the "real" file name with that. That way, multiple users can have ABC.DOCX without them conflicting.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Sometimes I just store the "name and location" in the database; the files get added to a separate folder on the server (e.g. receipt images); available at any time regardless of database status.
Meaningful file names are important (e.g. time stamped; invoice #; etc.).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
For Sql Server (which I assume you're using since the code references it) look in the docs;
UPDATE (Transact-SQL) | Microsoft Docs[^], under the heading "Updating Large Value Data Types".
--edit
An example of the usage can be found here[^]. Combine those and you can show a progressbar while reading/writing.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
modified 3-Nov-18 9:24am.
|
|
|
|
|
I have a list of System.Drawing.Point , e.g. selected pixels in a bitmap. Now I'd like to get a list of points which are nearby, say less than 10 pixels (simply the sum of distances in x and y direction) away from the former points.
When the number of points is small, that's easy. But when more than 1000 pixels are selected, the performance becomes important. Can you suggest some algorithms for this task?
Currently, I use a simple method:
public static IReadOnlyList<Point> GetSurroundingCloud(this IEnumerable<Point> _points, int _maximumDistance)
{
ISet<Point> result = new HashSet<Point>();
foreach (Point p in _points)
{
for (int x = 0; x <= _maximumDistance; x++)
{
for (int y = 0; y + x <= _maximumDistance; y++)
{
result.Add(new Point(p.X - x, p.Y - y));
result.Add(new Point(p.X - x, p.Y + y));
result.Add(new Point(p.X + x, p.Y - y));
result.Add(new Point(p.X + x, p.Y + y));
}
}
}
return result.ToList();
} As you can see, there are some features which won't scale well:
- the iteration over the input list (though O(n) could be acceptable)
- the loops with maximumDistance parameter - O(n^2) with that parameter
- HashSet.Add - the larger the list gets, the more points it has to look up for preventing duplicates, so something like O(n^2) or worse.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
modified 5-Nov-18 3:50am.
|
|
|
|
|
Trigonometry to the rescue! You can use Pythagorean theorem to calculate the hypotenuse which is basically the length from one point to another.
Hypotenuse - Wikipedia[^]
The trick to make it efficient is to avoid using the Square Root function.
Basically...
10 > sqrt(x^2 + y^2)
is a lot slower to calculate than
10^2 > x^2 + y^2
As some bonus trivia I found the Fast inverse square root - Wikipedia[^] really fascinating:
|
|
|
|
|
Thanks, Dar, but that's not the thing I am looking for. For the purpose of getting that list of points surrounding a given list of points, the sum of x and y will just do for the distance.
It is the generation of the list which I think of.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
|
The problem with using 'structs in this case would be their mutability will require you to update your list with the new copy of the struct created by modifying it.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Analytical Geometry to the rescue!
Given point (X0, Y0), return all points (X, Y) such that |X - X0| + |Y - Y0| <= R.
For the row Y == Y0, this is trivially all the points (X, Y0), X0 - R <= X <= X0 + R.
For the rows Y == Y0+/-1, this is trivially all the points (X, Y0+/-1), X0 - R + 1 <= X <= X0 + R - 1.
For the rows Y == Y0+/-n, this is trivially all the points (X, Y0+/-n), X0 - R + n <= X <= X0 + R - n.
And for Y == Y0+/-R, this is trivially the points (X0, Y0+/-R).
No coordinates are explicitly calculated, and no point is visited more than once.
Note that this approach would work for the more conventional distance, as well. Use Bresenham's circle-drawing algorithm to calculate the end-points of each row.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
@bhiller Without some 'unsafe voodoo, I don't see any way but "brute force;" in this example, I chose to generate a set of rectangles:
public static IEnumerable<Rectangle> GetNearbyPoints(int maxDist, List<Point> points, int nRows, int nColumns)
{
int x, y, w, h;
int half = maxDist / 2;
List<Rectangle> rects = new List<Rectangle>();
for (int i = 0; i < points.Count(); i++)
{
Point pt = points[i];
x = pt.X;
y = pt.Y;
if (x < 0) x = 0;
w = maxDist;
if (x + half > nColumns)
{
w = nColumns - x;
}
if (y < 0) y = 0;
h = maxDist;
if (y + half > nRows)
{
h = nRows - y;
}
yield return (new Rectangle(x,y,w,h));
}
}
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Thanks, Bill. That could pave the way to a solution.
I really need some kind of "List" of those points - IEnumerable is necessary, Count would be nice, mutability is not required at all. That "list" will be used as a parameter in further calculations.
So, a solution could be:
- create a rectangle enclosing all original points (that's O(n) for number of points)
- create a 2-dimensional array of bool, and map those points (i.e. x and y of the point become indices of the array, and there the value is set to true)
- create a 2-dimensional array of bool for the result. It's bigger, and the size can easily be determined.
- either
- - iterate over all result array and find out if a point of the original array can be reached
- - iterate over the original array and mark all points which can be reached in the result array. In contrast to the HashSet implementation, there is no need to check for duplicates: it does not matter if an item already true is set to true again.
- re-map the array to a List of Point
That should be O(n) for the number of original points (which is my main concern), but stil O(n^2) for the distance (which is a configurable value, and should not vary to much).
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
off the top of my head: from the IEnumerable<Rectangle>
public static IEnumerable<Point> RectanglesToPoints(IEnumerable<Rectangle> rects)
{
return rects.Select(r => RectToPoints(r)).SelectMany(pt => pt);
}
public static IEnumerable<Point> RectToPoints(Rectangle rect)
{
for (int i = rect.X; i < rect.Width + rect.X; i++)
{
for (int j = rect.Y; j < rect.Height + rect.Y; j++)
{
yield return new Point(i,j);
}
}
} I suspect there's a better way; is there something about when you need to go from IEnumerable to List, or other, that is critical here ?
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
Thanks for your input. I think I could have enough information now to improve the original solution (don't need to do it now, but I am sure I'll have to do it some when later).
Regarding your latest code, there a two points to consider: the resulting list will be used in a few places. So I'd have to do a ToList or ToArray call to prevent the Linq statement from being executed multiple times. That's an easy fix.
Also, when I need the amount of points in the resulting list, it should be free of duplicates. A call to Distinct in RectanglesToPoints should do the trick easily again.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
Hi Bernhard, It was fun to think about this.
I was puzzling over the issue of duplication, since two rectangles could overlap, without being identical. My only thought was to generate the full list of points, and call 'Distinct on that: I tried to ignore the thought of graphic paths that could represent rectangles with chunks taken out of them
cheers, Bill
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot
|
|
|
|
|
BillWoodruff wrote: It was fun to think about this. That's also my motivation for reading questions / articles / discussions on CodeProject. Nice to hear that I could provide an interesting topic.
Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!
|
|
|
|
|
I need help with a program for a match stick game between the computer and a user. program should ensure that the computer always wins.
Rules for the game are as follows :
•There are 21 match –stick
•The computer asks the player to pick 1,2,3 sticks.
•The computer starts first.
•After the person pick, the computer does is picking.
•Whoever is forced to pick up the last stick win the game.
|
|
|
|
|
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.
Try it yourself, you may find it is not as difficult as you think!
If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Computers take instructions very literal.
Computer picks 1 stick. 20 left.
User picks 1 stick. 19 left.
Computer picks 2 sticks. 17 left.
User picks 2 sticks. 15 left.
Computer picks 3 sticks. 12 left.
User picks 3 sticks. 9 left.
Everyone wins.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I win!");
Console.ReadLine();
}
}
|
|
|
|
|