|
Something like this?
public Form1()
{
InitializeComponent();
ComboBox combo = new ComboBox();
combo.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
("3 sided die") = 3,
("4 sided die") = 4,
("5 sided die") = 5,
("6 sided die") = 6,
}
Gives me an error in the enum "Identifier" expected.
Commented out my last population of the comboBox if I still need to use that. Not sure if I am on the right track here.
Then if I use a enum I need to change my button that is
private void button1_Click(object sender, EventArgs e)
to a private enum?
This comboBox really made me pull some hairs but you need to learn somewhere 
|
|
|
|
|
Well you can't write an enumeration that way, but that's the idea. Did you read the article I linked to?
|
|
|
|
|
I'm sorry, I checked all your replys but I can't see a link. I really appreciate you trying to push me into the right direction though 
|
|
|
|
|
At the bottom of my first reply where it says Using human readable enum values in a ComboBox[^]
|
|
|
|
|
 Ok, reading that link you gave me I got the comboBox working but I am missing something here when I add things to my button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
[Description("3 sided die")] Three = 3,
[Description("3 sided die")] Four = 4,
[Description("3 sided die")] Five = 5,
[Description("3 sided die")] Six = 6,
}
public class RollDice
{
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
Random rndGen = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
private void button1_Click(object sender, EventArgs e)
{
var dice = new RollDice();
var numberOfSides = comboBox1.SelectedItem;
var numberOfTimes = textBox1.Text;
int result = dice(numberOfSides, numberOfTimes);
result = (dice);
richTextBox1.Text = (result).ToString();
}
}
}
dice under my button is the problem, "is a 'variable' but used as a 'method'". I thought I could call that variable from the method above?
|
|
|
|
|
Which exact line?
"Random rndGen = new Random"
Make rndGen a static field and instantiate it only once.
"(result)."
You don't need the parentheses.
|
|
|
|
|
 Ok so I moved my random out and made it a static but I still don't get how to call it all on my button. Complete code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
