By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,598 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

Problem–displaying graphform fm another form buttonclick event wo losing data

P: 2
Problem – displaying a graphics form from another form’s button click event without losing the XY data plot.

If the Startup form is the GraphWindow form the Graphlib.vb calls create a XY data plot.
Code outline (not everything included):
(Sub GraphWindow_Paint(ByVal sender As Object, ByVal e As Sys…PaintEventArgs) _ Handles Me.Paint)
Call Graphscale(xmin, xmax, ymin, ymax)
Call DrawAxis(10,10)
XY data
Call GraphPlot
End Sub
End Class

If the Startup form is changed to PrintData form with a Test button which calls the GraphWindow form, the Graphlib.vb calls do not create a XY data plot. They are called because certain graph subroutines variables are dumped to a file.

PrintData Form Test button code:
Me.Hide( )
Dim frmGraphWindow1 As New GraphWindow
frmGraphWindow1.ShowDialog( ) ‘also tried Show
Apr 1 '10 #1
Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
Bad: Directly accessing controls of one class/form from another.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;


Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return button1.Checked; }
  5.    set { button1.Checked = value; }
  6. }
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Expand|Select|Wrap|Line Numbers
  1. Form2
  2. public bool IsOn
  3. {
  4.    get { return btnMeaningfulName.Checked; }
  5.    set {
  6.             btnMeaningfulName.Checked = value;
  7.             panelDashboard.Visible = value;
  8.             labelStatus = value == true ? "On" : "Off";
  9.             btnRunNow.Enabled = value;
  10. }
Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.
Apr 1 '10 #2

P: 2
Tryed suggestion with new project, Form1 & Form2 - worked ok with IsOn.
Form1 button_click code: Me.Hide(),Dim Form2req As New Form2,
Form2req.Show
Tryed IsOn code with orginal problem code. Had the same problem.
My problem seems to be when a forms btn_click calls a Graphics form.
(Sub Graphwindow.Paint( ..,e As Sys.Win.Forms.PainteventArgs)
Apr 5 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.