PDA

View Full Version : c# class communication help required :)



Mike
29-03-2010, 07:46 AM
I should know how to do this, but can't figure it out :)

I have a project with 2 classes - one is a form (Class_form) and one is a textbox class (Class_tbox) that creates a numeric only textbox.

I want Class_tbox to get a value from an input (a textbox) in Class_form. I thought I should be able to do this using the Set/Get, but it's not working... (I'm at work so have no code to post as example at this stage... if required I'll post some tonight if I can).

Anyone know how to get a value from my form class and use it in my tbox class?

Thanks,
Mike.

psycik
29-03-2010, 09:10 AM
Yes you should be able to do it with Get/Set Properties.

Is your class_form creating a new local instance of Class_tbox? or are both the class_tbox and class_form static classes?

If your form is using an instance of tbox and the value field is a public get/set property then it should be visible and usable.

Mike
29-03-2010, 07:06 PM
Is your class_form creating a new local instance of Class_tbox?Yes it was... but not working... could it be because the class defines a new type of textbox?

I'll try to post some sample code plus the class.

Mike.

Mike
29-03-2010, 07:16 PM
OK this is my class. It creates a new type of textbox that I can add to a form that accepts only numeric input (not my code, found it on the net...)

What I am trying to do, is get the entire value from the numerictextbox on my form and do something with it within this class (is that possible?) to find if decimal points exist (was trying to use a textbox.contains() or whatever) but I can't figure out how to pass the textbox.text value through to the other class.

Or am I doing this all wrong?


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace tester123
{
public class NumericTextBox : TextBox
{
bool allowSpace = false;

// Restricts the entry of characters to digits (including hex), the negative sign,
// the decimal point, and editing keystrokes (backspace).
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);

NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.Nu mberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;

string keyInput = e.KeyChar.ToString();

if (Char.IsDigit(e.KeyChar))
{
// Digits are OK
}
else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
keyInput.Equals(negativeSign))
{
// Decimal separator is OK
e.Handled = true;
}
else if (e.KeyChar == '\b')
{
// Backspace key is OK
}
// else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
// {
// // Let the edit control handle control and alt key combinations
// }
else if (this.allowSpace && e.KeyChar == ' ')
{

}
else
{
// Consume this invalid key and beep
e.Handled = true;
// MessageBeep();
}
}

public int IntValue
{
get
{
return Int32.Parse(this.Text);
}
}

public decimal DecimalValue
{
get
{
return Decimal.Parse(this.Text);
}
}

public bool AllowSpace
{
set
{
this.allowSpace = value;
}

get
{
return this.allowSpace;
}
}
}
}Mike.

somebody
29-03-2010, 07:52 PM
Or am I doing this all wrong?
Mike.

I think you might be misunderstanding the relationship between classes and objects.

If I understand you correctly, you have a form (let's call it FormA) which contains some textboxes and other controls, one of which is a NumericTextBox.

If this is the case, then the NumericTextBox class doesn't need to "get" a value from the textbox on the form, since the textbox on the form _is_ an instance of the NumericTextBox class.

As NumericTextBox inherits the TextBox class, you can access the .Text property to get whatever value is entered into the textbox. For example, if you wanted to add a new method to the NumericTextBox class called checkIsMike(), you could do the following:



public boolean checkIsMike(){
if (this.Text.equals("Mike"))
return true;

return false;

}


See how you don't have to care about the FormA class, since the value the user entered is actually stored in the text box object.


If within the form, you want to call the checkIsMike() method (say, in response to a button being clicked), you can just call it on the NumericTextBox object (let's say it's called "numericTextBox1")



public void onButtonClick(...something....)
{
if (numericTextBox1.checkIsMike())
MessageBox.Show("blah blah");
}



When you call that method on the numeric text box object, it will run the code you defined in the NumericTextBox class.

Mike
29-03-2010, 08:16 PM
somebody you're a genius :D I can see I've still got a LOT to learn :)

Thanks heaps! I've now got the numerictextbox only accepting one decimal place :) Brilliant.

Cheers,
Mike.