How to implement and call a custom method?  
Author Message
Karl Kaiser





PostPosted: Windows Forms General, How to implement and call a custom method? Top

Hi there,

I would like to create a subclass from textBox with a method that changes the font size based upon the length of the text, but I do not know how to call the method. First, here is the object and method definition:

public class Cell : TextBox {
public Cell() {
Multiline = true;
// ...etc.
}
// Here is the Formatting Method:
private void Format(object sender) {
if (sender.Text.Length <= 1) {
sender.Font = new Font(sender.Font.FontFamily.Name, 32); }
else { sender.Font = new Font(sender.Font.FontFamily.Name, 14); }
}
}

When this compiles, it errors out with the message " 'object' does not contain a definition for text." (Also, shouldn't I be able to use "This." instead of an object parameter ")

Then, elsewhere I add controls of this class type to a panel and try to call the format method:

for (int i = 0; i <= 8; i++) {
panel.Controls.Add(new Cell());
// .....
panel.Controls(i).Format(panel.Controls(i));


...the "Cells" are added but the attempt to call the Format method compiles with the error:

'System.Windows.Forms.Control' does not contain a definition for 'Format'


Windows Forms6  
 
 
Mubshir Raza Ali





PostPosted: Windows Forms General, How to implement and call a custom method? Top

Hi,

In custome text box control just register text change event in controls constructor.


For example ...


using
System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

namespace TextChange

{

public partial class UserControl1 :TextBox

{

public UserControl1()

{

InitializeComponent();

this.TextChanged += new EventHandler(UserControl1_TextChanged);

}

void UserControl1_TextChanged(object sender, EventArgs e)

{

MessageBox.Show("TC");

}

}

}

Hope this help.



 
 
Karthik Krishnaswami





PostPosted: Windows Forms General, How to implement and call a custom method? Top

private void Format(object sender) {
if (sender.Text.Length <= 1) {
sender.Font = new Font(sender.Font.FontFamily.Name, 32); }
else { sender.Font = new Font(sender.Font.FontFamily.Name, 14); }
}
}

I think you need to cast the sender object to a TextBox.

so say,

private void Format(object sender) {

TextBox tb = sender as TextBox;

if(tb == null)

return;
if (tb.Text.Length <= 1) {
tb.Font = new Font(tb.Font.FontFamily.Name, 32); }
else { tb.Font = new Font(tb.Font.FontFamily.Name, 14); }
}
}


 
 
James Curran





PostPosted: Windows Forms General, How to implement and call a custom method? Top

Well, this is easy:

for (int i = 0; i <= 8; i++)
{
Cell cell = new Cell();
panel.Controls.Add(cell);
// .....
cell.Format(cell);

But, let's assume that the first half and the second half of that aren't in the same loop:


for (int i = 0; i <= 8; i++)
{
Cell cell = new Cell();
panel.Controls.Add(cell);
}
///
foreach(Cell cell in panel.Controls)
{
if (cell != null) // non-Cell control in panel will be null
{
cell.Format(cell);
}
}