How can i create two sections to focus and other to writting? - c#

This code i am using to focus of a entry, its a custon entry
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Entry.IsFocusedProperty.PropertyName )
{
//place1. this code is use to focus
}
// place2. here enter the text when the user written
}
How can i know if the user is writting? but dont enter to both places.

Option 1:You could set the TextProperty to check the user editing .
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == Entry.IsFocusedProperty.PropertyName)
{
//place1. this code is use to focus
}
if (e.PropertyName == Entry.TextProperty.PropertyName)
{
//place2. this code is use to edit
var content = Element.Text;
}
}
Option 2:
You could set the TextChanged of the Entry .
protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged(e);
if(Control!=null)
{
Element.TextChanged += Element_TextChanged;
}
}
private void Element_TextChanged(object sender, TextChangedEventArgs e)
{
// var content = Element.Text;
}

Related

Reorder customcontrol inside multiple flowLayoutPanel

I am trying to make an app to generate a tierlist.
Each tier consists of a flowlayout and you basically move the controls around by drag-and-drop.
The problem is that even though I can move the elements with drag-and-drop, the element is always added to the end of the list, and not where you drop the pointer.
Is there any way to maintain order during drag-and-drop?
These are the methods that are in the flowlayout
private void flow_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void flow_DragDrop(object sender, DragEventArgs e)
{
((UserControl1)e.Data.GetData(typeof(UserControl1))).Parent = (Panel)sender;
}
While these are from the usercontrol:
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
this.DoDragDrop(this, DragDropEffects.Move);
}
You can opt this solution to reorder the dropped controls into a FlowLayoutPanel control. The part that utilizes the Control.ControlCollection.GetChildIndex and Control.ControlCollection.SetChildIndex methods.
Let's say you have a custom Control or UserControl named DragDropControl:
public class DragDropControl : Control
{
public DragDropControl()
{
AllowDrop = true;
BackColor = Color.LightSteelBlue;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
TextRenderer.DrawText(e.Graphics, Text, Font,
ClientRectangle, Color.Black,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter);
}
}
Note: From what I see in the images, just use a simple Label control instead.
Let's create a custom FlowLayoutPanel and encapsulate all the required functionalities to not repeat that in your implementation for each FLP.
public class DragDropFlowLayoutPanel : FlowLayoutPanel
{
public DragDropFlowLayoutPanel()
{
AllowDrop = true;
}
[DefaultValue(true)]
public override bool AllowDrop
{
get => base.AllowDrop;
set => base.AllowDrop = value;
}
The custom FLP implements the mouse, drag and drop events of its children as well.
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is DragDropControl)
{
e.Control.DragOver += OnControlDragOver;
e.Control.DragDrop += OnControlDragDrop;
e.Control.MouseDown += OnControlMouseDown;
}
}
protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
e.Control.DragOver -= OnControlDragOver;
e.Control.DragDrop -= OnControlDragDrop;
e.Control.MouseDown -= OnControlMouseDown;
}
Handle the child controls MouseDown event to do the drag:
private void OnControlMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var control = sender as DragDropControl;
DoDragDrop(control, DragDropEffects.Move);
}
}
Handle the DragEnter and DragOver methods and events of the FLP and its children to set the drop effect.
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
private void OnControlDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
if (GetChildAtPoint(p) == ddc)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
}
Finally, call from the DragDrop method override and event the DropControl method and pass the DragEventArgs param.
protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);
DropControl(e);
}
private void OnControlDragDrop(object sender, DragEventArgs e)
{
DropControl(e);
}
The dropped control takes the index of the control under the mouse position if any otherwise it will be inserted at the end of the Controls collection.
private void DropControl(DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
var child = GetChildAtPoint(p);
var index = child == null
? Controls.Count
: Controls.GetChildIndex(child);
ddc.Parent = this;
Controls.SetChildIndex(ddc, index);
}
}
}
Put it all together.
public class DragDropFlowLayoutPanel : FlowLayoutPanel
{
public DragDropFlowLayoutPanel()
{
AllowDrop = true;
}
[DefaultValue(true)]
public override bool AllowDrop
{
get => base.AllowDrop;
set => base.AllowDrop = value;
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is DragDropControl)
{
e.Control.DragOver += OnControlDragOver;
e.Control.DragDrop += OnControlDragDrop;
e.Control.MouseDown += OnControlMouseDown;
}
}
protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
e.Control.DragOver -= OnControlDragOver;
e.Control.DragDrop -= OnControlDragDrop;
e.Control.MouseDown -= OnControlMouseDown;
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);
DropControl(e);
}
private void OnControlDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
if (GetChildAtPoint(p) == ddc)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
}
private void OnControlDragDrop(object sender, DragEventArgs e)
{
DropControl(e);
}
private void OnControlMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var control = sender as DragDropControl;
DoDragDrop(control, DragDropEffects.Move);
}
}
private void DropControl(DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
var child = GetChildAtPoint(p);
var index = child == null
? Controls.Count
: Controls.GetChildIndex(child);
ddc.Parent = this;
Controls.SetChildIndex(ddc, index);
}
}
}

