Reputation: 249
I have a simple program I have to make here but I'm not quite sure how to start it. I have my class file already defined as shown below. What I need to do is to make a Windows Form Application (as shown below in the picture.) I need to enter the information into the textboxes and then output it into the richtextbox1 at the bottom. And I don't have it shown below, but in the Windows Form, how do you call a method if I had it in this class file?
class Telephone
{
private string manufacturer;
private string model;
private bool isConnected = false;
private string lastNumberDialed;
private string phoneNumber;
public Telephone()
{
}
public Telephone(string manufacturer, string model, string phoneNumber)
{
Manufacturer = manufacturer;
Model = model;
PhoneNumber = phoneNumber;
}
public string Manufacturer
{
get { return manufacturer; }
set { manufacturer = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
public string PhoneNumber
{
get { return phoneNumber; }
set { phoneNumber = value; }
}
new public string ToString()
{
return "Manufacturer: " + manufacturer;
}
}
And here's the design code:
public partial class TelephoneEntryForm : Form
{
public TelephoneEntryForm()
{
InitializeComponent();
}
private void btnEnter_Click(object sender, EventArgs e)
{
}
private void btnShowPhones_Click(object sender, EventArgs e)
{
}
private void btnDial_Click(object sender, EventArgs e)
{
}
private void btnRedial_Click(object sender, EventArgs e)
{
}
private void btnHangUp_Click(object sender, EventArgs e)
{
}
}
Upvotes: 1
Views: 1597
Reputation: 17176
Well, if I understand You correctly, You already have button Enter click event handler - btnEnter_Click
method... That means that all code, which is written in this method, will execute when You click
Enter button.
private void btnEnter_Click(object sender, EventArgs e)
{
// Read tha values of textboxes (not necessarily but more readable)
string manufacturer = manufacturerTextBox.Text;
string model = modelTextBox.Text;
string phoneNumber = phoneNumberTextBox.Text;
// Create new phone instance, based on entered data
var telephone = new Telephone(manufacturer, model, phoneNumber);
// Enter this data to reachtextbox
richtextbox1.Text += string.Format("{0}\n", telephone);
// Clear text boxes
manufacturerTextBox.Text = modelTextBox.Text = phoneNumberTextBox.Text = string.empty;
}
but the thing is that during such scenario You will lose all entered data, because your ToString()
method outputs only Manufacturer
... You should modify this method to output full telephone info... Something like:
class Telephone
{
...
public override string ToString()
{
return string.Format("Manufacturer: {0}; Model: {1}; Phone number: {2}", Manufacturer, Model, PhoneNumber)
}
...
}
or save this data in any type of file.
Upvotes: 1
Reputation: 66449
I don't really understand why you're asking someone to finish this, since you've got the entire thing wired up to go... unless someone provided you with the code you posted.
If you want to store your textbox data in a separate class before displaying it, instantiate it in your code-behind:
public partial class TelephoneEntryForm : Form
{
private Telephone telephone;
public TelephoneEntryForm()
{
InitializeComponent();
telephone = new Telephone();
}
Then I'd probably subscribe to the KeyDown
events for all those text fields, and save the values of each TextBox
in their respective properties in your Telephone instance. Then in btnShowPhones_Click
, use the values in the Telephone instance for display in the RichTextBox
.
This should give you enough information to make a go of it. Let us know if you get stuck, and exactly where you're stuck, so someone can help you.
Upvotes: 2
Reputation: 12328
Are you asking how to hook a button up to call your code? If that's the case, double-click on the button to create the event handler method. Within that method, add calls to whatever code you want.
Upvotes: 0