-
June 16th, 2005, 10:00 AM
#1
C# / .NET Equivalent to CWinApp / CFormView Application using Forms
Hi ...
Working on a new C# .NET Application ...
I have written many applications using Visual C++ 6.0 MFC using CWinApp derived classes and CFormView. Typicalled I would load configuration and framework stuff (logging, metrics, alerts ...) in the CApp Class. Then when I needed to access one of these objects from another object I would use AfxGetApp() to get access to objects in the CApp Class.
Also ... with regards to the gui I would use a CFormView derived class and then I was always able to get a pointer to this class to access the controls on the CFormView derived class.
Sample Code. The code below will demonstate how to access between the foundation classes.
Code:
// Get a pointer to the view
CMainFrame *pMainFrame = (CMainFrame *)AfxGetMainWnd();
CFrameWnd* pChild = pMainFrame->GetActiveFrame();
CTestView* pView = (CTestView*)pChild->GetActiveView();
// Get a pointer to the app
CTestApp *pApp = (CTestApp *)AfxGetApp();
// Write to the application status, a listbox on the view
pView->WriteLog("Inside destrctor for CVideo");
// Create a pointer to the Metric object and set the logging
// object inside.
m_pMetrics = new CMetrics();
m_pMetrics->SetLog(pApp->GetLog());
Using C# .NET
I have created a Windows Application with a Form. I have placed a ListBox on the Form and would like to create a method that I can call from other objects that will be used to write to the ListBox on the Form.
Also ... Where would be a good place to load configuration data (registry settings ext. ) and then access this data. Would you create a singleton object and then access this ?? What is the stadard / recommended approach with this ...
Thanks,
Chris
Sample Code
This code was my attempt to write a message to the ListBox contained in the Form Class from the Video Class. This doe not work ... but builds with no errors.
Code:
// This is the Form Class with the ListBox
namespace Test
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox ListBoxStatus;
}
public void WriteStatusMessage(String strMessage)
{
this.ListBoxStatus.Items.Add(strMessage);
}
}
// The Video Class will try to write a status message
namespace Test
{
public class Video
{
public Video()
{
// Write a message when the Video object
// is created
String strMessage = "Inside the Video Constructor";
Form1 NewForm = new Form1();
NewForm.WriteStatusMessage(strMessage);
}
public int nColor;
public int nHue;
public string strComment;
}
}
-
June 16th, 2005, 10:55 AM
#2
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
Originally Posted by macgowan
Also ... Where would be a good place to load configuration data (registry settings ext. ) and then access this data. Would you create a singleton object and then access this ?? What is the stadard / recommended approach with this ...
I do it like this, I have not seen any standard for it, but it seems like a reasonable way to do it.
I guess config files are common as well, though I have not spent much time figuring out how to use them.
Originally Posted by macgowan
Sample Code
This code was my attempt to write a message to the ListBox contained in the Form Class from the Video Class. This doe not work ... but builds with no errors.
It sure looks ok to me (I have no compiler here so I can't try it), maybe you need to refresh the ListBox?
-
June 16th, 2005, 03:22 PM
#3
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
First, the configuration issues: You can use the registry (great API for it in Microsoft.Win32.Registry class - look in MSDN).
But, the best way I think is using .NET configuration files. Add a new item to your project - Application Configuration File. A new file (App.Config) will be added. You can see in MSDN the structure of this Xml file - very easy.
for example you can use this:
Code:
<configuration>
<appSettings>
<add key="Path" value="C:\temp" />
</appSettings>
</configuration>
and use this code to get the value:
Code:
string path = ConfigurationSettings.AppSettings["Path"];
No easier way to do it. You can define your own sections, and configure arrays of values, key-value and other stuff.
-
June 16th, 2005, 03:35 PM
#4
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
As for the first question (the ListBox), well you can pass the Items property of the ListBox as parameter to methods, and change it in other objects too.
The changes will be reflected on the form.
-
June 16th, 2005, 04:42 PM
#5
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
Originally Posted by jhammer
But, the best way I think is using .NET configuration files.
Now I remember, I looked at that but I could not figure out an easy way to update the configuration file (of course I could read the XML and change it). Neither could I figure out where to store the user specific configuration. But there must be some way to do it. (I just sticked to the registry... but since I access it through my own singleton I can change it later.... config files seems to be the .Net way of managing configuration.)
Last edited by klintan; June 16th, 2005 at 04:44 PM.
-
June 17th, 2005, 07:23 AM
#6
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
There is a great free library called Nini which has common interfaces for ini, xml, registry, command-line arguments and .NET configuration file.
The point is that you can also update the configuration files and not just read them.
the link is here:
http://nini.sourceforge.net/
-
June 17th, 2005, 01:27 PM
#7
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
Hi ...
Thanks for the feed back on the Configurations stuff. With regards to the ListBox in the Form1 Class. I have a public method in the Form1 Class called WriteStatusMessage(). This method will format the string and add the date and time and in the future could write to a file.
I would like to be able to call this method from other classes. In the sample code below I an able to create an instance of the Form1 Class and then call the WriteStatusMessage() method. Stepping through the code I actaully get into the method and to the point where I do the Add() on the ListBox.Item Object.
Any idea why the message will not display on the ListBox. I have also uncommented the
Sample Code below
Form1 Class
Some code is removed fro clarity
The code below contains the ListBox and the method to write to the ListBox. The Video Class will call the method, does not produce text on the list control .
Thanks,
Chris
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestBlueRayon
{
public class Form1 : System.Windows.Forms.Form
{
private void menuItemVideo_Click(object sender, System.EventArgs e)
{
//string strTemp;
string strMessage;
// Tell the user what is happening
strMessage = "Create Video Object";
WriteStatusMessage(strMessage);
// Create the Video object
Video GreenVideo = new Video();
// Change the contrast
// This call will try to write to the ListBox
GreenVideo.SetVideoContrast();
}
public void WriteStatusMessage(string strMessage)
{
string strDisplayMessage;
DateTime dtCurrent = DateTime.Now;
int nYear = dtCurrent.Year;
int nMonth = dtCurrent.Month;
int nDay = dtCurrent.Day;
int nHour = dtCurrent.Hour;
int nMinute = dtCurrent.Minute;
int nSecond = dtCurrent.Second;
int nMillisecond = dtCurrent.Millisecond;
string strTemp;
strDisplayMessage = String.Format("{0,0:D4}.{1,0:D2}.{2,0:D2} {3,0:D2}:{4,0:D2}:{5,0:D2}.{6,0:D3} {7}",
nYear,
nMonth,
nDay,
nHour,
nMinute,
nSecond,
nMillisecond,
strMessage);
// Shutdown the painting of the ListBox as items are added.
this.ListBoxStatus.BeginUpdate();
this.ListBoxStatus.Items.Add(strDisplayMessage);
this.ListBoxStatus.EndUpdate();
}
This is the Video Class.
From this class we will call a method that will write to the ListBox using the
WriteStatusMessage() method.
Code:
namespace TestBlueRayon
{
public class Video
{
public Video()
{
// This will not write to the ListBox ??
Form1 NewForm = new Form1();
NewForm.WriteStatusMessage("Hello");
}
// Public attributes
public int nColor;
public int nHue;
public string strComment;
public void SetVideoContrast(int nIndex)
{
// This will NOT write to the ListBox
Form1 NewForm = new Form1();
NewForm.WriteStatusMessage("Hello");
nColor = 12;
nHue = 34;
strComment = "Contrast has been set";
}
}
}
-
June 18th, 2005, 01:11 PM
#8
for configuration :
take a look at :
Configuration Application Blockfrom Microsoft's Enterprise Library..
-
June 22nd, 2005, 12:05 PM
#9
Re: C# / .NET Equivalent to CWinApp / CFormView Application using Forms
The following is the solution to the ListBox Issue
Thanks Andy Tacker (CodeGuru)
Note that we added a reference to the form in the Video Class (m_ParentForm1). Then when we create the Video Object in the Form1 Object we will set the Video.m_ParentForm1 attribute to be <this> to set the reference back to the Form1 Object. Then when we want to use the Form1::WriteStatusMessage() method we can used the reference to Form1 from m_ParentForm1.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace TestBlueRayon
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MenuItem menuItemVideo;
private System.Windows.Forms.ListBox ListBoxStatus;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
WriteStatusMessage("TestBlueRayon Application Started.");
WriteStatusMessage("Good to have you with us. ");
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
// took out code
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void menuItemVideo_Click(object sender, System.EventArgs e)
{
//string strTemp;
string strMessage;
// Tell the user what is happening
strMessage = "Create Video Object";
WriteStatusMessage(strMessage);
// Create the Video object
Video GreenVideo = new Video();
// Set the parent object
GreenVideo.m_ParentForm1 = this;
// Set video contrast
GreenVideo.SetVideoContrast(0);
}
public void WriteStatusMessage(string strMessage)
{
// You have pressed ???
string strDisplayMessage;
DateTime dtCurrent = DateTime.Now;
int nYear = dtCurrent.Year;
int nMonth = dtCurrent.Month;
int nDay = dtCurrent.Day;
int nHour = dtCurrent.Hour;
int nMinute = dtCurrent.Minute;
int nSecond = dtCurrent.Second;
int nMillisecond = dtCurrent.Millisecond;
string strTemp;
strDisplayMessage = String.Format("{0,0:D4}.{1,0:D2}.{2,0:D2} {3,0:D2}:{4,0:D2}:{5,0:D2}.{6,0:D3} {7}",
nYear,
nMonth,
nDay,
nHour,
nMinute,
nSecond,
nMillisecond,
strMessage);
// Shutdown the painting of the ListBox as items are added.
// this.ListBoxStatus.BeginUpdate();
this.ListBoxStatus.Items.Add(strDisplayMessage);
// this.ListBoxStatus.EndUpdate();
// this.ListBoxStatus.Invalidate();
this.ListBoxStatus.Update();
}
}
}
using System;
namespace TestBlueRayon
{
public class Video
{
// Public attributes
public int nColor;
public int nHue;
public string strComment;
public Form1 m_ParentForm1;
// Default constructor
public Video()
{
nColor = 0;
nHue = 0;
m_ParentForm1 = null;
}
public void SetVideoContrast(int nIndex)
{
// Set some values for the color and hue
nColor = 12;
nHue = 34;
strComment = "Contrast has been set";
if (m_ParentForm1 != null)
{
m_ParentForm1.WriteStatusMessage("SetVideoContrast() called");
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|