|
Hello,
One way is to do it like this (dNumber defined earlier, and in this case textbox is named txtBox):
double dNumber;<br />
<br />
try <br />
{<br />
dNumber = double.Parse(txtBox.Text);<br />
}<br />
catch(FormatException)<br />
{<br />
MessageBox.Show("You typed an invalid value\n\r" +<br />
"Please try again");<br />
}
This diplays an errorbox in case user puts in wrong kind of an input.
-PP
-- modified at 7:29 Wednesday 24th May, 2006
|
|
|
|
|
Howdy,
I'm having some problems having information delivered between a custom user component and the main form.
In the component I have a combobox to where I want to get information from the main form. I tried using the following expression:
<br />
string[] sectionRange = ((Proha)TopLevelControl).giveSections();
ProHa is the Class name. This works, but it says "Object reference not set to an instance of an object" for 3 times whenever I try to view the component in the main form's Design view or run the program.
If the above should be instantiated somehow, how can it be done?
Big thanks to anyone who can help.
|
|
|
|
|
I think, if you create your control for the first time, you must also send a reference of the main form to your component.
e.g.
public class MainForm
{
MyComponent myComp = new MyComponent(this);
}
public class MyComponent
{
private MainForm mainForm = null;
public MyComponent(MainForm mainForm)
{
this.mainForm = mainForm;
}
}
|
|
|
|
|
Thanks, that worked, but now the design view has some problems since I had to modify the constructor in my component and now it doesn't seem to match with something else declared in MyComponent.Designer.cs file. I'll try separating the process from the constructor..
|
|
|
|
|
Good day everyone,
Pls help if you're interested in
I'm doing a handwriting program.
I've got a static method to remove points of line. In this method there are 2 ArrayList. I'd like to remove whatever points they are matched each other. But I've got a problem:Index is out of range...
Anyone know how fix this or other way to compare 2 objects?
Thanks so much
private static void RemovePoint()
{
for (int i = 0; i < pointErase.Count; i ++)
{
for (int j = 0; j < pointArray.Count; j ++)
{
bool isMatch = Object.Equals(pointArray[i], pointErase[j]);
if (isMatch)
{
pointArray.RemoveAt(i);
pointErase.RemoveAt(j);
}
}
}
}
eric
|
|
|
|
|
bool isMatch = Object.Equals(pointArray[i], pointErase[j]);
this line is the wrong way round according to your loops, it should be
bool isMatch = Object.Equals(pointArray[j], pointErase[i]);
As are your lines that actually do the RemoveAt
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
-- modified at 7:12 Wednesday 24th May, 2006
|
|
|
|
|
Yup. You got your indexers mixed up.
V V
bool isMatch = Object.Equals(pointArray[i], pointErase[j]);
if (isMatch)
{
pointArray.RemoveAt(i); <-
pointErase.RemoveAt(j); <-
}
Just switch i for j and j for i and you should be good to go.
-Larantz-
|
|
|
|
|
If you use .NET 2.0 and switch to a List<T> , you can do all this in one line with List<T>::RemoveAll(...)
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
.NET 1.x allowed you to clear an ArrayList with .Clear and where did he say he wanted to remove all from the list?
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
RemoveAll does NOT clear the list. It removes all elements that match a certain predicate (delegate function). In this case, he would pass a predicate to the RemoveAll function that compares all elements against all elements in the other list.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Thanks everyone,
I got it. the capacity of pointArray may be greater than pointErase. So I just delete the erased points only.
eric
|
|
|
|
|
By the way, I still got a problem, I can't write the leftover points to a file.
It keeps writing all points in pointArray and pointErase ArrayList even though I removed erased points before hand.
Pls help if you have time
Thanks
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace Part_2_A2_ST2
{
public class Form1 : System.Windows.Forms.Form
{
private StreamReader sr;
private StreamWriter sw;
private Pen pen;
private Point pt1, pt2;
private bool isMouseDown;
private bool isMouseUp;
private Bitmap tempImage;
private Point pt;
private Graphics g;
private SolidBrush bgBrush;
private static ArrayList pointArray;
private static ArrayList pointErase;
private bool line = false;
private bool eraser = false;
private Point point;
private Graphics tempG;
private static Point ptCount;
private static Point ptErase;
private static string xDraw, yDraw;
private static string xErase, yErase;
private static string drawPoint, erasePoint;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton tbNew;
private System.Windows.Forms.ToolBarButton tbOpen;
private System.Windows.Forms.ToolBarButton tbSave;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton tbPointer;
private System.Windows.Forms.ToolBarButton Rectangle;
private System.Windows.Forms.ToolBarButton tbEllipse;
private System.Windows.Forms.ToolBarButton tbLine;
private System.Windows.Forms.ToolBarButton tbPolygon;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton tbEraser;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.ComponentModel.IContainer components;
public Form1()
{
pointArray = new ArrayList();
pointErase = new ArrayList();
point = new Point(0, 0);
InitializeComponent();
pen = new Pen(Color.Black, 2);
pt1 = new Point(50, 50);
pt2 = new Point(200, 200);
isMouseDown = false;
isMouseUp = false;
tempImage = new Bitmap( SystemInformation.PrimaryMonitorMaximizedWindowSize.Width,
SystemInformation.PrimaryMonitorMaximizedWindowSize.Height);
tempG = Graphics.FromImage(tempImage);
tempG.Clear(this.BackColor);
if (!File.Exists("DataPoints.txt"))
File.Create("DataPoints.txt");
pt = new Point(0,0);
g = this.CreateGraphics();
bgBrush = new SolidBrush(this.BackColor);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (!isMouseDown)
{
using (sr = new StreamReader("DataPoints.txt"))
{
string str = sr.ReadLine();
if (str == null)
return;
string [] strs = str.Split(' ');
if (strs.Length != 2)
return;
Point pt1 = new Point(int.Parse(strs[0]),int.Parse(strs[1]));
Graphics g = e.Graphics;
while ((str = sr.ReadLine()) != null)
{
strs = str.Split(' ');
if (strs.Length != 2)
break;
Point pt2 = new Point(int.Parse(strs[0]),int.Parse(strs[1]));
g.DrawEllipse(Pens.Blue, pt2.X-2, pt2.Y-2, 4, 4);
g.DrawLine(Pens.Black, pt1, pt2);
pt1 = pt2;
}
}
}
}
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (line)
{
isMouseDown = true;
pt1 = new Point(e.X, e.Y);
point = pt1;
sw = new StreamWriter("DataPoints.txt");
}
}
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (line)
{
if (isMouseDown)
{
pt2 = new Point(e.X, e.Y);
Graphics g = this.CreateGraphics();
g.DrawLine(pen, point, pt2);
tempG.DrawLine(pen, point, pt2);
point = pt2;
xDraw = (e.X).ToString();
yDraw = (e.Y).ToString();
drawPoint = xDraw + " " + yDraw;
AddPointDraw(drawPoint);
}
}
if (eraser)
{
if (isMouseDown)
{
line = false;
g.FillRectangle(bgBrush,e.X-10,e.Y-10,20,20);
g.DrawRectangle(Pens.Red,e.X-10,e.Y-10,20,20);
g.DrawRectangle(new Pen(this.BackColor),pt.X-10, pt.Y-10, 20, 20);
pt.X = e.X;
pt.Y = e.Y;
xErase = (e.X).ToString();
yErase = (e.Y).ToString();
erasePoint = xErase + " " + yErase;
AddPointErase(erasePoint);
}
}
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
eraser = false;
isMouseUp = true;
g.FillRectangle(bgBrush, e.X-10, e.Y-10, 30, 30);
if (line)
{
isMouseDown = false;
this.BackgroundImage = (Bitmap) tempImage.Clone();
sw.Close();
this.Refresh();
}
}
private void Form1_Resize(object sender,System.EventArgs e)
{
this.BackgroundImage = (Bitmap) tempImage.Clone();
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if ( e.Button == tbPolygon)
line = true;
else if (e.Button == tbEraser)
{
eraser = true;
isMouseDown = false;
}
else if (e.Button == tbSave)
Save();
}
public void AddPointDraw(String drawPoint)
{
pointArray.Add(drawPoint);
}
public void AddPointErase(String erasePoint)
{
pointErase.Add(erasePoint);
}
private static void RemovePoint()
{
for (int i = 0; i < pointErase.Count; i ++)
{
for (int j = 0; j < pointArray.Count; j ++)
{
bool isMatch = Object.Equals(pointArray[j], pointErase[i]);
if (isMatch)
{
pointArray.RemoveAt(j);
pointErase.RemoveAt(i);
}
}
}
}
private void Save()
{
RemovePoint();
sw.WriteLine("pointArray...........");
foreach (object s in pointArray)
{
sw.WriteLine(s);
}
sw.WriteLine("pointErase................");
foreach (object s in pointErase)
{
sw.WriteLine(s);
}
sw.WriteLine("Stop--------------------------");
sw.Close();
}
}
}
eric
|
|
|
|
|
hi all,
first: i have 2 forms. first is a login form and the other is my main form.
how can i show thelogineduser and logintime on second form ? (how do i get logintime)
second: is possible to subtract two dates and get the number of days between them ? (exp: 10.08.2010 - 09.09.2006 = ?) or do i have to calculate some extra code ?
thanks in advance,
bye.
|
|
|
|
|
1) Not enough information to answer your question!
2) Subtracting two DateTime instances gives a TimeSpan instance. A TimeSpan instance has a property named TotalDays
Current blacklist
svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour
|
|
|
|
|
you can send the logintime to second
form by
form2 frm2 = new form2(logintime);
frm2.show();
you need to modify the costructor of form2 to accept the parameter.
to subtract one date from another you have to store the result in
timespan type of variable
DateTime date1=Convert.ToDateTime("20-12-1978");
DateTime date2=Convert.ToDateTime("20-12-1979");
TimeSpan date3= date2.Subtract(date1);
int days = date3.Days;
rahul
|
|
|
|
|
I searched in MSDN but not found. Please help me how to gets WINDOWS directory like "%Systemroot%" in Windows for Programming!!
Thank you for your support!
|
|
|
|
|
Environment.GetEnvironmentVariable("SYSTEMROOT") points to the Windows directory
Environment.GetFolderPath(SpecialFolder.System) includes the "system32" directory
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
my project have three form, one mdi form and two form children, from one button on form children (form1), how i can call event menuitem_click to call form children (form2). Note, i design menu on mdi form to call form children by menuitem. Help me
Thanks
|
|
|
|
|
//in any event of form2
Form1 f1 = new Form1();<br />
f1.MenuItem_Click(sender,e);
|
|
|
|
|
hi
my dataset contain 2 related tables and i want user click on datagrid cell in datagrid1 the table2's information show in datagrid2 (it is true work ).but i click the other cells of datagrid1 the dataset
don't clear and show related information of previous row . when i clear dataset.tables["table2"].clear() the follwing error show me :
Object reference not set to an instance of an object
how to solve my problem ??(i want to show only information of current row)
|
|
|
|
|
According to me, 2 DataTable objects are related, so you can not remove all rows of table2. Because table1's data is referencing to data of table2.
NPS.
|
|
|
|
|
Encouraged by the success of my last thread, here's another:
If I show() a MessageBox in my MDI app, it's hidden somewhere behind the main window and completely locks the application. I have to quit using the task manager.
The same goes for system-generated MessageBoxes, like the "Overwrite?" dialog that can pop up during a SaveFileDialog operation.
Any ideas?
Overall, I have so many problems with MDI (redrawing, keyboard input and more) that I'm considering faking the whole MDI paradigm myself instead...
|
|
|
|
|
Try to give to show() function child and main window handles.
Best regards, Alexey.
|
|
|
|
|
MessageBox.Show() only has overloads that accept one handle (actually a IWin32Window), and I tried passing the MDI form - same problem...
Or did you mean some other way?
|
|
|
|
|
Try to pass active child form handle.
Best regards, Alexey.
|
|
|
|