In Windows Store app (XAML/C#), I've created a custom control with 'RichEditBox' in it. I want to limit number of characters (MaxLength) for it or at least vertical scroll bar should be disabled. How can I achieve it?
I think it's not possible to set a limit for the number of characters in a declarative manner, but you may handle the text changed event and do your checks in code behind.
For the scroller you can set the ScrollViewer.VerticalScrollBarVisibility property to Disabled.
Here try this out:
<RichEditBox x:Name="TextElementControl"
Background="{Binding Background, ElementName=userControlModified}"
ManipulationMode="None"
ScrollViewer.HorizontalScrollMode="Disabled"
AcceptsReturn="True" TextWrapping="Wrap"
SizeChanged="TextElementControlSizeChanged"
IsDoubleTapEnabled="False"
BorderThickness="0"
BorderBrush="{x:Null}"
Padding="10,10,10,10"
/>
Code Behind:
TextElementControl.TextChanged += TextElementControlTextChanged;
More Code Behind:
private void TextElementControlTextChanged(object sender,
RoutedEventArgs e)
{
string str;
TextElementControl.Document.GetText(Windows.UI.Text.TextGetOptions.None,
out str);
TextElementControl.Height = double.NaN;
if (str.Trim().Length > 502 && !_loading)
{
if (popUpReminder == null)
{
popUpReminder = new Popup();
popUpReminder.IsLightDismissEnabled = true;
var panel = new StackPanel();
panel.Background = BlackSolidColorBrush;
panel.Height = 60;
panel.Width = 220;
var reminderText = new TextBlock();
reminderText.FontSize = 14;
reminderText.Text = "You have exceeded the maximum number of characters for this textbox.";
reminderText.TextWrapping = TextWrapping.Wrap;
reminderText.Focus(Windows.UI.Xaml.FocusState.Programmatic);
reminderText.Margin = new Thickness(10, 5, 10, 5);
panel.Children.Add(reminderText);
Border brder = new Border();
brder.BorderBrush = RedSolidColorBrush;
brder.BorderThickness = new Thickness(2);
brder.Child = panel;
popUpReminder.Child = brder;
popUpReminder.HorizontalOffset = Window.Current.CoreWindow.Bounds.Width - panel.Width - 10;
popUpReminder.VerticalOffset = Window.Current.CoreWindow.Bounds.Bottom - 100 - panel.Height - 10;
}
popUpReminder.IsOpen = true;
TextElementControl.Document.Undo();
} }
Related
I am using below code to create tooltips.
m_ctrlToolTip.Create(this, TTS_ALWAYSTIP|TTS_BALLOON);
m_ti.cbSize = sizeof(TOOLINFO);
m_ti.uFlags = TTF_IDISHWND|TTF_TRACK|TTF_TRANSPARENT|TTF_ABSOLUTE;
m_ti.hwnd = m_hWnd;
m_ti.hinst = NULL;
m_ti.uId = (UINT)1;
m_ti.lpszText = "";;
m_ti.rect=CRect(0,0,0,0);
m_ctrlToolTip.SetMaxTipWidth(SHRT_MAX);
m_ctrlToolTip.SetDelayTime(TTDT_AUTOPOP,5000);
m_ctrlToolTip.Activate(TRUE);
void CLadIOView::OnMouseMove(UINT nFlags, CPoint point)
{
static CPoint prevPoint =0;
static CLadRemoteIOModule* pLastIO=NULL;
CLadRemoteIOModule* pLastIO1=pLastIO;
pLastIO=NULL;
bool bToolTipSet = false;
// Go thru each module already added
POSITION pos = gobjEztouchApp.m_objLadderLogic.m_objSysAttr.m_objRemoteIOModuleLst.GetHeadPosition();
for( ; pos != NULL; )
{
CLadRemoteIOModule* pIO = gobjEztouchApp.m_objLadderLogic.m_objSysAttr.m_objRemoteIOModuleLst.GetNext(pos);
// Get the rectangle for the module
CRect rectModule = GetIOModuleRect(pIO->m_nModulePosition);
if(!rectModule.PtInRect(pt)) continue;
pLastIO=pIO;
if(pLastIO1==pIO) break;
if(!m_bMouseDown && !m_bPlacingANewModule && prevPoint != pt)
{
CString sDescription, sPartNumber, sAddressRange;
GetIOModuleText2(pIO,sDescription, sPartNumber, sAddressRange);
sPartNumber.Remove('[');
sPartNumber.Remove(']');
CString sModuleDetails;
sModuleDetails.Format(_T("Position: M%d\nModule Type: %s\nModule Part No: %s"), pIO->m_nModulePosition+1, sDescription,sPartNumber);
if(pIO->GetIPSize() > 0)
sModuleDetails+=_T("\nInput Address: ")+ pIO->GetInputAdr()+_T(" - ")+pIO->GetEndInputAdr();
if(pIO->GetOPSize() > 0)
sModuleDetails+=_T("\nOutput Address: ")+ pIO->GetOutputAdr()+_T(" - ")+pIO->GetEndOutputAdr();
CPoint pp = pt-GetScrollPosition();
ClientToScreen(&pp);
m_ctrlToolTip.UpdateTipText(sModuleDetails,this,1);
m_ctrlToolTip.SendMessage(TTM_TRACKPOSITION, 0, (LPARAM)MAKELPARAM(pp.x, pp.y+16));//+16 to move the tooltip stem down
m_ctrlToolTip.SendMessage(TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ti);
//to track mouse leave abd there we can remove tooltip
TRACKMOUSEEVENT tk;
tk.cbSize = sizeof(tk);
tk.dwFlags = TME_LEAVE;
tk.hwndTrack = m_hWnd;
_TrackMouseEvent(&tk);
prevPoint = pt;
bToolTipSet = true;
}
bToolTipSet = true;
break;
}
if(!bToolTipSet)
{
m_ctrlToolTip.SendMessage(TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ti);
m_ctrlToolTip.UpdateTipText("",this,1);
m_ctrlToolTip.Pop();
}
The tooltip remains visible for very few seconds and then they disappear. I find this time too short to read some of the longer tooltips. Is there a way to increase the time they remain visible?
I tried to increase the time using setdelaytime function but it doesn't help me.
Thanks in advance.
I am writing application in windows phone 8.
I need to provide a textbox which will allow user to enter only "Numbers" and if required, only one dot ".
I set <TextBox InputScope="Number" /> but it allow multiple dot.
How can i set single dot in textbox of windows phone?
Set an event that triggers everytime a text has change like this:
<TextBox x:Name="textBox1" TextChanged="textBox1_TextChanged" />
And then in the event function loop through the text, count the dots and if the dot count is higher than 1, delete the said dot.
EDIT: you said if I could provide a sample algorithm:
string str = textBox1.Text;
int dotCount = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '.')
{
dotCount++;
if (dotCount > 1)
{
str.Remove(i, 1);
i--;
dotCount--;
}
}
}
This code is not working as it should so I made some improvements.. hope it helps! I'm using KeyUp but TextChange is also OK to use.
private void tbMainText_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
TextBox tb = (TextBox)sender; //Getting the textbox which fired the event if you assigned many textboxes to the same event.
string str = tb.Text;
int dotCount = 0;
for (int i = 0; i < str.Length; i++)
{
if (str[i] == '.')
{
dotCount++;
if (dotCount > 1)
{
str = str.Remove(i, 1); //Assigning the new value.
i--;
dotCount--;
}
}
}
tb.Text = str;
tb.Select(tb.Text.Length, 0); //Positioning the cursor at end of textbox.
}
I have a winform application on which there is a mix of Winform and a WPF control hosted on ElementHost. When the winform textbox goes into Validating because the WPF control is clicked, and there is validating error, the ElementHost keeps firing Focus event which makes my application stuck.
Please advice if there is a way to resolve it.
I have created a sample application to replicate the issue.
I have a Form1 containing 2 winform textboxes (textbox1, textbox2) and a WPF control hosted on ElementHost.
textbox1_validating event handler checks if input is not "1", then the event is cancelled by setting the event argument e.Cancel to true. This works well if textbox1 goes into validating because other winform control is clicked.
However, if textbox1 goes into validating due to WPF control gains focus, the cancel event does not work. ElementHost keeps firing Focus event causing textbox1_validating been called again and again.
UserControl Code:
<UserControl x:Class="WpfControlLibrary1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="54" d:DesignWidth="295" >
<Grid Height="43">
<TextBox Height="20" HorizontalAlignment="Left" Name="textBox1" VerticalAlignment="Top" Width="106" Text="Default1" />
<TextBox Height="20" HorizontalAlignment="Left" Margin="130,0,0,0" Name="textBox2" VerticalAlignment="Top" Width="73" Text="Default2" />
</Grid>
WinForm Code:
public partial class Form1 : Form
{
public static bool HasError = false;
public Form1()
{
InitializeComponent();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
if (textBox1.Text != "1" && textBox1.Text != "")
{
MessageBox.Show("Input is invalid, expecting \"1\". ");
HasError = true;
e.Cancel = true;
}
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this._durationCtrlHost = new WindowsFormsApplication1.DurationCtrlHost();
this._durationCtrl = new WpfControlLibrary1.UserControl1();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(120, 23);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(242, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox1_Validating);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(121, 56);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(240, 20);
this.textBox2.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(24, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(90, 13);
this.label1.TabIndex = 4;
this.label1.Text = "WinformTextbox1";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(25, 63);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(90, 13);
this.label2.TabIndex = 5;
this.label2.Text = "WinformTextbox2";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(24, 104);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 13);
this.label3.TabIndex = 6;
this.label3.Text = "WPFControl";
//
// _durationCtrlHost
//
this._durationCtrlHost.Location = new System.Drawing.Point(120, 90);
this._durationCtrlHost.Name = "_durationCtrlHost";
this._durationCtrlHost.Size = new System.Drawing.Size(271, 60);
this._durationCtrlHost.TabIndex = 3;
this._durationCtrlHost.Child = this._durationCtrl;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(420, 282);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this._durationCtrlHost);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
}
public class DurationCtrlHost : ElementHost
{
private const int WM_SETFOCUS = 0x0007;
private const int WM_KILLFOCUS = 0x0008;
private int SetFocusFiredCount = 0;
private int KillFocusFiredCount = 0;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SETFOCUS)
{
SetFocusFiredCount++;
if (Form1.HasError)
return;
}
else if (m.Msg == WM_KILLFOCUS)
{
KillFocusFiredCount++;
}
base.WndProc(ref m);
}
}
Add New Row in GridView winform on Button Click.
Need to have single button in grid rather than DataGridViewButtonColumn
you can add it by using
GridView1.DataSource = datatable;
and then add new row by this
datatable.Rows.Add();
this will add new row and give control id into add().
use this
public partial class Form1 : Form
{
Button textBoxDgv1 = new Button();
Label labelDgv1 = new Label();
the next is on the Form_Load event
private void Form1_Load(object sender, EventArgs e)
{
labelDgv1.Text = "Delete";
labelDgv1.Height = 20;
labelDgv1.AutoSize = false;
labelDgv1.BorderStyle = BorderStyle.FixedSingle;
labelDgv1.TextAlign = ContentAlignment.MiddleCenter;
int Xdgv1 = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgv1;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
this.dataGridView1.Controls.Add(labelDgv1);
and one more section is in dataGridView1_CellPainting
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
int sum = 0;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[3].Value != string.Empty)
{
sum += Convert.ToInt32(this.dataGridView1[3, i].Value);
}
}
textBoxDgv1.Text = sum.ToString();
int Xdgvx = this.dataGridView1.GetCellDisplayRectangle(2, -1, true).Location.X;
labelDgv1.Width = this.dataGridView1.Columns[2].Width + Xdgvx;
labelDgv1.Location = new Point(0, this.dataGridView1.Height - textBoxDgv1.Height);
textBoxDgv1.Width = this.dataGridView1.Columns[3].Width;
Xdgvx = this.dataGridView1.GetCellDisplayRectangle(3, -1, true).Location.X;
textBoxDgv1.Location = new Point(Xdgvx, this.dataGridView1.Height - textBoxDgv1.Height);
}
There is a requirement of updating image in image boxes of WPF. I am thinking of creating a list with all the paths and then using a timer control checking the 10 seconds. After the 10 seconds has elapsed the next id from list is taken and bound to the image box. I am new to WPF. Can any one help me with a working example.
Use a DispatcherTimer to invoke a method at regular intervalls. In this method change the bound image, remember to raise the INotifyPropertyChanged event to let WPF know it should query the bound property again.
Hi i have made thig running with the below code .
private void timer_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
Action action1 = () => this.BeginStoryboard((Storyboard)this.FindResource("BlinkStoryboardFed"));
Dispatcher.BeginInvoke(action1);
Action action = () => BindToImages(lststr);
Dispatcher.BeginInvoke(action);
//BindToImages(lststr);
_timer.Start();
}
public void BindToImages(List<string> lststrpath)
{
lock (_locker)
{
for (int i = 0; i < lststrpath.Count; i++)
{
if (count == 0)
{
startindex = i;
this.BindToImgIndx = startindex;
AppState.Index = i;
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(lststrpath[startindex].ToString(), UriKind.Relative);
img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit();
image1.Source = img;
count++;
}
else
{
int k = AppState.Index;
k = ++k;
this.BindToImgIndx = startindex;
if (k < lststrpath.Count)
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(lststrpath[k].ToString(), UriKind.Relative);
img.CacheOption = BitmapCacheOption.OnLoad;
img.EndInit();
image1.Source = img;
}
AppState.Index = k;
}
this.BeginStoryboard((Storyboard)this.FindResource("BlinkStoryboardUnFed"));
break;
}
}
}