KEMBAR78
Creating a Name seperator Custom Control using C# | PDF
Name.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace custctrl
{
public partial class Name :TextBox
{
public Name()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
private String _FirstName;
private String _LastName;
public String GetFirstName()
{
UpdateNames();
return _FirstName;
}
public String GetLastName()
{
UpdateNames();
return _LastName;
}
private void UpdateNames()
{
int commapos;
int spacepos;
string name;
name = Text;
commapos =Text.IndexOf(",");
spacepos =Text.IndexOf("");
string[] flname;
if(commapos!=-1)
{
flname = Text.Split(',');
_FirstName = flname[0];
_LastName = flname[1];
}
else if(spacepos!=-1)
{
flname = Text.Split(' ');
_FirstName = flname[0];
_LastName = flname[1];
}
else
{
throw new InvalidOperationException();
}
return;
}
private void TxtName_TextChanged(object sender, EventArgs e)
{
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace custctrl
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//LblLast.Text = name1.Text;
LblFirst.Text = name1.GetFirstName();
LblLast.Text = name1.GetLastName();
}
}
}
Output

Creating a Name seperator Custom Control using C#

  • 1.
    Name.cs using System; using System.Collections.Generic; usingSystem.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace custctrl { public partial class Name :TextBox { public Name() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { base.OnPaint(pe); } private String _FirstName; private String _LastName; public String GetFirstName() { UpdateNames(); return _FirstName; } public String GetLastName() { UpdateNames(); return _LastName; }
  • 2.
    private void UpdateNames() { intcommapos; int spacepos; string name; name = Text; commapos =Text.IndexOf(","); spacepos =Text.IndexOf(""); string[] flname; if(commapos!=-1) { flname = Text.Split(','); _FirstName = flname[0]; _LastName = flname[1]; } else if(spacepos!=-1) { flname = Text.Split(' '); _FirstName = flname[0]; _LastName = flname[1]; } else { throw new InvalidOperationException(); } return; } private void TxtName_TextChanged(object sender, EventArgs e) { } } }
  • 3.
    Form1.cs using System; using System.Collections.Generic; usingSystem.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace custctrl { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //LblLast.Text = name1.Text; LblFirst.Text = name1.GetFirstName(); LblLast.Text = name1.GetLastName(); } } }
  • 4.