Session variable can't be updated

Whenever i try to update a session variable that has been previously entered it won't update.
Heres an example on what i'm talking about:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
So when i click the button the first time, the textbox will be updated. But when i edit the text and click the button again, the textbox just reverts to what it was the first time i.e doesn't update. Anyone have any ideas?
So when i click the button the first time, the textbox will be
updated. But when i edit the text and click the button again, the
textbox just reverts to what it was the first time
I believe it's because you are doing just exactly as that:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
In that code you have you should be checking if the page load is caused by a post back (click of a button). So you should be doing this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Test"] != null && Session["Test"].ToString().Length > 0)
{
TextBox1.Text = Session["Test"].ToString();
}
}
Session["Test"] = string.Empty;
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
This is tested code.
Do it like this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["Test"] = "";
}
if (Session["Test"] != null)
{
Session["Test"] = ASPxTextBox1.Text;
}
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
ASPxTextBox1.Text = Session["Test"].ToString();
}
You page is posted back thats why it is taking previous value as you have written
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
int this code the text box text will be restored with the previous value which you entered before,
so your code should be
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
}
This should work
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["Test"] != null)
{
TextBox1.Text = Session["Test"].ToString();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
You are getting the Page_Load event before the Button Click, so your Page_Load is overwriting the value of TextBox1.Text with the previous value in the Session. That is why it never changes after the first time it is set.
Check that you are not responding to a post on Page_Load like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TextBox1.Text = (Session["Test"] ?? "").ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["Test"] = TextBox1.Text;
}
With that said, you probably want to avoid using the Session altogether if it can be helped.

Unsubscribe Event Handler from Protected Override Void

Is it possible to unsubscribe an event handler from an protected override void?
protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
{
base.OnViewLoaded(sender, e);
list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer sb = e.OriginalSource as ScrollViewer;
if (sb.ContentVerticalOffset==sb.ScrollableHeight)
{
scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
}
My problem is that i dont get acces to the scroll obejct in the scroll_ScrollChanged Method.
This code is not tested but can't you simple cast the sender object to the ScrollViewer and unsubscribe from the event like that:
protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
{
base.OnViewLoaded(sender, e);
list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer scroll = (ScrollViewer)sender;
if (scroll.ContentVerticalOffset==scroll.ScrollableHeight)
{
scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
}
Sender should be a reference to the object that you require.
protected override void OnViewLoaded(object sender, ViewLoadedEventArg e)
{
base.OnViewLoaded(sender, e);
list = VisualTreeUtil.FindFirstInTree<ListView>(Application.Current.MainWindow, "ListView");
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer scroll = VisualTreeUtil.FindFirstInTree<ScrollViewer>(this.list);
scroll.ScrollChanged+=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
ConfigureAndSuperviseInputControls(this.list);
ScrollViewer sb = sender as ScrollViewer;
if (sb.ContentVerticalOffset==sb.ScrollableHeight)
{
scroll.ScrollChanged-=new ScrollChangedEventHandler(scroll_ScrollChanged);
}
}

DataGridView RowValidation Error

I'm sync my data from Grid to DataBase using really weird way :
for example :
#region Line methods
private void LinesView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
private void LinesView_RowValidated(object sender, DataGridViewCellEventArgs e)
{
lineTableAdapter.Update(fRIIBDataSet.Line);
}
#endregion
but when I switched some columns to ComboBox I need to make some trick before Update alike that :
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
//deltaTableAdapter.Update(fRIIBDataSet.Delta); TODO
}
private void dataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
{
deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (lambdacat != null)
{
string selected = (LimView.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell).FormattedValue.ToString();
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
}
//deltaTableAdapter.Update(fRIIBDataSet.Delta);
}
troubles comes after uncommenting Update method.
When I opening the window with a table I've got error message : Damaged the internal index DataTable: "5. " How can I fix / avoid this error ?
Does modifying it to this work ?
if (selected != "")
{
int find = Array.IndexOf(dict, dict.Where(x => x == selected).FirstOrDefault());
LimView.Rows[e.RowIndex].Cells[0].Value = dictiddarray[find];
fRIIBDataSet.Delta.BeginInit(); //
deltaTableAdapter.Update(fRIIBDataSet.Delta);
fRIIBDataSet.Delta.EndInit();
}

C# executing code from one button in another

I've one button - button1click and textbox in which when i type something and press enter, i'd like to run code from button1click.
How can I do it without copying entire code from button1click into EnterPressed?
private void button1click(object sender, EventArgs e)
{
//Some Code
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Execute code from button1
}
}
Maybe something like that? But I'm getting errors...
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(object sender, EventArgs e)
}
}
Just have button1_click call a method and then you can call that same method from anywhere you want to. So in your example:
private void button1click(object sender, EventArgs e)
{
Foo();
}
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Foo();
}
}
void Foo()
{
//Do Something
}
I personally wouldn't manually call another control's event.
void EnterPressed(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1click(sender, EventArgs.Empty);
}
}

Categories

Resources