Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Form parent = (Form)FindForm();

this works in class DropDownTextBox : UserControl
but I use this in class DropDownText : System.Windows.Forms.TextBox
it returns null

I just wants location of parent form when it moved
Thanks in advance.
Posted
Comments
Alexander Dymshyts 7-Jan-14 3:30am    
can you provide more code? it's hard to understand what you mean
Sid_Joshi 7-Jan-14 4:15am    
AutoSuggest is a User control I wants to build
Please help me to build
thanks in advance
Sid_Joshi 7-Jan-14 3:53am    
namespace WindowsFormsApplication2
{
public partial class AutoSuggest : System.Windows.Forms.TextBox
{
public AutoSuggest()
{
InitializeComponent();
initControl();
}
int index = -1;
ListBox listBox1;
Form f3;
private DataTable dt;
Form parent;
public DataTable DataSource
{
get { return dt; }
set { dt = value; }
}

protected void initControl()
{
listBox1 = new ListBox();
listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
f3 = new Form();
f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f3.ShowInTaskbar = false;
f3.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.TextChanged += new EventHandler(textBox1_TextChanged);
parent = (Form)FindForm();
//parent.Resize += new EventHandler(parent_Resize);
//parent.ResizeEnd += new EventHandler(parent_ResizeEnd);
//parent.Move += new EventHandler(parent_Move);
listBox1.Click += new EventHandler(listBox1_Click);
}


private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);
listBox1.Items.Clear();
index = -1;
//listBox1.Visible = false;
removeComp();
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);
listBox1.Items.Clear();
index = -1;
//listBox1.Visible = false;
removeComp();
}
public void putComp()
{
Point location = this.PointToScreen(Point.Empty);
int x = location.X-1;
int y = location.Y + this.Height-2; //Down to textBox
//int y = textBox1.Location.Y -95; //Up to textBox

//int y = this.Location.Y + textBox1.Location.Y + textBox1.Height + 22;


f3.Location = new System.Drawing.Point(x, y);

f3.Width = this.Width;
f3.Height = 200;
//listBox1.Location = new System.Drawing.Point(0, 0);
listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
f3.TopMost = true;
f3.Show();
f3.Controls.Add(listBox1);
this.Focus();


}
public void removeComp()
{

f3.Hide();
f3.Controls.Remove(listBox1);
index = -1;
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (e.KeyCode == Keys.Down)
{
index++;
if (index >= listBox1.Items.Count - 1)
{
index = listBox1.Items.Count - 1;

}
}
if (e.KeyCode == Keys.Up)
{

index--;
if (index <= 0)
{
index = 0;

}
//textBox1.Select(textBox1.TextLength, 0);
e.SuppressKeyPress = true;

}

listBox1.SelectedIndex = index;

if (e.KeyCode == Keys.Enter)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);


listBox1.Items.Clear();
index = -1;
Sid_Joshi 7-Jan-14 4:10am    
//listBox1.Visible =false;
removeComp();
}
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
listBox1.Items.Clear();

if (this.TextLength != 0)
{
//listBox1.Visible = true;
putComp();

DataRow[] dr = dt.Select(dt.Columns[0].Caption + " like '" + this.Text + "%'");
foreach (DataRow row in dr)
{
listBox1.Items.Add(row[0].ToString());
}

}
else
//listBox1.Visible = false;
removeComp();
}

private void parent_Resize(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
removeComp();
putComp();
}
}
private void parent_ResizeEnd(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
removeComp();
putComp();
}
}
private void parent_Move(object sender, EventArgs e)
{
if (listBox1 != null)
{
if (listBox1.Items.Count > 0)
{
removeComp();
putComp();
}
}
}
private void listBox1_Click(object sender, EventArgs e)
{
parent.Focus();
}

}
}

1 solution

Don't. It's a poor idea from an OOPs point of view, because the control should not know what it is embedded into.
Instead, the parent form should feed information to the user control when it needs updating, or if the functionality of the control absolutely requires access to it's containing form information, then it should be passed with the control constructor.
 
Share this answer
 
Comments
Sid_Joshi 7-Jan-14 4:25am    
Can u explain me with code?
OriginalGriff 7-Jan-14 4:47am    
Explain which bit?
Sid_Joshi 7-Jan-14 5:08am    
I can't understand what you wants to talk.
OriginalGriff 7-Jan-14 5:10am    
Sorry - I'll try to explain more clearly.
"Can u explain me with code?"
Which part of the operation do you need explained?
Sid_Joshi 7-Jan-14 7:45am    
namespace WindowsFormsApplication2
{
public partial class AutoSuggest : System.Windows.Forms.TextBox
{
public AutoSuggest()
{
InitializeComponent();
initControl();
}
int index = -1;
ListBox listBox1;
Form f3;
private DataTable dt;
Form parent;
public DataTable DataSource
{
get { return dt; }
set { dt = value; }
}

protected void initControl()
{
listBox1 = new ListBox();
listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
f3 = new Form();
f3.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f3.ShowInTaskbar = false;
f3.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.KeyDown += new KeyEventHandler(textBox1_KeyDown);
this.TextChanged += new EventHandler(textBox1_TextChanged);
parent = (Form)FindForm();
//parent.Resize += new EventHandler(parent_Resize);
//parent.ResizeEnd += new EventHandler(parent_ResizeEnd);
//parent.Move += new EventHandler(parent_Move);
listBox1.Click += new EventHandler(listBox1_Click);
}


private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);
listBox1.Items.Clear();
index = -1;
//listBox1.Visible = false;
removeComp();
}
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);
listBox1.Items.Clear();
index = -1;
//listBox1.Visible = false;
removeComp();
}
public void putComp()
{
Point location = this.PointToScreen(Point.Empty);
int x = location.X-1;
int y = location.Y + this.Height-2; //Down to textBox
//int y = textBox1.Location.Y -95; //Up to textBox

//int y = this.Location.Y + textBox1.Location.Y + textBox1.Height + 22;


f3.Location = new System.Drawing.Point(x, y);

f3.Width = this.Width;
f3.Height = 200;
//listBox1.Location = new System.Drawing.Point(0, 0);
listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
f3.TopMost = true;
f3.Show();
f3.Controls.Add(listBox1);
this.Focus();


}
public void removeComp()
{

f3.Hide();
f3.Controls.Remove(listBox1);
index = -1;
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (listBox1.Items.Count > 0)
{
if (e.KeyCode == Keys.Down)
{
index++;
if (index >= listBox1.Items.Count - 1)
{
index = listBox1.Items.Count - 1;

}
}
if (e.KeyCode == Keys.Up)
{

index--;
if (index <= 0)
{
index = 0;

}
//textBox1.Select(textBox1.TextLength, 0);
e.SuppressKeyPress = true;

}

listBox1.SelectedIndex = index;

if (e.KeyCode == Keys.Enter)
{
this.Text = listBox1.Text;
this.Select(this.Text.Length, 0);


listBox1.Items.Clear();
index = -1;
//listBox1.Visible =

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900