I am currently making a small application for a BMI calculation. The program compiles fine, but I get a run time error of
Format Exception Unhandled
in this line:
height = float.Parse(textBox1.Text);
The line is a part of the function:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
float height;
height = float.Parse(textBox1.Text);
height = height*height;
}
You haven't said what's in the text box when you parse it. Perhaps it's empty, or the user's typed something like "fred". You should always assume that the input could be invalid:
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
float height;
if (!float.TryParse(textBox1.Text, out height))
{
// Indicate to the user that the input is invalid, and stop processing
// at this point. For example, you may want to highlight the textbox with
// a red box. Return at the end of the block.
}
// It parsed correctly: continue...
height = height*height;
...
}
(This would probably be structured slightly differently in an MVVM approach, but you'd still want to use float.TryParse to test the user input before accepting it.)
Related
I have used below code to print the Panel of windows form.
private void button1_Click(object sender, EventArgs e)
{
System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(Doc_PrintPage);
doc.Print();
}
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Panel grd = new Panel();
Bitmap bmp = new Bitmap(panel2.Width, panel2.Height, panel2.CreateGraphics());
panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height));
RectangleF bounds = e.PageSettings.PrintableArea;
float factor = ((float)bmp.Height / (float)bmp.Width);
e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width);
bmp.Save("test12.jpg");
}
Now from above code, when i click on button the print function will be call but it excluded label in it. i am attaching image for your reference. first image is my UI design. , when i use print functionality it removes the label value as you can see in other image. i have used rectagleshap control which are in Pink color and i am displaying label on it. I think the label may be send back but when i used front back then also it is not appear.
Can you just try this one here i was using this for capture the whole screen which ever is active window its like screencapture or screenshot.
private void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
Bitmap bitmap = new Bitmap(panel2.Width, panel2.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.InterpolationMode = InterpolationMode.Default;
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
bitmap.Save(pathDownload + filename + ".jpeg", ImageFormat.Jpeg);
bmp.Save("test12.jpg");
}
In my case label was shown back to the rectangle, so i added one more label and set it as bring front. thanks for the help.
I am using Android.Support.V7.Widget.ListPopupWindow as a Drop-Down Menu from a Button within my layout. Here is the code snippet I am using
void MenuIcon_Click (object sender, EventArgs e)
{
popupWindow = new Android.Support.V7.Widget.ListPopupWindow (this);
popupAdapter = new MenuPopUpAdapter (this,selectedIndex,menuList);
popupAdapter.ItemClick+= PopupAdapter_ItemClick;
popupWindow.SetAdapter (popupAdapter);
popupWindow.AnchorView = menuButton;
Display display = WindowManager.DefaultDisplay;
Point size = new Point();
display.GetSize (size);
int width = size.X;
popupWindow.Width =160;
popupWindow.Show ();
}
But while debugging I noted that, even though I have given it a static width, it is rendered differently in different devices. What is causing this issue ?
This is because of the different screen densities in Android devices. You need to mention dimensions in DPs(Density Independent Pixels) to overcome this issue. This documentation from Google will be a nice read
You can get the corresponding pixel value to be mentioned while setting dimensions programatically from this method.
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = Resources.DisplayMetrics;
int px = (int)Math.Round(dp * (displayMetrics.Density));
return px;
}
You may modify the code as above to fix the issue
popupWindow.Width =dpToPx(160);
I am trying to build a calculator that will give me the calculation for width length and Area and perimeter. Now where I'm stuck, once I label the text box ( txtWidth) and then I click on it to bring up the code editor what do I put in under the handler. Second question how do I enter the math for them. Like I know that to get the area I just do widthlength and for perimeter it's 2*width + 2*length. And I also need to add fractional decimal entries like 10.5 and 20.65. I hope this gives more insight to what I'm trying to do.
Assuming you're using C#... add two labels (lblArea & lblPerimeter) and a button (btnCalculate), and add an event for the button (double-click on it):
Here's your homework:
private void CalculateDisplayInfo()
{
double len = 0.0;
if (!double.TryParse(txtLength.Text, out len))
{
MessageBox.Show(this, "Invalid Length Input", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
double width = 0.0;
if (!double.TryParse(txtWidth.Text, out width))
{
MessageBox.Show(this, "Invalid Width Input", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
lblArea.Text = (width * len).ToString();
lblPerimeter.Text = (2 * (width + len)).ToString();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
CalculateDisplayInfo();
}
Could anyone please help me , how I can programmatically increase or decrease the font size of web browser control in windows phone 7 c sharp . I need this to implement the zoom functionality in web browser control.
here is my code
private void btn_zoomin_click(object sender, EventArgs e)
{
double fs = webBrowser1.FontSize;
webBrowser1.FontSize = fs+10;
}
But the font is not being changed at all.
Any kind of help will be appreciated.
Many thanks,
Munazza
Yes there is a solution, you can fire a script inside the html page to enlarge the font by using the webBrowser's "InvokeScript" , here is a sample:
webBrowser1.InvokeScript("ChangeFontSize","12");
TO DO : a script inside the html :
<script type="text/javascript">
function ChangeFontSize(size) {
.....
....
return true;
}
<script/>
I don't think it's supposed to work this way.
The user can zoom in and out like he does in the Internet Explorer, but you can't change the font size.
You could, technically, get the html file that you want to display, parse it and if the fontsize is defined inline, you could change it and then display the new html file in the WebBrowser control, but I don't think that's a very good idea.
You have to use WebBrowser.InvokeScript:
// Initial text size
int textSize = 100;
private void TextPlusOnClick(object sender, EventArgs e)
{
textSize *= 2;
string szfn = "{styleText = \"body { -ms-text-size-adjust:" + textSize + "% }\";styleTextNode = document.createTextNode(styleText);styleNode = document.createElement(\"style\");styleNode.appendChild(styleTextNode);document.getElementsByTagName(\"head\")[0].appendChild(styleNode);};";
webBrowser.InvokeScript("eval", szfn);
}
private void TextMinusOnClick(object sender, EventArgs e)
{
textSize /= 2;
string szfn = "{styleText = \"body { -ms-text-size-adjust:" + textSize + "% }\";styleTextNode = document.createTextNode(styleText);styleNode = document.createElement(\"style\");styleNode.appendChild(styleTextNode);document.getElementsByTagName(\"head\")[0].appendChild(styleNode);};";
webBrowser.InvokeScript("eval", szfn);
}
See a description on the text size adjust property and this post on MSDN.
I want in this code in the any step label show the Number of that step.
In the my code just show last number in the label!
i can do it with doevent() but I think at times face with problem
enter code here
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
while (i<100)
{
i++;
label1.Text = string.Format("Step is :{0}", i);
Application.DoEvents();
label1.Invalidate();
System.Threading.Thread.Sleep(1000);
}
}
Assuming you want the counter to update the label while still performing the actions of Application.DoEvents(), you will likely need to run the tasks on a separate thread, else the code will execute and return the result after the thread has been released back to the application.