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;
}
}
}
}
}
}
Related
I am triying the adapt my function which is found in dll but I am newbie on using dll and dividing the code.
The code in the dll is:
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;
namespace gdll
{
public class Class1
{
private Bitmap DoGray(Bitmap bmp)
{
for (int i = 0; i < bmp.Height - 1; i++)
{
for (int j = 0; j < bmp.Width - 1; j++)
{
int value = (bmp.GetPixel(j, i).R + bmp.GetPixel(j, i).G + bmp.GetPixel(j, i).B) / 3;
Color clr;
clr = Color.FromArgb(value, value, value);
bmp.SetPixel(j, i, clr);
}
}
return bmp;
}
}
}
But how should I write the code in form1.cs. Form1.cs[Design] have two buttons and 2 pictureBox.The first button for the original picture,the second one is for the filtered picture.I wrote the code version without dll(I wrote the function to the same page)below:
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}
Of course it is not working while function at dll file.
And this is the codes at 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;
using gdll;
namespace dnmimage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog sfd = new OpenFileDialog();
sfd.Filter = "Image Files|*.bmp|All Files|*.*";
sfd.InitialDirectory = ".";
if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
pictureBox1.ImageLocation = sfd.FileName;
}
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = DoGray(image);
pictureBox2.Image = gray;
}
The DoGray function is private. In order to make it visible outside your Class1 it must be public. Further you should make it static:
public static Bitmap DoGray(Bitmap bmp)
Next step is to reference the gdll dll (the one with your DoGray function) needs to be referenced from the WinForms project (the one with the Form1.cs file).
The easiest way to do this is to have both projects (WinForms project and the dll project) in the same Solution and use a project reference.
Then right click on your WinForms project (the project that contains your Form1.cs file) and select “Add Reference…”
In the “Reference Manager” select “Projects” on the left side and check your gdll (the one with the Class1.cs file) and click ok.
Now you can use the DoGray function:
private void button2_Click(object sender, EventArgs e)
{
Bitmap image= new Bitmap(pictureBox1.Image);
Bitmap gray = gdll.Class1.DoGray(image);
pictureBox2.Image = gray;
}
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;
}
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
can viola jones recognize faces without any addition method like PCA or anything else ? how's the accurancity? and how to get rid the false negative in detecting? because there's so much false negative in viola jones detecting. if you know something please tell me.
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;
using Emgu.CV.Util;
using Emgu.CV.Features2D;
using Emgu.CV;
using Emgu.CV.GPU;
using Emgu.CV.VideoStab;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
namespace deteksi_wajah
{
public partial class Form1 : Form
{
Capture capture; // untuk koneksi ke webcam
HaarCascade haar;
public Form1()
{
InitializeComponent();
}
//method
//Proses image aquisision bertipe rgb
private void prosesFrame(object sender, EventArgs arg)
{
Image < Bgr, byte > image = capture.QueryFrame(); //hasil koneksi gambar didapat bertipe rbg
imageBox1.Image = image; // citra yg didapat berada dalam box
if( image != null)
{
Image < Gray, byte > gray = image.Convert<Gray,byte>();
var faces = gray.DetectHaarCascade(haar, 1.1 , 1,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20,20))[0];
foreach (var face in faces)
{
Image<Gray,byte>hasil = image.Copy(face.rect).Convert<Gray,byte>().Resize(100, 100, INTER.CV_INTER_CUBIC);
image.Draw(face.rect, new Bgr(Color.Red),3);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (capture == null)
{
try
{
capture = new Capture();
}
catch
{
}
}
//jika camera tidak sama dengan null
if (capture != null)
{
if (btn_start.Text == "Pause")
{
btn_start.Text = "Resume";
Application.Idle -= prosesFrame; // mengaktifkan kamera
}
else
{
btn_start.Text = "Pause";
Application.Idle += prosesFrame;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
haar = new HaarCascade("haarcascade_frontalface_default.xml");
}
}
}
I think the problem with your code is the parameters in detection are too small.
gray.DetectHaarCascade(haar, 1.1 , 1,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20,20))
Try increase the minNeighbor and minSize value. minNeighbor = 1 do no grouping and cause a lot of false positive. For me, with my current camera at 1280*720 resolution, minNeighbor = 10 and minSize = 100,100 did a very good job.
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.