As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Needs details or clarity Add details and clarify the problem by editing this post.
I'm working in a chat program using C# and i need to give to every user a different color ,
=>So I need a function to change color of writing in C#
Thanks
I am going to assume this is a WinForms questions (which it feels like, based on it being a "program" rather than a website/app). In which case you can simple do the following to change the text colour of a label:
myLabel.ForeColor = System.Drawing.Color.Red;
Or any other colour of your choice. If you want to be more specific you can use an RGB value like so:
myLabel.ForeColor = Color.FromArgb(0, 0, 0);//(R, G, B) (0, 0, 0 = black)
Having different colours for different users can be done a number of ways. For example, you could allow each user to specify their own RGB value colours, store these somewhere and then load them when the user "connects".
An alternative method could be to just use 2 colours - 1 for the current user (running the app) and another colour for everyone else. This would help the user quickly identify their own messages above others.
A third approach could be to generate the colour randomly - however you will likely get conflicting values that do not show well against your background, so I would suggest not taking this approach. You could have a pre-defined list of "acceptable" colours and just pop one from that list for each user that joins.
You can try this with Color.FromArgb:
Random rnd = new Random();
lbl.ForeColor = Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Can anyone give me a practical usage where clone is used, I understand how its done and what are the types in it, but I dont see its practical usage. Also I read it somewhere that it is used to save the state of the object, but I still think that rarely the entire state of object would be so important.
If you have an object y encapsulated (for instance it is private) within object X, but you provide an accessor method (such as getY()) then you risk returning the reference to your private object and that's not good. You don't want client code to be able to get the reference to it. So instead you might copy or clone the object within getY(). As mentioned though copying is probably preferred vs. clone -- see Effective Java item 11.
Here is the example code:
public class X {
private Date y = new Date();
// other code here
public Date getY() {
//this could be bad:
//return y;
//this is good:
return new Date(y);
}
Sometimes you load your object properties to a form then bind each property to a (textbox,combobox, ...), after you do changes to the (textbox, checkbox,...) even if you cancel you can't get the original values, so cloning is the best option,
clone your object then bind, if you want to cancel the original object is always unchanged, this is just one case
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am developing a Windows Application by using C#. I have a form in which I dragged one textBox and one button control. I want to retrieve the user Full Computer Name in the textBox by clicking the button.I wrote some codes for that as..........
private void button1_Click(object sender, EventArgs e)
{
string name = System.Environment.MachineName;
textBox1.Text = name[0].ToString();
}
By clicking the button, it retrieves only the first letter(eg: D for Donald-PC) of the Computer Name by which it starts but I want to retrieve the Full Name(eg: Donald-PC). Please someone help if I have make any change in my codes.Thanks
Just output the full name, not just the 0 index:
textBox1.Text = name;
You are taking the first character of System.Environment.MachineName. You need to do this:
textBox1.Text = System.Environment.MachineName;
As an aside, textBox1 for a control name is really lousy.
You are using char index of array, drop [0] and use just name.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am developing a website in C# and ASP.NET MVC where people can manage their own web pages. At the moment I am using the permalink solution of StackOverflow but I am not sure if this will work in my situation because people will add and delete pages constantly. This means that the id in the pages table will grow very large.
Example: mydomain.com/page/17745288223/my-page-title
Is there a better solution?
I think that for your case (users creating pages) it's actually more user friendly to put all pages created by a single user under his/her own path i.e.:
mydomain.com/page/{username/nickname/some-name-selected-by-user}/my-page-title
If you don't want to use such format an int or long in URL will probably do.
Well, you could use some kind of a hash to make lookups more efficient. You could, for instance compute a SHA-1 hash of page title, creating date, user information, etc. - just like git does for commit ids.
Or you could use simple numbers, but convert them into some compact representation using hexadecimal numbers or alphanumerical characters like some url-shortening services.
Though this started as a comment I decided it was growing larger so here it is again..
The page id solution seems just fine.
What are you worried about? If you are expecting a few million pages that's 7 characters. If you are expecting more than a few billion pages that's 9 - 10 characters.. Pretty manageable, I think.
You could also represent it as hex and reduce it to a maximum of 8 characters to fit up to 2^32 different ids.
This means that the id in the pages table will grow very large.
What's the problem with that?
The largest value for an int is also very large (just over 2 billion) so I doubt it will hit any limit unless you are planning to have millions of users with thousands of pages each.
If you are still worried then you can use a long (64-bit integer). It can handle trillions of users with millions of pages each. Note that the population of the Earth is only a few billion.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
For my parser i want to (depending on user input) save parse stats for the sites a user chooses to parse.
So lets say the user chooses site A, site B and site D:
I then from my threads want to be able to add different statitsitcs like; links parsed, pages parsed, duplicates found, etc. (so basically int values for the sites).
How would i create this into c#?
My idea is to make some kind of multidimensional array?
Is there a better way?
Any suggestions are very welcome :)
I would create a class that encapsulates the statistics and than use a dictionary with a website name as key.
public class Statistics
{
public int PagesParsed { get; set; }
}
var collection = new Dictionary<string, Statistics>();
collection.Add("Site A", new Statistics { PagesParsed = 42 });
var siteAStatistics = collection["Site A"];
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to store values permanently in a variable. The same variable should be able to be edited and saved by user, and whatever it is I need the value of the variable from the last time I modified it, like a database. Please explain me with some examples!
You need to serialize it somewhere that persists when the computer is rebooted, and then read it when your program starts again. This could be a database, (for simpler DBs, look at something like SQLite) an XML file, etc.
You can't. You need persistent storage for this (database, file, registry, etc)
Depending on your application you can save your variables for example in a config file or database.
After your application gets started again you load the settings.
MSDN - Application Settings Overview
c# store user settings in database
write the data in a file. when you than restart the application you can read the parameters out of this file and set some variables.
file could mean - sql, db, ini or whatever you are using.
all the best
you can use Properties.Settings advantage is that you can Save a variable per user scope or have Application scope at the same time
internal class Program
{
private static void Main(string[] args)
{
Properties.Settings1.Default.Test = "StackOverFlow";
Properties.Settings1.Default.Save();
Console.ReadKey();
}
}