[Description("3 sided die")] Three = 3,
[Description("4 sided die")] Four = 4,
[Description("5 sided die")] Five = 5,
[Description("6 sided die")] Six = 6,
}
public class RollDice
{
public static Random rndGen = new Random((int) DateTime.Now.Ticks);
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
private void button1_Click(object sender, EventArgs e)
{
var dice = new RollDice();
var numberOfSides = comboBox1.SelectedItem;
var numberOfTimes = textBox1.Text;
int result = dice(numberOfSides, numberOfTimes);
richTextBox1.Text = result.ToString();
}
}
}
Getting the feeling this should be easy but I just can't see it 
|
|
|
|
|
Why is RollDice a separate class?
Because RandomDice is static you call it as int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
var numberOfSides = comboBox1.Text;
var numberOfTimes = textBox1.Text;
int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
}
Gives me an error after the int result =
class Dice.Form1.RollDice
Error:
The best overloaded method match for 'Dice.Form1.RollDice.RandomDice(int, int)' has some invalid arguments
|
|
|
|
|
Angel_78 wrote: textBox1.Text
That's a string; you'll need to parse it. See Int32.TryParse
Or, better yet, don't use a TextBox, use a NumericUpDown.
|
|
|
|
|
Ok so I changed my numberOfTimes textBox1 to a numericUpDown1 and I sort of got it to work but not the way I want it to.
private void button1_Click(object sender, EventArgs e)
{
var numberOfSides = (int)comboBox1.SelectedValue;
var numberOfTimes = (int)numericUpDown1.Value;
int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
richTextBox1.Text = Convert.ToString(result);
}
All I get is the total random number displayed to my richTextBox1, I want it to display each roll separately.
What am I missing here?
modified 14-May-12 17:44pm.
|
|
|
|
|
Angel_78 wrote: Convert.ToString(result);
No need for Convert; just use result.ToString() (in general, you should never need Convert, so avoid it).
Perhaps you should set a breakpoint in RandomDice and step through to see what's happening.
|
|
|
|
|
In application I made for one client I have a lots of forms with a datagridviews containing comboboxcell usually showing parent data. for example datagridview 'Orders' has as 'Partner' column with combobox cells.
application has been working for a few years now, everything works great on different computers with different MS OS ( Win XP, Win 7). on all except one computer where on exiting a form throws 'Error: DataGridViewComboBoxCell Value is not Valid '. all prerequisites are instaled correctly, I even checked regional settings, they are same as on any other computer. I don't know what else should I try.
anybody with a experience in this sort of problems?
|
|
|
|
|
markoVVVV wrote: 'Error: DataGridViewComboBoxCell Value is not Valid '.
Can you reproduce the error at will? Can you Debug.Print the value?
Bastard Programmer from Hell
|
|
|
|
|
You could write "MessageBox.show()" in exit module.If the program could show this messageBox when run.Then this "MessageBox.show" will
ahead moved. If the program couldn't show the "MessageBox.show()",then the ahead code error.
|
|
|
|
|
Hi,
I am trying to load 100 MB of Text file in rich text box, but the application gets hang or some times it gives out of memory exception.
I tried below
Case1: it is running for long time
fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
sr = new StreamReader(fs);
//rtxtFile.AppendText( sr.ReadToEnd());
while (!sr.EndOfStream)
{
rtxtFile.AppendText(sr.ReadLine() + System.Environment.NewLine);
}
Case2: out of memory exception
fs = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
sr = new StreamReader(fs);
rtxtFile.AppendText( sr.ReadToEnd());
anybody give the solution for the above problem;
with reagrds,
Vishnuvarthan.V
|
|
|
|
|
Member 2389379 wrote: I am trying to load 100 MB of Text file in rich text box
Member 2389379 wrote: anybody give the solution
stop trying that. Try some common sense.
|
|
|
|
|
Why are you trying to do this? It's highly unlikely that the user will read all that data - and those that do certainly won't do it in one massive window containing the whole file. Rather than trying to read all that data in, why not just retrieve a subset of that data and display the subset instead? That way, you can keep your application memory size down to a minimum, and give the user a much better text browsing experience.
|
|
|
|
|
Member 2389379 wrote: I am trying to load 100 MB of Text file in rich text box
And you expected this to take how long? It'll probably take the RTB about a half hour to load all that, depending on content.
Member 2389379 wrote: anybody give the solution for the above problem;
That's easy. Don't load 100MB into the RTB.
|
|
|
|
|
Yes, that is quite big and it suprising just how slow the AppendText/ReadLine combination actually is.
I think your best bet would be to use the LoadFile(filename, RichTextBoxStreamType.PlainText) method as it may manage memory more efficiently than AppendText/ReadToEnd.
By the way on my system a 100MB, 2,302,561 line file takes about 30 seconds to load using LoadFile. In contrast the AppendText/ReadLine method started about 15 minutes ago and according to TaskManager only 7MB has been read so far. The CPU is taking a remarkable beating for so little progress.
Alan.
|
|
|
|
|
Hi,
I've got an issue concerning dictionaries.
The situation is like this.
I've got a database with a table containing peoples and their functions. Now, I want a dictionary with the functions as key, and all the corresponding people in a IEnumerable-list as value.
So, something like this:
("Student", "Joe Palmer, Marc Speed, Scott Roberts")
("Teacher", "Felipe Bauer, Lewis Hamilton, Jenson Spencer")
("Administration", "Mike Stanford", "Philip Carrey")
Where the function is the key, and the names are the values. So the first element of the dictionary has "student" as key, and a collection of 3 names as value.
Could someone help me out with this? I've tried to accomplish this, but I keep on struggling...
Many thanks in advance!
Greetz,
Mark
|
|
|
|
|
go for a Dictionary<string, List<Person>>
The add operation takes a test, akin to:
public void add(string function, Person person) {
if (!dictionary.ContainsKey(function)) dictionary.Add(new List<Person>());
List<Person> persons=dictionary[function];
persons.Add(person);
}
|
|
|
|
|
Thanks!
It was so simple when I saw your solution... 
|
|
|
|
|
You're welcome.
|
|
|
|
|
I'm trying to read in a list of VS2010 Projects from the registry:
private const string VS_PROJECT_MRU_PATH = @"Software\Microsoft\VisualStudio\10.0\ProjectMRUList";
public static List<string> GetSubKeyNodeNames(BaseKey BaseKey, string SubKey)
{
List<string> retVal = null;
RegistryKey baseKey;
if (SubKey != "")
{
baseKey = _GetRegistryBaseKey(BaseKey);
baseKey = baseKey.OpenSubKey(SubKey, false);
}
else
{
baseKey = _GetRegistryBaseKey(BaseKey);
}
if (baseKey == null && SubKey != "")
{
throw new Exception("Registry subkey not found");
}
else
{
string[] names = baseKey.GetValueNames();
retVal = (from n in names
select n).ToList();
}
return retVal;
}
I have verified that the key DOES indeed exist. I just doesn't open. The problem is similar to this[^] except that I'm reading from HKEY_CURRENT_USER, not HKEY_LOCAL_MACHINE.
Anyone know what;s going on here?
Thanks
Everything makes sense in someone's mind
|
|
|
|