|
Maybe you misunderstood my question.
I'm trying to find the alphabetical index position of where to insert an item.
So if I have
"An item"
"My Item"
"Some stuff"
and I want to insert "New Item", then it should have index position of 2 and be inserted between M and S.
[Update]
I could simply add it to the list, then do
DatabaseItems = new ObservableCollection(myTempList.OrderBy(x => x.DatabaseTypeName))
but this will recreate the list every time. It's cheaper to insert the new item into the list at the position it belongs at. I'm just not sure how to find that position
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
There's no such thing.
You would have to write the code to determine the insert location. The quick'n'dirty method would be to just for(index...) over the collection until you find the alphabetical position and then do an Insert(index, object) to insert into the collection. Keep in mind, this doesn't scale well. The larger the collection, the more expensive this operation becomes.
Really, if an alphabetical collection had to be maintained, I would probably write my own version of ObservableCollection and add the support to it along with b-tree support to keep the CPU workload down when inserting items.
But, I really wouldn't do that because a "sorted view" is the realm of the view of the collection, not of the collection itself.
|
|
|
|
|
I guess a SortedSet<t> would work.
I'll give it a try
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Dave, your "A guide to posting questions on CodeProject" link no longer works:
This www.codeproject.com page can’t be found
No webpage was found for the web address: http://www.codeproject.com/scrapbook/ForumGuidelines.asp
Go to http://www.codeproject.com/
Search Google for codeproject scrapbook Forum Guidelines
HTTP ERROR 404 The "Asking questions" one is fine, though.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Damn it Chris, stop moving my cheese!
|
|
|
|
|
He's just trying to keep you on your toes!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Your current list l :
{"A","C","D"}
your instruction:
l .Add("B");
your new list l :
{"A","B","C","D"}
I believe that this is what's your mind trying to achieve.
In fact, I can only think of 2 appropriate ways to fulfil such a task.
1. SORT IT:
Yes, just add your new item normally. Once you do, sort your collection.
Tip: do not call the sorting instruction each time you update the ObservaleCollection, do it once you add all your items.
c# - How to sort ObservableCollection alphabetically? - Stack Overflow[^]
2. PLAY WITH IT:
Write a for loop that runs through the contents of your ObservableCollection. Within each step, a comparison will be done. You will compare your new item's name with each item, once you find a match, push all your items starting from that position to the right side of your collection.
Sample:
Here is your list:
• A
• C
you add a new item: l.Add("B"):
• A
•
• C
the C got pushed down because C's position is greater than B's.
C's old position will be reserved, B will be added to it.
• A
• B
• C
|
|
|
|
|
Use design patterns in the game and ensure a rollback
Send a console implementation of the game patches. Ensure that the size of the field is selected and the level of difficulty (how much the board differs from the state of the "victory"), make it possible for the game to become more complicated when a certain amount of random moves takes place instead of the user's turn. Realize the possibility of canceling moves.
The patterns are expected: Singleton, Factory method, Team, Memento.
<pre>
using System;
namespace 15puzzle
{
class Points
{
public readonly int x, y;
public Points(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Game
{
public int Length=0;
const int nw = 4, height = 4;
int[,] field = new int[nw, height];
Points[] FieldValue = new Points[16];
public Game(int[] point)
{
int r = 0;
string[] file = new string[4];
Length = 16;
mixer(point);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < nw; i++)
{
field[j, i] = point[r];
FieldValue[point[r]] = new Points(j, i);
r++;
}
}
}
public void mixer(int[] p)
{
int tmp = 0;
Random rnd = new Random();
for (int i = 0; i < 16; i++)
{
bool isExist = false;
do
{
isExist = false;
tmp = rnd.Next(0, 16);
for (int j = 0; j < i; j++)
{
if (tmp == p[j]) { isExist = true; }
}
}
while (isExist);
p[i] = tmp;
}
}
private Points GetLocation(int value)
{
return FieldValue[value];
}
public void drawField()
{
Console.WriteLine("----------------------------");
for (int i = 0; i < nw; i++)
{
for (int j = 0; j < height; j++)
{
Console.Write(field[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("----------------------------");
}
public bool repeat(double Length,int[] point)
{
for (int i = 0; i < Length; ++i)
{
for (int y = i + 1; y < Length; ++y)
{
if (point[i] == point[y])
{
Console.WriteLine(point[i] + " ==" + point[y]);
throw new ArgumentException("Numbers should not be repeated");
}
}
}
return true;
}
public Boolean finish()
{
bool temp = false;
int value = 1;
for (int i = 0; i < nw; ++i)
{
for (int j = 0; j < height; ++j)
{
if (field[i, j] == value)
{
temp = true;
++value;
if (value == Length)
{
value = 0;
}
}
else
{
return false;
}
}
}
return temp;
}
public void Move(int value)
{
try
{
Console.WriteLine(value);
if (value > 15 || value < 0)
{
throw new ArgumentException();
}
int x = GetLocation(0).x;
int y = GetLocation(0).y;
int ValueX = GetLocation(value).x;
int ValueY = GetLocation(value).y;
if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
{
field[x, y] = value;
field[ValueX, ValueY] = 0;
var vere = FieldValue[0];
FieldValue[0] = FieldValue[value];
FieldValue[value] = vere;
}
}
catch (ArgumentException)
{
Console.WriteLine("There is no such number, try again: ");
}
catch (Exception)
{
Console.WriteLine("Next to this number is not 0, try again: ");
}
}
}
class Points2
{
public readonly int x, y;
public Points2(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Game2
{
public int Length = 0;
const int nw = 3, height = 3;
int[,] field = new int[nw, height];
Points[] FieldValue = new Points[9];
public Game2(int[] point)
{
int r = 0;
string[] file = new string[3];
Length = 9;
mixer(point);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < nw; i++)
{
field[j, i] = point[r];
FieldValue[point[r]] = new Points(j, i);
r++;
}
}
}
public void mixer(int[] p)
{
int tmp = 0;
Random rnd = new Random();
for (int i = 0; i < 9; i++)
{
bool isExist = false;
do
{
isExist = false;
tmp = rnd.Next(0, 9);
for (int j = 0; j < i; j++)
{
if (tmp == p[j]) { isExist = true; }
}
}
while (isExist);
p[i] = tmp;
}
}
private Points GetLocation(int value)
{
return FieldValue[value];
}
public void drawField()
{
Console.WriteLine("----------------------------");
for (int i = 0; i < nw; i++)
{
for (int j = 0; j < height; j++)
{
Console.Write(field[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("----------------------------");
}
public bool repeat(double Length, int[] point)
{
for (int i = 0; i < Length; ++i)
{
for (int y = i + 1; y < Length; ++y)
{
if (point[i] == point[y])
{
Console.WriteLine(point[i] + " ==" + point[y]);
throw new ArgumentException("Numbers should not be repeated");
}
}
}
return true;
}
public Boolean finish()
{
bool temp = false;
int value = 1;
for (int i = 0; i < nw; ++i)
{
for (int j = 0; j < height; ++j)
{
if (field[i, j] == value)
{
temp = true;
++value;
if (value == Length)
{
value = 0;
}
}
else
{
return false;
}
}
}
return temp;
}
public void Move(int value)
{
try
{
Console.WriteLine(value);
if (value > 15 || value < 0)
{
throw new ArgumentException();
}
int x = GetLocation(0).x;
int y = GetLocation(0).y;
int ValueX = GetLocation(value).x;
int ValueY = GetLocation(value).y;
if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1)) || (ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
{
field[x, y] = value;
field[ValueX, ValueY] = 0;
var vere = FieldValue[0];
FieldValue[0] = FieldValue[value];
FieldValue[value] = vere;
}
}
catch (ArgumentException)
{
Console.WriteLine("There is no such number, try again: ");
}
catch (Exception)
{
Console.WriteLine("Next to this number is not 0, try again: ");
}
}
}
}
<pre>using System;
using System.Collections.Generic;
namespace ConsoleApplication9
{
class program
{
static void Main()
{
int i;
int[] p = new int[16];
for (i = 0; i < 16; i++)
{
p[i] = i + 1;
}
p[15] = 0;
int n;
Console.WriteLine("Input integer number size: 4-4x4, 3-3x3 ");
n = int.Parse(Console.ReadLine());
if(n == 4)
{
puzzle4.game1();
}
if(n == 3)
{
puzzle3.game2();
}
}
}
class puzzle3
{
public static void game2()
{
int i;
int[] p = new int[9];
for (i = 0; i < 9; i++)
{
p[i] = i + 1;
}
p[8] = 0;
Console.WriteLine("15 Puzzle");
Game2 MyGame = new Game2(p);
int score = 0;
MyGame.mixer(p);
for (;;)
{
MyGame.drawField();
int a;
Console.WriteLine("Change number: ");
string input = Console.ReadLine();
if (Int32.TryParse(input, out a))
{
MyGame.Move(a);
}
else
{
Console.WriteLine("Error input");
}
if (MyGame.finish())
{
MyGame.drawField();
Console.WriteLine("Winner");
Console.WriteLine("Game end");
break;
}
score++;
Console.WriteLine("Number of moves: " + score);
}
}
}
class puzzle4
{
public static void game1()
{
int i;
int[] p = new int[16];
for (i = 0; i < 16; i++)
{
p[i] = i + 1;
}
p[15] = 0;
Console.WriteLine("15 Puzzle");
Game MyGame = new Game(p);
int score = 0;
MyGame.mixer(p);
for (;;)
{
MyGame.drawField();
int a;
Console.WriteLine("Change number: ");
string input = Console.ReadLine();
if (Int32.TryParse(input, out a))
{
MyGame.Move(a);
}
else
{
Console.WriteLine("Error input");
}
if (MyGame.finish())
{
MyGame.drawField();
Console.WriteLine("Winner");
Console.WriteLine("Game end");
break;
}
score++;
Console.WriteLine("Number of moves: " + score);
}
}
}
class GameHistory
{
public Stack<Game> History { get; private set; }
public GameHistory()
{
History = new Stack<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!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
What OG said, with two addendum that will keep other developers from murdering you in the future:
- Use meaningful names in your source code. When you find yourself naming classes things like "repeat" or "Game2" you need to re-evaluate life a little.
- Don't use exceptions for flow control. Never. Never ever.
"There are three kinds of lies: lies, damned lies and statistics."
- Benjamin Disraeli
|
|
|
|
|
There is an implementation of the game, you need to roll back the previous move, choose the level of complexity (the size of the playing field 3x3, 4x4), using the Singleton patterns, Factory method, Command, Memento, help.
<pre>using System;
namespace puzzle15
{
class Points
{
public readonly int x, y;
public Points(int x, int y)
{
this.x = x;
this.y = y;
}
}
class Game
{
public int Length=0;
const int nw = 4, height = 4;
int[,] field = new int[nw, height];
Points[] FieldValue = new Points[16];
public Game(int[] point)
{
int r = 0;
string[] file = new string[4];
Length = 16;
mixer(point);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < nw; i++)
{
field[j, i] = point[r];
FieldValue[point[r]] = new Points(j, i);
r++;
}
}
}
public void mixer(int[] p)
{
int tmp = 0;
Random rnd = new Random();
for (int i = 0; i < 16; i++)
{
bool isExist = false;
do
{
isExist = false;
tmp = rnd.Next(0, 16);
for (int j = 0; j < i; j++)
{
if (tmp == p[j]) { isExist = true; }
}
}
while (isExist);
p[i] = tmp;
}
}
private Points GetLocation(int value)
{
return FieldValue[value];
}
public void drawField()
{
Console.WriteLine("----------------------------");
for (int i = 0; i < nw; i++)
{
for (int j = 0; j < height; j++)
{
Console.Write(field[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("----------------------------");
}
public bool repeat(double Length,int[] point)
{
for (int i = 0; i < Length; ++i)
{
for (int y = i + 1; y < Length; ++y)
{
if (point[i] == point[y])
{
Console.WriteLine(point[i] + " ==" + point[y]);
throw new ArgumentException("Numbers should not be repeated");
}
}
}
return true;
}
public Boolean finish()
{
bool temp = false;
int value = 1;
for (int i = 0; i < nw; ++i)
{
for (int j = 0; j < height; ++j)
{
if (field[i, j] == value)
{
temp = true;
++value;
if (value == Length)
{
value = 0;
}
}
else
{
return false;
}
}
}
return temp;
}
public void Move(int value)
{
try
{
Console.WriteLine(value);
if (value > 15 || value < 0)
{
throw new ArgumentException();
}
int x = GetLocation(0).x;
int y = GetLocation(0).y;
int ValueX = GetLocation(value).x;
int ValueY = GetLocation(value).y;
if ((ValueX == x && (ValueY == y - 1 || ValueY == y + 1))||(ValueY == y && (ValueX == x - 1 || ValueX == x + 1)))
{
field[x, y] = value;
field[ValueX, ValueY] = 0;
var vere = FieldValue[0];
FieldValue[0] = FieldValue[value];
FieldValue[value] = vere;
}
}
catch (ArgumentException)
{
Console.WriteLine("There is no such number, try again: ");
}
catch (Exception)
{
Console.WriteLine("Next to this number is not 0, try again: ");
}
}
}
class Points2
{
..............................................................................................
}
class Game2
{
public int Length = 0;
const int nw = 3, height = 3;
int[,] field = new int[nw, height];
Points[] FieldValue = new Points[9];
public Game2(int[] point)
{
int r = 0;
string[] file = new string[3];
Length = 9;
mixer(point);
for (int j = 0; j < height; j++)
{
for (int i = 0; i < nw; i++)
{
field[j, i] = point[r];
FieldValue[point[r]] = new Points(j, i);
r++;
}
}
}
.................................................................................................
}
}
using System;
using System.Collections.Generic;
namespace puzzle15
...............................................................................................
class GameHistory
{
public Stack<Game> History { get; private set; }
public GameHistory()
{
History = new Stack<Game>();
}
}
}
|
|
|
|
|
And?
There is no question here, just a code dump!
What is the problem?
What is happening that you didn't expect, or not happening that you did?
What help do you need?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
I want to compose message while getting part of the message from excel column data where i should be able to put any column on any part of the message, example,if i have excel with 2 columns, i should be able to compose message as below:
Hello {Name}, Kindly renew your license no {licenseNo}
or
Your license {licenseNo} expires on 28/09/2017. Thank you {Name}.
Where {Name} and {licenseNo} will be obtained from excel file.
|
|
|
|
|
And?
What have you tried?
Where are you stuck?
What help do you need?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
Am unable to concatenate my typed messages with the data on the excel columns.
|
|
|
|
|
At the risk of repeating myself:
Quote: And?
What have you tried?
Where are you stuck?
What help do you need?
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So when you give us no real information, we can give you no real help.
Help us to help you!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi,
Maybe this will help in better explaining what i wish to achieve.
I have excel document containing Cell Numbers and Names of customer. I want to be able to write custom text messages to all customers by concatenation typed text and data on excel such that i will be able to send same message to all the customers on the list for example. I will type "Hello" then insert the column name {Name} and proceed to add more text. This should ready "Hello Mike" where Mike is obtained from say cell A2 on my excel.
I am using C# and I have designed form as follows:
1. Option to select the excel with the cell numbers and name.
2. Text box, where am typing my text.
3. I have drop down list that populates the column headers from excel (eg Name, cell number).
4. I have a multi line text box where my messages will be displayed
I wish to compose my message as follows:
a. Enter text eg "Hello" and press add text to add text to the multi line text box
b. Select the column eg Name and click on Add Column to add column variable to the multi line text box
c. The text area should show 'Hello Mike' generated by Hello {Name}.
Am stuck at the point of merging/concatenation my typed text and data from excel to form one complete message.
Here is the screenshot of what i have done so far.
sshot.PNG - Google Drive[^]
|
|
|
|
|
So you can read the data from Excel ok, and - given the cell name - access the column.
But you are having problems replacing the text? That's trivial, compared to the excel bit...
The way I'd do it is to start with a regex: scan the input text for "{...}" data, and extract the content as a column name:
(?<ColName>{.*?}) Will do it.
The Matches collection will give you the name of column in Excel, surrounded by the curly brackets. Read that column from your Excel data, and use string.Replace to change all instances.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've never seen anything like this, but I guess it's similar to a JQuery Json request in which you have a Url and the data is the querystring.
So for an example, https://secure.shippingapis.com/ShippingAPI.dll?API=RateV4&XML=<xml> is simply the request and you wait for the response on the XML page.
I have no clue how to transmit this and get the response XML. Just looking for suggestions.
I tried this but it just returns the request.
WebRequest request = WebRequest.Create(endpointUrl + txn);
WebResponse response = request.GetResponse();
Console.WriteLine(response.ResponseUri);
If it ain't broke don't fix it
|
|
|
|
|
I have this working for now, but it seems overkill. endpointUrl is the URL and txn is the XML, rxn is the return string to deserialize.
var httpWebRequest = (HttpWebRequest)WebRequest.Create(endpointUrl + txn);
httpWebRequest.ContentType = "application/xml";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{<br />
streamWriter.Write("");
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
rxn = result.ToString();
}
If it ain't broke don't fix it
|
|
|
|
|
Would these encodings be the fault of the way I'm transmitting this from my previous post?
<MailService>Priority Mail Express 2-Day<sup>™</sup></MailService>
If it ain't broke don't fix it
|
|
|
|
|
the encoded stuff was just a trademark.
Works good enough now to finish the API
If it ain't broke don't fix it
|
|
|
|
|
hi thnx for helping me in past question
so i Reach here [ex image]
by using this code
private void Form1_Load(object sender, EventArgs e)
{
string img = @"D:\Capture1.PNG";
Bitmap bmp = new Bitmap(img);
pictureBox1.Image = Image.FromFile(img);
int w = bmp.Width;
int h = bmp.Height;
int f = 1;
Random rr = new Random();
for (int y=0;y < h ; y++)
{
for (int x=0 ; x < w ; x++)
{
int a = rr.Next(256);
int r = rr.Next(256);
int g = rr.Next(256);
int b = rr.Next(256);
bmp.SetPixel(x, y, Color.FromArgb(a, r, g, b));
}
}
pictureBox2.Image =bmp;
}
but the problem is the code chose random value to each pixel
how can i chose the value of the pixel and not make it random ?
|
|
|
|
|
Use Bitmap.GetPixel instead of Random.
I can't help thinking that if you invested as much work in thinking about the problem and actually looking at your code as you are in asking questions, you would get these tasks done a lot quicker and easier - this isn't complicated stuff you are doing here...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Dear All,
I have export one graph from placeholder by using save image file.
but i want export whole placeholder with graph in excel sheet like grid view export.
Please help me.
|
|
|
|
|