I have a customTextBox1 on a Form.
customTextbox1 multiline is false and TextAlign is set to center.MaxLength is 23. And the customTextBox1 width is 92.customTextBox1 Font is set to "MS ゴシック", 12F.
When I type "12345678901234567890123" in the TextBox, the text is scrolling to the last character.Also,when I click on the text , the text is highlighted blue and I can drag to the left and right of the text.
.NetFramework 3.5
What I want is 2 things:
1)when the text is longer than TextBox width, I don't want to scroll to the last character.I want to stop scrolling at the right margin of the TextBox.
for example,
when I type "1234567890123456", I want to show "12345678901"and the rest of the overflow text should not be shown.
2)when I Click and Drag the text, I want to show "12345678901" only
And want to get rid of the blue highlighted selection too.
1)overflow text is showing
2)i can click and drag to the end of the text and beginning of the text
here is my code
CustomTextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class CustomTextBox : TextBox
{
public override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
public CustomTextBox()
{
InitializeComponent();
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
customTextBox1.AutoSize = true;
customTextBox1.Size = new Size(92,21);
customTextBox1.Multiline = false;
customTextBox1.TextAlign = HorizontalAlignment.Center;
customTextBox1.MaxLength = 23;
customTextBox1.Font = new Font("MS ゴシック", 12F);
}
}
}
Update1 :
I want to do this strange behavior of textBox because I am making a exact replica of an application written in other language which is not supported anymore. So we have to write it in C#. Both application will run on windows.
In the old application, there is a textBox in which user can type in ID numbers.
1)That textBox does not show overflow text.
If I type ("12345678901234567890123") ,it only show "12345678901" but if I click backspace [13]times, the text begins "1234567890". so I know the overflow text are there just not showing.
2)I can't click and drag the text right and left as in C# textBox.
I have manage to replicate No.1 behavior though.
Here is my code
CustomTextBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Runtime.InteropServices;
namespace DisabledTextSelectForm
{
public partial class CustomTextBox : TextBox
{
public override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = value; }
}
public bool DisabledScrolling { get; set; }
int caretPos = 0;
public CustomTextBox()
{
InitializeComponent();
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
var isDigit = char.IsDigit(e.KeyChar);
var isBackSpace = e.KeyChar == (char)Keys.Back;
var diffWidth = 0;
if (Text.Length >= 2)
{
var firstChar = TextRenderer.MeasureText(Text[0].ToString(), Font);
var secondChar = TextRenderer.MeasureText(Text.Substring(0, 2).ToString(), Font);
diffWidth = secondChar.Width - firstChar.Width;
caretPos = Width / diffWidth;
}
if (caretPos != 0 && Text.Length >= caretPos && DisabledScrolling)
{
if (isDigit)
{
Text = Text.Length < MaxLength ? Text + e.KeyChar.ToString() : Text;
}
else if (isBackSpace)
{
Text = Text.Substring(0,Text.Length - 1);
}
ScrollTo(caretPos - 1);
e.Handled = true;
}
base.OnKeyPress(e);
}
private void ScrollTo(int scrollPosition)
{
if (Text.Length >= scrollPosition)
{
Select(scrollPosition, 0);
ScrollToCaret();
}
}
}
}
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DisabledTextSelectForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
customTextBox1.DisabledScrolling = true;
customTextBox1.AutoSize = true;
customTextBox1.Size = new Size(92,21);
customTextBox1.Multiline = false;
customTextBox1.TextAlign = HorizontalAlignment.Center;
customTextBox1.MaxLength = 23;
customTextBox1.Font = new Font("MS ゴシック", 12F);
}
}
}
I know how to disable clicking and draging of Text in TextBox.
Add a Timer then set it Enable with 10 ms interval and in Tick event of your Timer put this code:
private void timer1_Tick(object sender, EventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
In MouseMove event of your TextBox put this code:
private void textBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
textBox1.SelectionLength = 0;
textBox1.SelectionStart = 0;
textBox1.ScrollToCaret();
}
In KeyDown event of your TextBox put this code:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
textBox1.SelectionStart = textBox1.Text.Length;
}
Add this code in initialize block
public Form1()
{
InitializeComponent();
textBox1.HideSelection = true;
}
Related
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MyTest
{
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private GroupBox lastGB = null;
public Form1()
{
InitializeComponent();
GroupBox gb = new GroupBox();
gb.Location = new Point(100, (lastGB == null ? startingY : lastGB.Bounds.Bottom));
gb.Size = new Size(1220, 400);
gb.BackColor = SystemColors.Window;
gb.Text = "";
gb.Font = new Font("Colonna MT", 12);
this.Controls.Add(gb);
}
It's creating a small line on the top of the groupbox and I don't want this line to show.
And I want to write some text on it in the middle of it.
How can I make it just complete white ? And how to write some text in the middle on the groupbox ?
The main idea is to create over the form a white sheet or box with text inside that's it.
It looks like your intention is to put subsequent boxes just below the last box. As mentioned, a Label would probably be best. I'd also move that code to a method you can call over and over to keep from repeating code elsewhere. You could pass a message to display in the Label. Also, don't forget to update the reference to the "last box" when ever you create a new one:
public partial class Form1 : Form
{
private int x, y;
private int gap = 0;
private int startingY = 150;
private Label lastLbl = null;
public Form1()
{
InitializeComponent();
AddLabel("Hello World");
}
private void button1_Click(object sender, EventArgs e)
{
AddLabel(DateTime.Now.ToString());
}
private void AddLabel(String msg)
{
Label lbl = new Label();
lbl.Location = new Point(100, (lastLbl == null ? startingY : lastLbl.Bounds.Bottom));
lbl.Size = new Size(1220, 400);
lbl.BackColor = Color.White;
lbl.Text = msg;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Font = new Font("Colonna MT", 12);
this.Controls.Add(lbl);
lastLbl = lbl;
}
}
I am using C# and .NET 4 to make a little program for my friend. I am a bengineer in coding.
I want to count the number of pressing X and Y on the keyboard. I managed to make it running in the background, but i have problem with counting. I tried searching for functions that may help me, but i didn't find anyting.
The program now running and if X or Y is pressed down, the counter just spinning.
Here's what i have:
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;
using System.Threading;
using System.Windows.Input;
namespace gombszamlalo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool fut = true;
private void Form1_Load(object sender, EventArgs e)
{
Thread TH = new Thread(billentyuzet);
CheckForIllegalCrossThreadCalls = false;
TH.SetApartmentState(ApartmentState.STA);
TH.Start();
}
public int x = 0;
public int y = 0;
void billentyuzet()
{
while (fut)
{
if ((Keyboard.GetKeyStates(Key.X) & KeyStates.Down) > 0)
{
x++;
label4.Text = x.ToString();
}
if ((Keyboard.GetKeyStates(Key.Y) & KeyStates.Down) > 0)
{
y++;
label5.Text = y.ToString();
}
if ((Keyboard.GetKeyStates(Key.F6) & KeyStates.Down) > 0)
{
fut = false;
}
}
}
I would be very happy, if somebody can help me to fix this code. Thank you very much!
Try this Global Mouse and Keyboard Library
I believe it contains all you need.
For counting Keypresses on Background you need Keyboard Hook. Have a look at these link:
https://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
C# : Keyboard Hook
This is in WinForms. and i am using microsoft.vsualbasic.powerpacks
how can i concatenate Rectangle control name in c# this is what i have so far
string n = "1";
Rectangle match = this.Controls.Find("rectangleShape" + n,true)[0] as Rectangle;
match.BackColor = Color.Red;
Try some of the solutions
here... – Idle_Mind
okay will do try – droid fiji
it didnt really help :( – droid fiji
Here's a working example based on the solution I linked to in the comments. Note that the BackStyle property of your RectangleShape needs to be Opaque for you to see the color you set!
This code will set the BackColor of rectangeShape1 thru rectangeShape3 to Red:
using Microsoft.VisualBasic.PowerPacks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string rectName;
string rectBaseName = "rectangleShape";
var shapeContainer = this.Controls.OfType<ShapeContainer>().FirstOrDefault();
if (shapeContainer != null)
{
for (int i = 1; i <= 3; i++)
{
rectName = rectBaseName + i.ToString();
RectangleShape match = shapeContainer.Shapes.OfType<RectangleShape>().FirstOrDefault(o => o.Name == rectName);
if (match != null)
{
match.BackColor = Color.Red;
match.BackStyle = BackStyle.Opaque;
}
}
}
}
}
}
I have a little problem of mine to solve. I have a button which has an image background.I tried to color the whole button but the image can not be seen after coloring the whole button. How can I edit this "imagebutton" like in this example? http://i.stack.imgur.com/XaQQQ.png
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 bura
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.BackgroundImage != null)
{
button2.BackgroundImage = null;
button2.BackColor = Color.Black;
}
else {
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
private void button3_Click(object sender, EventArgs e)
{
button2.BackgroundImageLayout = ImageLayout.Stretch;
button2.BackgroundImage = Image.FromFile("C:\\Users\\rati\\Desktop\\ks.png");
}
}
}
This
Just made a button by using the designer with the following code:
this.button1.BackColor = System.Drawing.Color.DodgerBlue;
this.button1.BackgroundImage = global::WindowsFormsApplication.Properties.Resources.ChargeImage;
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.button1.Image = global::WindowsFormsApplication.Properties.Resources.DatabaseImage;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(264, 160);
this.button1.TabIndex = 0;
this.button1.UseVisualStyleBackColor = false;
And that is the result:
So what is your problem?
You can edit your pictures with a method like this:
private static void DrawLinesOnBitmap(Bitmap bmp)
{
using (var p = new Pen(Color.Black, 5))
{
using (var g = Graphics.FromImage(bmp))
{
g.DrawLine(p, 0, 0, bmp.Width, bmp.Height);
}
}
}
This method adds a line from the left top corner to the right bottom corner. Just draw some more lines and you should get your wanted result.
I currently have an TChart where I want to introduce a draggable horizontal line which changes the color of the points below the line. I have chosen to use ColorLine for this purpose but the line does not appear in the TChart. Am I using the right TChart tool for the job or am I missing something?
Below is the stripped down version of my current code.
public class testClass
{
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public testClass()
{
line = new Colorline();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
}
Code below works fine for me here. If your problem persists please send a Short, Self Contained, Correct (Compilable), Example project to reproduce the problem here. You can post your files at www.steema.net/upload.
using Steema.TeeChart;
using Steema.TeeChart.Styles;
using Steema.TeeChart.Tools;
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
testClass();
//existing code which assigns colors
tChart1.Series.Add(new Steema.TeeChart.Styles.Bar()).FillSampleValues();
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
tChart1.Tools.Add(line);
line.Active = true;
line.Axis = tChart1.Axes.Left;
line.Value = lineYVal;
}
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public void testClass()
{
line = new ColorLine();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
if (savedChart != null)
{
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
else
{
foo(tChart1);
}
}
}
}
It turns out that while the line was being added correctly, the code I was using to display the chart was not updating the chart properly and was displaying a version of the chart from before it was passed into my highlight function.