Reputation: 1420
I need some instructions on the best way to do this, I have some pseudo code.
/*form needs to be a drop down as below*/
// -------------------------------
// | X |
// -------------------------------
// | "chose your option" |
// | |
// | [choice 1 [v]] |
// | |
// | [ok] [cancel] |
// -------------------------------
int optionchosen = confirmoptionbox();
if (optionchosen==1){
//do something
}
if (optionchosen==2){
// do something else
}
if (optionchosen==3){
// third way
}
//etc etc
Now, I know how to do that with a new form (etc etc) but I was genuinely wondering if there was a more "elegant" option that doesn't involve heaps of things
Upvotes: 0
Views: 536
Reputation: 16623
Sincerly I don't see where is the problem writing the form by yourself. However the pseudocode you wrote is almost perfect, infact it is the best way of doing it. Althought your pseudocode could be improved as I'm going to write. Speaking of the form you could structure it in this way to make it reusable:
class myForm : Form{
public int Result;
private Label lblText;
private Button btnOk, btnCancel;
private CheckBox[] checkboxes;
public myForm(string text, params string[] choicesText){
//set up components
lblText = new Label(){
Text = text,
AutoSize = true,
Location = new Point(10, 10)
//...
};
checkboxes = new CheckBox[choicesText.Length];
int locationY = 30;
for(int i = 0; i < checkboxes.Length; i++){
checkboxes[i] = new CheckBox(){
Text = choicesText[i],
Location = new Point(10, locationY),
Name = (i + 1).ToString(),
AutoSize = true,
Checked = false
//...
};
locationY += 10;
}
btnOk = new Button(){
Text = "OK",
AutoSize = true,
Location = new Point(20, locationY + 20)
//...
};
btnOk += new EventHandler(btnOk_Click);
//and so on
this.Controls.AddRange(checkboxes);
this.Controls.AddRange(new Control[]{ lblText, btnOk, btnCancel /*...*/ });
}
private void btnOk_Click(object sender, EventArgs e){
Result = checkboxes.Where(x => x.Checked == true).Select(x => Convert.ToInt32(x.Name)).FirstOrDefault();
this.Close();
}
}
Then in the main form:
using(myForm form = new myForm("Select a choice", "choice 1", "choice 2")){
form.ShowDialog();
int result = form.Result;
switch(result){
case 1: MessageBox.Show("You checked choice 1") ; break;
case 2: MessageBox.Show("You checked choice 2") ; break;
default: MessageBox.Show("Invalid choice"); break;
}
}
PS:
Here I used checkboxes but you can change it and add a combobox with a dropdown style, and then you will have what you need.
Upvotes: 1
Reputation: 2399
design a form .open it up as a modal window and get value from NewForm
NewForm nf=new NewForm();
nf.ShowDialog();
Upvotes: 1