|
Here's a minimal example, without validation, etc.
Snippet from the main form (extraneous stuff omitted):
class MainForm : Form
{
private string chosenPizza = "Pepperoni";
private bool cheesy = true;
private void buttonChooseNewPizza_Click(object sender, EventArgs e)
{
PizzaOrderForm orderForm = new PizzaOrderForm();
orderForm.PizzaType = chosenPizza;
orderForm.Cheesy = cheesy;
DialogResult answer = orderForm.ShowDialog(this);
if (answer == DialogResult.OK)
{
chosenPizza = orderForm.PizzaType;
cheesy = orderForm.Cheesy;
labelOrderStatus.Text = "Your " + chosenPizza + " pizza with" +
(cheesy ? "" : "out") +
" cheese is coming right up!";
}
}
}
And from the sub-dialog form:
partial class PizzaOrderForm : Form
{
public PizzaOrderForm()
{
InitializeComponent();
}
private string pizzaType;
public string PizzaType
{
get
{
return pizzaType;
}
set
{
switch (value.ToLower())
{
case "pepperoni":
case "hawaiian":
case "vegetarian":
pizzaType = value;
break;
default:
throw new ArgumentOutOfRangeException(string.Format("Sorry, {0} pizza is not on the menu.", value ));
}
}
}
private bool cheesy = true;
public bool Cheesy
{
get
{
return cheesy;
}
set
{
cheesy = value;
}
}
private void PizzaOrderForm_Load(object sender, EventArgs e)
{
textBoxPizza.Text = PizzaType;
checkBoxCheese.Checked = Cheesy;
}
private void buttonOK_Click(object sender, EventArgs e)
{
PizzaType = textBoxPizza.Text;
Cheesy = checkBoxCheese.Checked;
}
}
Also, the OK and Cancel button's DialogResult properties should be set accordingly.
(If you're using .NET 1.0 or before, don't be confounded by the partial class, it just lets the form designer do all its code generation in a separate file).
Matt Gerrans
|
|
|
|
|
Do one thing Take a Global Module and Decalre a variable which is of type public. and Assign the Textbox value to that .U can access that Variables value from any form.
Deepak
|
|
|
|
|
Please don't do this, for your own sake. It may seem simple and clean at first, but you will surely regret it as your app grows.
Even the solution I gave above is not ideal; the data should probably be encapsulated in some objects, which could be passed from one form to the other (not by events, but in the same way I did with individual variables).
Matt Gerrans
|
|
|
|
|
Hi guys,
I have created a Visual Studio setup project, and when I build it I get a .msi installer file. Does anyone know how to create a single .exe installer file, or how to convert a .msi file to an .exe file that can be downloaded? Thanks.
|
|
|
|
|
Why don't you want an .MSI?
|
|
|
|
|
Hi,
I have nothing against .msi files... I'm just wondering why every file I download off the web is .exe and never .msi, and if there is any advantage to using .exe. What do people use to create an .exe file?
Yigal
|
|
|
|
|
Hi,
When I create a context menu with C#, only the left button can select entries in the menu (AFTER the menu has been invoked by a right click). Does anyone know how to enable selection using the right button, like the way it is in most Windows applications?
Thanks.
|
|
|
|
|
I have found out why this happens: The way the context menu works depends on which control it's attached to. For example, when the context menu is attached to a text box, the various context menu click events are only invoked when the left mouse button is clicked; when the context menu is attached to a RichTextBox clicking any mouse button on a context menu selection works. Other controls I've tried: Panel, Label and Form, behave like a text box (only left button works).
Can anyone explain this inconsistency?
Yigal
|
|
|
|
|
I guess this question has been asked before, but I could not find any solution for it so here it is again:
How can I download gif-file from internet (URL) (using C# code)?
I want to use this function in a Pocket PC program that I am currently writing.
Thanks in advance!
Frank, Norway
|
|
|
|
|
have you attempted
Bitmap bm = new Bitmap(@"http://<absolute address="" of="" picture="">.gif");
|
|
|
|
|
you can do that?~?~!
/\ |_ E X E GG
|
|
|
|
|
It did not work.
I am trying to convert the "Threaded WebDownload class with Progress Call-backs" project to a SmartDevice project. It is working fine on the PC but it crashes when I run the application on the PPC (WM 2003). It seems to crash when this statment is excecuted :
DownloadThread dl = new DownloadThread();
I don't know why.
|
|
|
|
|
I have a string that contains some text. I want to check if the text can be converted into an integer. At the moment my code looks like this:
bool isInt = true;
try
{
int intValue = int.Parse(stringValue);
}
catch(Exception e)
{
isInt = false;
}
At the end of the code isInt will contain whether the string value can be converted to an int or not.
Is there a better way to do this? I don't like having to catch an exception when I expect that many string values will not be convertable to an int. It seems very inefficient.
|
|
|
|
|
It's the most secure way to convert a string to an int.
Why don't you put just all assignments into one single try-catch block?
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg wrote:
Why don't you put just all assignments into one single try-catch block?
I don't want to convert it. This is part of some validation code. I just want to see if it is possible to convert it.
|
|
|
|
|
Since someone voted my previous reply a 3 I can only guess that I didn't explain myself very well.
I don't have multiple assignments. I only want to check one value and I only want to check if it is possible to convert it to an integer. I do not want to do an actual conversion.
If further explanation is needed, I'll be happy to explain more.
|
|
|
|
|
|
A double is no use to me. It will accept as valid values that are not valid integers. It is a pity, however, that there doesn't seem to be an equivalent for Int32.
|
|
|
|
|
I suggest you actually read the documentation.
There's a flag controlling what kind of numbers to parse.
mav
|
|
|
|
|
mav.northwind wrote:
I suggest you actually read the documentation.
I did read the documentation, but it does not explain on the TryParse page that it can be used to check integers. That is pretty useless documentation if you have to delve into sub-documents to find something that should have been at least mentioned on the first page even if there was no details about it.
Thank you for at least prodding me to read further because I did eventaully find the answer on this route. I hope you don't mind my little rant at the documentation. MSDN is usually very good.
|
|
|
|
|
Glad to hear you found the information.
The NumberStyles enumeration is in fact not explained on the page of TryParse, but that's the standard way MSDN is built. It's a separate type, so it's documented separately. With a proper link, though.
Anyway, once you actually have found the information you needed it's usually there where you'd expect it to be. But the location is only obvious _after_ you've found it, not when you're actually trying to find it
mav
|
|
|
|
|
|
I don't know how fast this is compared to the other options, but this way won't throw any exceptions, which are inefficient. Using regular expressions to validate input is a common practice:
bool isNumber = System.Text.RegularExpressions.Regex.IsMatch(stringValue, "[0-9]");
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Conversation With a Muslim
Judah Himango
|
|
|
|
|
But you have to be careful with regular expressions, since they are only testing what you think should be a valid int, not what really may be valid. For example, the regular expression you offered won't do too well on negative values.
Matt Gerrans
|
|
|
|
|
What would be better is just catching an InvalidCastException, instead of anything that may go wrong.
Christian Graus - Microsoft MVP - C++
|
|
|
|