473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Changing properties of form controls

Hi,

I am trying to write a simple application to retrieve data from the
Windows registry and insert it into textboxs on a windows form.

So far I have one namespace containing two classess.

The first class handles the form generation - (this was done using GUI
form designer).

The second class handles the retrieval of the data from the registry.

What I want to be able to do is enter the retrieved data into the form
textboxs. I have tried creating a method to change the value of the
textbox text property in the first class and calling this from the
second class. The code runs but doesn't show the updated text value
although the code is executing as a console writeline in the same
method executes ok.

I'm relatively new to C# but do have some college experience of C++.
Any help would be most appreciated. Any comment on the basic
application structure would also be appreciated.

TIA

Tony Wilkinson
Nov 16 '05 #1
4 2419
Tony W wrote:
Hi,

I am trying to write a simple application to retrieve data from the
Windows registry and insert it into textboxs on a windows form.

So far I have one namespace containing two classess.

The first class handles the form generation - (this was done using GUI
form designer).

The second class handles the retrieval of the data from the registry.

What I want to be able to do is enter the retrieved data into the form
textboxs. I have tried creating a method to change the value of the
textbox text property in the first class and calling this from the
second class. The code runs but doesn't show the updated text value
although the code is executing as a console writeline in the same
method executes ok.

I'm relatively new to C# but do have some college experience of C++.
Any help would be most appreciated. Any comment on the basic
application structure would also be appreciated.

TIA

Tony Wilkinson

Sounds like bad design to me.
Maybe you should have a class that handles getting the keys from the reg
and writing changes back and another (presumably winform) to display and
edit.
In this case, your main class (form) would create an instance of your
reg class and use its methods to retrieve the relevant keys and would
then populate its fields from there.

If you dont want to change...
In your second class, how are you accessing the form.
If you are doing it by creating a new instance, then bear in mind that
this is not the same instance that is displayed.

If you create a singleton form, your method will work as you will always
be accessing 1 instance.

Post back with more details if you need.

JB
Thunderbird and loving it.
Nov 16 '05 #2
Hi,

Thanks for replying to my post. I'm still unsure where I am going wrong so have taken the liberty of posting an edited version of my code below. I
have removed code relating to other controls and registry entries as well as comments etc.

As you can see I have a class called FormUserInfo that handles form creation and changes. I have another class for getting registry data and
manipulating it prior to displaying via the form form the first class.

The settboAppVer method appears to work as the passed string value displays correctly prior to the control property change and the control text value
reflects the new value after changing the value.

The only problem I have is showing the changes on screen.

Once again any advice will be most welcome, I've searched the web and can't find an answer to this anywhere.

Thanks again,

Tony W

// code starts here~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Text;

namespace AVMonitor
{
public class FormUserInfo : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tboAppVer;

public FormUserInfo()
{
InitializeComponent();
}

private void InitializeComponent() {
this.tboAppVer = new System.Windows.Forms.TextBox();
this.SuspendLayout();

// tboAppVer
this.tboAppVer.BackColor = System.Drawing.SystemColors.Control;
this.tboAppVer.Location = new System.Drawing.Point(128, 184);
this.tboAppVer.Name = "tboAppVer";
this.tboAppVer.ReadOnly = true;
this.tboAppVer.TabIndex = 12;
this.tboAppVer.Text = "textBox1";

// FormUserInfo
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 302);
this.Controls.Add(this.tboAppVer);
this.Name = "FormUserInfo";
this.Text = "AVM";
this.ResumeLayout(false);
}

public void settboAppVer(string strAppVer)
{
MessageBox.Show (strAppVer); //displays correct value as passed to method
tboCurrentDatVer.Text = strAppVer;
MessageBox.Show(tboAppVer.Text); //displays correct value as passed to method
}

}

public class AVDetails
{

//? ATTRIBUTES
string strAppVersion;

//?METHODS
public string getAppVersion()
{
//? get the Application version
AVDetails AVD = new AVDetails();
strAppVersion = AVD.getRegistryEntry("<Registry path>", "Version").ToString();

return strAppVersion;
}

public object getRegistryEntry(string strKeyName, string strKeyElement)
{
//? read the registry
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey(strKeyName);
Object strKeyValue = pRegKey.GetValue(strKeyElement);

return strKeyValue;
}

public static void Main()
{
string strAppVer;

AVMonitor.AVDetails AVD = new AVDetails();
AVMonitor.FormUserInfo FUI = new FormUserInfo();

//? get the AppVer
strAppVer = AVD.getAppVer();

//? apply values to form
FUI. settboAppVer (strAppVer);
Application.Run(new FormUserInfo());

}
}
}

Nov 16 '05 #3
Hi Tony
Firstly, if you change
Application.Run(new FormUserInfo());
to
Application.Run(FUI);
your program should work.
This is because you are creating an instance of FormUserInfo and setting
the data on it but then showing a new instance that has not had the data
set.

BUT..
You have not got a very good design there.
I would suggest that you move your entry point into FormUserInfo and
then in the load event of your form, create an instance of your
avdetails class and use the methods to load itself.

PSEUDOCODE

public class frmMain : sys.win.forms.form
{
...........
this.load =+ new eventhandler(this.load);

private void load(obj sender, eventargs e)
{
AVDETAILS avd = new AVDETAILS();
this.txtAppVersion.text = avd.getAppVersion();
}
}

Do you know how to use events?
It is better for a form / class to use other classes to load itself than
to rely on them to load it. This allows for easier reuse of the class.

Please see my inline comments.
HTH
JB
Im a Starship Trooper
This is my letter to dad
Transferred from Saigon to Baghdad
And now Im dead --Ozi Batla, Starship Trooper

Tony W wrote:
Hi,

Thanks for replying to my post. I'm still unsure where I am going wrong so have taken the liberty of posting an edited version of my code below. I
have removed code relating to other controls and registry entries as well as comments etc.

As you can see I have a class called FormUserInfo that handles form creation and changes. I have another class for getting registry data and
manipulating it prior to displaying via the form form the first class.

The settboAppVer method appears to work as the passed string value displays correctly prior to the control property change and the control text value
reflects the new value after changing the value.

The only problem I have is showing the changes on screen.

Once again any advice will be most welcome, I've searched the web and can't find an answer to this anywhere.

Thanks again,

Tony W

// code starts here~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Text;

namespace AVMonitor
{
public class FormUserInfo : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tboAppVer;

public FormUserInfo()
{
InitializeComponent();
}

private void InitializeComponent() {
this.tboAppVer = new System.Windows.Forms.TextBox();
this.SuspendLayout();

// tboAppVer
this.tboAppVer.BackColor = System.Drawing.SystemColors.Control;
this.tboAppVer.Location = new System.Drawing.Point(128, 184);
this.tboAppVer.Name = "tboAppVer";
this.tboAppVer.ReadOnly = true;
this.tboAppVer.TabIndex = 12;
this.tboAppVer.Text = "textBox1";

// FormUserInfo
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(528, 302);
this.Controls.Add(this.tboAppVer);
this.Name = "FormUserInfo";
this.Text = "AVM";
this.ResumeLayout(false);
}

public void settboAppVer(string strAppVer)
{
MessageBox.Show (strAppVer); //displays correct value as passed to method
tboCurrentDatVer.Text = strAppVer;
MessageBox.Show(tboAppVer.Text); //displays correct value as passed to method
} Should not need to expose public method like this.
}

public class AVDetails
{

//? ATTRIBUTES Not attributes but fields or private / member variables.

Fields are public varaibles
Private variables are private and not accessible through a property.
Member variables are private but are accessible through property accessors.
string strAppVersion; What is this for?
is it public / private, do you want outside classes to be able to 'see'
this?

You can wrap this in a region such as

#region Member Variables
private int m_MyID;
#endregion

#region Properties
public int MyID
{
get
{
return m_MyID;
}
set
{
m_MyID = value;
}
#endregion

//?METHODS
public string getAppVersion()
{
//? get the Application version
AVDetails AVD = new AVDetails(); You do not need to create an instance here as you already have an
instance (this).
strAppVersion = this.getRegistryEntry("<Registry path>",
"Version").ToString();
or
strAppVersion = getRegistryEntry("<Registry path>", "Version").ToString(); strAppVersion = AVD.getRegistryEntry("<Registry path>", "Version").ToString();

return strAppVersion; }

public object getRegistryEntry(string strKeyName, string strKeyElement)
{
//? read the registry
RegistryKey pRegKey = Registry.LocalMachine;
pRegKey = pRegKey.OpenSubKey(strKeyName);
Object strKeyValue = pRegKey.GetValue(strKeyElement);

return strKeyValue;
}

public static void Main()
{
string strAppVer;

AVMonitor.AVDetails AVD = new AVDetails();
AVMonitor.FormUserInfo FUI = new FormUserInfo();

//? get the AppVer
strAppVer = AVD.getAppVer();

//? apply values to form
FUI. settboAppVer (strAppVer);
Application.Run(new FormUserInfo()); You are creating a new instance that has no relation to FUI, the one you
have previously loaded.
}
}
}

Nov 16 '05 #4
Hi,

Thanks for the input. I have tried the easy suggestion and that has worked for now.

I have a background in VBA and VB so know about re-usable code but not to the same strict rules as C whatever. Different names for roughly the same
things doesn't help either or the same name for completely different things.

I dived into C# as I needed to create a simple app that would run as a service and this is almost impossible to do in VB6.
I have been using some online tutorials to try and get what I needed to sort out my very simple service. I guess it shows.

Do you know of any GOOD online tutorials that go further than single class examples without forms etc that I could use? If not how about a GOOD book
recommendation?
After I've bodged my way through this I'll go back to basics and start learning over again.

Thanks again,

Tony W
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: deko | last post by:
I'm trying to find a way to set form/control properties programmatically. In a nut shell: 1. Open a database 2. Open a form in design view 3. Do something like this: For Each prp In...
2
by: ColinWard | last post by:
Hi. I have a form which has as its recordsource an SQL string. The SQL String is as follows: SELECT * from CONTACTS where false. this ensures that there is no data loaded in the form when the...
8
by: deko | last post by:
I'm hoping someone can sanity check my understanding of the Object Model for Forms/Controls. I'm having trouble drilling down into Control properties. First, I have a record set with the...
3
by: Gerry Abbott | last post by:
Hi all, Im wondering why the following will work Me.Controls("subnonstorePos").visible = False while Me.Controls("subnonstorePos").AllowEdits = False give me an 'property not support by...
3
by: qwerty | last post by:
I´m new to ASP.Net. My workmate has some experience with it. He claimed that in ASP.Net working with frames is much simpler than it was ASP. I asked explanation but he couldn't give me such. (a...
1
by: Firewalker | last post by:
I am attempting to change the backColor property on the previously instantiated buttons FROM a listbox_doubleClick event. I thought it would be something like this: If...
7
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and...
32
by: deko | last post by:
I have a popup form with a textbox that is bound to a memo field. I've been warned about memo fields so I'm wondering if I should use this code. Is there any risk with changing the form's...
3
by: Dan Soendergaard | last post by:
I have this code: for (int i = 0; i < this.Controls.Count; i++) { if (this.Controls is ValidationTextBox) { // Change the property of the control here. } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.