How to add animated gif to a button? - c#

Can you tell me how to add an animation gif to a button in c#. Just by adding gif to resources and setting as button image didn't work very well (next frames apear over the previous ones). The problem seems to be in a way how c# is treating transparency but I don't know how to fix it.
Thanks in advance.
--- edit ---
After playing with the gif the issue was with transparency in gif. Next frame was drawn over the previous one so elements I wanted to become transparent in the middle of the animation just didn't do it properly (they still have the color of the previous frame). Solution was to create white areas on the next frame to cover the previous one. White was my transparent color so everything looked just fine after that :)
I hope someone will see it useful.
Happy programming :)

Just a note to everyone. Gif animation will play automaticaly if used on the .Image property but NOT on the .BackGroundImage one. Just in case someone is trying it that way.

In order to do this, you need to do the following:
Set the BackGroundImageLayout property to Center. This property is set to Tile by default.
Set the Image property of the button to your animated GIF.
This will work, since I tested it, and it worked for me.
EDIT: Designer code posted below:
this.Button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.Button4.Image = (System.Drawing.Image)resources.GetObject("Button4.Image");
this.Button4.Location = new System.Drawing.Point(220, 329);
this.Button4.Name = "Button4";
this.Button4.Size = new System.Drawing.Size(81, 68);
this.Button4.TabIndex = 4;
this.Button4.Text = "Button4";
this.Button4.UseVisualStyleBackColor = true;

I've got it to work.
1 of 2 things worked for me:
I've reedited the gif adding property to every frame to undraw itself.
I've changed the version of VS (I was using free professional student version, now I
m using one from MSDN subscription)
Thanks for input guys. It guided me to conclusion that that it might be something wrong with VS itself not with my programming style :)

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 System.Media;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SoundPlayer sp = new SoundPlayer(#"1.wav");
SoundPlayer s = new SoundPlayer(#"2.wav");
private System.Windows.Forms.Label[] arLabel;
int temp = 10;
int help = 0;
int r = 0, b = 0, a, tem = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
f1();
}
private void arLabel_Click(object sender, System.EventArgs e)
{
Label myButton = (Label)sender;
if (myButton.Text == "")
{
myButton.ForeColor = System.Drawing.Color.Red;
sp.Play();
temp = 1;
myButton.Text = "x".ToString();
help = 0;
f3();
}
for (int i = 0; i < arLabel.Length; i++)
{
if (arLabel[i].Text != "x")
this.arLabel[i].ForeColor = System.Drawing.Color.Green;
}
s.Play();
//------------------------------------------------------------------------------------------------------------------------------------
if (temp == 1)
{
if (arLabel[6].Text == "" && arLabel[0].Text == "x" || arLabel[8].Text=="x"&&arLabel[6].Text=="")
arLabel[6].Text = "o".ToString();
else if (arLabel[4].Text == "")
arLabel[4].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[0].Text == arLabel[1].Text && arLabel[2].Text == "")//
arLabel[2].Text = "o".ToString();
else if (arLabel[0].Text == arLabel[2].Text && arLabel[1].Text == "")//
arLabel[1].Text = "o".ToString();
else if (arLabel[1].Text == arLabel[2].Text && arLabel[0].Text == "")//
arLabel[0].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[6].Text == arLabel[7].Text && arLabel[8].Text == "")
arLabel[8].Text = "o".ToString();
else if (arLabel[6].Text == arLabel[8].Text && arLabel[7].Text == "")
arLabel[7].Text = "o".ToString();
else if (arLabel[8].Text == arLabel[7].Text && arLabel[6].Text == "")
arLabel[6].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[3].Text == arLabel[4].Text && arLabel[5].Text == "")
arLabel[5].Text = "o".ToString();
else if (arLabel[3].Text == arLabel[5].Text && arLabel[4].Text == "")
arLabel[4].Text = "o".ToString();
else if (arLabel[5].Text == arLabel[4].Text && arLabel[3].Text == "")
arLabel[3].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[0].Text == arLabel[3].Text && arLabel[6].Text == "")
arLabel[6].Text = "o".ToString();
else if (arLabel[0].Text == arLabel[6].Text && arLabel[3].Text == "")
arLabel[3].Text = "o".ToString();
else if (arLabel[6].Text == arLabel[3].Text && arLabel[0].Text == "")
arLabel[0].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[1].Text == arLabel[4].Text && arLabel[7].Text == "")
arLabel[7].Text = "o".ToString();
else if (arLabel[1].Text == arLabel[7].Text && arLabel[4].Text == "")
arLabel[4].Text = "o".ToString();
else if (arLabel[7].Text == arLabel[4].Text && arLabel[1].Text == "")
arLabel[1].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[2].Text == arLabel[5].Text && arLabel[8].Text == "")
arLabel[8].Text = "o".ToString();
else if (arLabel[8].Text == arLabel[5].Text && arLabel[2].Text == "")
arLabel[2].Text = "o".ToString();
else if (arLabel[2].Text == arLabel[8].Text && arLabel[5].Text == "")
arLabel[5].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[0].Text == arLabel[4].Text && arLabel[8].Text == "")
arLabel[8].Text = "o".ToString();
else if (arLabel[0].Text == arLabel[8].Text && arLabel[4].Text == "")
arLabel[4].Text = "o".ToString();
else if (arLabel[8].Text == arLabel[4].Text && arLabel[0].Text == "")
arLabel[0].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[2].Text == arLabel[4].Text && arLabel[6].Text == "")
arLabel[6].Text = "o".ToString();
else if (arLabel[2].Text == arLabel[6].Text && arLabel[4].Text == "")
arLabel[4].Text = "o".ToString();
else if (arLabel[4].Text == arLabel[6].Text && arLabel[2].Text == "")
arLabel[2].Text = "o".ToString();
//------------------------------------------------------------------------------------------------------------------------------------
else
for (int i = 0; i < 9; i++)
{
if (arLabel[8].Text == "")
{
arLabel[8].Text = "o".ToString();
break;
}
else if (arLabel[7].Text == "")
{
arLabel[7].Text = "o".ToString();
break;
}
else if (arLabel[6].Text == "")
{
arLabel[6].Text = "o".ToString();
break;
}
//------------------------------------------------------------------------------------------------------------------------------------
else if (arLabel[i].Text == "")
{
this.arLabel[i].ForeColor = System.Drawing.Color.Red;
arLabel[i].Text = "o";
break;
}
}
}
temp = 0;
help = 1;
f3();
}
private void f1()
{
this.arLabel = new Label[9];
int b = 400;
int c = 40;
for (int i = 0; i < arLabel.Length; i++)
{
if (i == 3 || i == 6)
{
c = c + 200;
b =400;
}
this.arLabel[i] = new Label();
this.arLabel[i].Location = new System.Drawing.Point(b, c);
this.arLabel[i].Size = new System.Drawing.Size(200, 200);
this.arLabel[i].TabIndex = i;
this.arLabel[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.arLabel[i].Text = "".ToString();
arLabel[i].Click += new System.EventHandler(this.arLabel_Click);
this.Controls.Add(arLabel[i]);
this.arLabel[i].Font = new System.Drawing.Font("Modern No. 20", 120.74999F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.arLabel[i].ForeColor = System.Drawing.Color.Green;
arLabel[i].Visible = true;
this.arLabel[i].BackColor = System.Drawing.Color.Khaki;
this.arLabel[i].BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
b = b + 200;
}
}
private void f3()
{
if (arLabel[0].Text == arLabel[1].Text && arLabel[2].Text == arLabel[1].Text && arLabel[1].Text != "".ToString())
{
temp = 0;
f2();
}
if (arLabel[3].Text == arLabel[4].Text && arLabel[3].Text == arLabel[5].Text && arLabel[3].Text != "".ToString())
{
temp = 0;
f2();
}
if (arLabel[6].Text == arLabel[7].Text && arLabel[8].Text == arLabel[6].Text && arLabel[6].Text != "".ToString())
{
temp = 0;
f2();
}
// if (arLabel[0].Text == arLabel[3].Text && arLabel[0].Text == arLabel[6].Text && arLabel[6].Text != "".ToString())
// f2();
if (arLabel[0].Text == arLabel[3].Text && arLabel[0].Text == arLabel[6].Text && arLabel[6].Text != "".ToString())
{
f2();
temp = 0;
}
if (arLabel[1].Text == arLabel[4].Text && arLabel[4].Text == arLabel[7].Text && arLabel[7].Text != "".ToString())
{
temp = 0;
f2();
}
if (arLabel[2].Text == arLabel[5].Text && arLabel[8].Text == arLabel[2].Text && arLabel[2].Text != "".ToString())
{
temp = 0;
f2();
}
if (arLabel[0].Text == arLabel[4].Text && arLabel[4].Text == arLabel[8].Text && arLabel[8].Text != "".ToString())
{
temp = 0;
f2();
}
if (arLabel[2].Text == arLabel[4].Text && arLabel[6].Text == arLabel[4].Text && arLabel[4].Text != "".ToString())
{
temp = 0;
f2();
}
}
private void f2()
{
if (help == 1)
{
r++;
// label4.Text = r.ToString();
MessageBox.Show("THE is o");
f6();
}
else
{
b++;
// label5.Text = b.ToString();
MessageBox.Show("THE is x");
}
for (int i = 0; i < arLabel.Length; i++)
arLabel[i].Text = "".ToString();
f6();
}
private void f6()
{
Random number = new Random();
a = number.Next(0, 8);
this.arLabel[a].ForeColor = System.Drawing.Color.Green;
arLabel[a].Text = "o".ToString();
s.PlaySync();
}
}
}

Related

C# route example

I have a problem with my program. I have no other idea to make it.
Here is my attempt:
public static string routegen(string filename)
{
string[] directions = readwrite.linesread(filename);
int beginx = int.Parse(directions[0].Substring(0,1));
int beginy = int.Parse(directions[0].Substring(2));
string inwhich = "dodecagon";
int dbsquare = 0;
for (int i = 1; i < directions.Length; ++i)
{
if (inwhich == "dodecagon")
{
if (directions[i] == "N") ++beginy;
if (directions[i] == "S") --beginy;
if (directions[i] == "E") ++beginx;
if (directions[i] == "W") --beginx;
if (directions[i] == "WSW" || directions[i] == "ESE") inwhich = "triangletop";
if (directions[i] == "WNW" || directions[i] == "ENE") inwhich = "trianglebottom";
if (directions[i] == "NNE" || directions[i] == "SSE") inwhich = "triangleleft";
if (directions[i] == "NNW" || directions[i] == "SSW") inwhich = "triangleright";
}
if (inwhich == "square")
{
++dbsquare;
if (directions[i] == "N")
{
inwhich = "triangletop";
++beginy;
}
if (directions[i] == "S")
{
inwhich = "trianglebottom";
--beginy;
}
if (directions[i] == "W")
{
inwhich = "triangleleft";
--beginx;
}
if (directions[i] == "E")
{
inwhich = "triangleright";
++beginx;
}
}
if (inwhich == "triangletop")
{
if (directions[i] == "WNW")
{
--beginx;
inwhich = "dodecagon";
}
if (directions[i] == "ENE")
{
++beginx;
inwhich = "dodecagon";
}
if (directions[i] == "S")
{
//--beginy;
inwhich = "square";
}
}
if (inwhich == "trianglebottom")
{
if (directions[i] == "WSW")
{
--beginx;
inwhich = "dodecagon";
}
if (directions[i] == "ESE")
{
++beginx;
inwhich = "dodecagon";
}
if (directions[i] == "N")
{
//++beginy;
inwhich = "square";
}
}
if (inwhich == "triangleleft")
{
if (directions[i] == "NNW")
{
++beginy;
inwhich = "dodecagon";
}
if (directions[i] == "SSW")
{
--beginy;
inwhich = "dodecagon";
}
if (directions[i] == "E")
{
//++beginx;
inwhich = "square";
}
}
if (inwhich == "triangleright")
{
if (directions[i] == "NNE")
{
++beginy;
inwhich = "dodecagon";
}
if (directions[i] == "SSE")
{
--beginy;
inwhich = "dodecagon";
}
if (directions[i] == "W")
{
//--beginx;
inwhich = "square";
}
}
There are dodecagons and between them there are triangles and squares. The program gets the start coordinates from a text file first line ( ex. 1 1 ) and from the other lines it gets the directions ( there are 12 direction: N NNE ENE E ESE SSE S SSW WSW W WNW NNW N).
The task is to give back the final dodecagon coordinates, the number of the squares in the route and the number of the dodecagons which occured more than once.
I attached a picture with an example.
The directions: 1 1 ENE N N ENE E WSW D E SSE
Here is the picture: http://i.stack.imgur.com/ZnxIc.png
The solution: final coordinates: 3 1 squares: 2 decagons occured more than once: 0
Thank you for your help in advance!

DataTable clears itself after a for loop

I am using a data table to store some data. After the for loop check the length of the data table, the data table clears itself. There is no data in it. Can you advise why is it so ? This is part part of a report that is using the DevExpress reporting engine to create the reports.
DataTable ResultList = new DataTable();
ResultList.Columns.Add("range");
ResultList.Columns.Add("bpfrom");
ResultList.Columns.Add("bpto");
ResultList.Columns.Add("qualMemb");
ResultList.Columns.Add("qualTurn");
ResultList.Columns.Add("qualTurnPercent");
ResultList.Columns.Add("grMemb");
ResultList.Columns.Add("grTurn");
ResultList.Columns.Add("grTurnPercent");
ResultList.Columns.Add("totalamt");
ResultList.Columns.Add("giftlevel");
string[] fromrangelist = fromrange.Split(',');
string[] torangelist = torange.Split(',');
int j;
if (fromrange == "")
{
DataRow dr = ResultList.NewRow();
dr["range"] = 1;
dr["bpfrom"] = 0;
dr["bpto"] = 100000000; //biggest value
dr["giftlevel"] = "ALL";
ResultList.Rows.Add(dr);
}
else
{
for (j = 0; j < fromrangelist.Length; j++)
{
DataRow dr = ResultList.NewRow();
dr["range"] = j + 1;
dr["bpfrom"] = Convert.ToDecimal(fromrangelist[j]);
dr["bpto"] = Convert.ToDecimal(torangelist[j]);
dr["giftlevel"] = "level:" + (j + 1).ToString() + "(" + fromrangelist[j] + "-" + torangelist[j] + ")";
ResultList.Rows.Add(dr);
}
}
//Get ALL Total spend
decimal TotalSpend = Math.Round((from table1 in context.mmsspddtls
where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
&& table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
&& (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
&& (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
&& !table1.mmsspdstfg.Equals('V')
select new
{
totalamt = table1.mmsspdpram
}).Sum(s => s.totalamt), 2);
for (int i = 0; i < ResultList.Rows.Count; i++)
{
decimal fromvalue = Convert.ToDecimal(ResultList.Rows[i]["bpfrom"]);
decimal tovalue = Convert.ToDecimal(ResultList.Rows[i]["bpto"]);
ResultList.Rows[i]["totalamt"] = TotalSpend;
//Member Query that match the range
var MemberQuery = (from table1 in context.mmscrdlogs
where table1.mmscdlcpcd.Equals(cpcd) && table1.mmscdlbrcd.Equals(brcd)
&& table1.mmscdlstst.Equals('N') && table1.mmscdlbpis >= fromvalue && table1.mmscdlbpis <= tovalue
&& table1.mmscdleddt >= fromexpiredate && table1.mmscdleddt <= toexpiredate
select new
{
spendfr = fromvalue,
spendto = tovalue,
membercode = table1.mmscdlmbcd
}).Distinct();
//Count of member in the range
ResultList.Rows[i]["qualMemb"] = MemberQuery.Count();
//member total spend in the range
decimal qualTurn = 0;
var qualQuery = (from table1 in context.mmsspddtls
from table2 in MemberQuery.Where(s => s.membercode == table1.mmsspdmbcd)
where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
&& table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
&& (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
&& (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
&& !table1.mmsspdstfg.Equals('V')
select new
{
totalamt = table1.mmsspdpram
});
if(qualQuery.Count() > 0)
{
qualTurn = Math.Round(qualQuery.Sum(s => s.totalamt), 2);
}
ResultList.Rows[i]["qualTurn"] = qualTurn;
//member total spend% with total in the range
if (qualTurn == 0)
ResultList.Rows[i]["qualTurnPercent"] = 0;
else
ResultList.Rows[i]["qualTurnPercent"] = Math.Round((qualTurn / TotalSpend) * 100, 2);
var MemberQueryGR = (from table1 in context.mmsspddtls
from table2 in context.pxppludpxes.Where(s => s.pxppdxcpcd.Equals(table1.mmsspdcpcd) && s.pxppdxbrcd.Equals(table1.mmsspdbrcd) && s.pxppdxplcd.Equals(table1.mmsspdplcd))
from table3 in context.mmssyspars.Where(s => s.mmssypcpcd.Equals(table1.mmsspdcpcd) && s.mmssypbrcd.Equals(table1.mmsspdbrcd))
where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
&& table2.pxppdxpx01 / (table3.mmssypbpmt / table3.mmssyppont) >= fromvalue
&& table2.pxppdxpx01 / (table3.mmssypbpmt / table3.mmssyppont) <= tovalue
&& table1.mmsspdtxty.Equals("GR") && table1.mmsspdplna.Equals('2')
&& table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
select new
{
spendfr = fromvalue,
spendto = tovalue,
membercode = table1.mmsspdmbcd
}).Distinct();
//Count of GR member in the range
ResultList.Rows[i]["grMemb"] = MemberQueryGR.Count();
//GR member total spend in the range
decimal grTurn = 0;
if (MemberQueryGR.Count() > 0)
{
var grQuery = (from table1 in context.mmsspddtls
from table2 in MemberQueryGR.Where(s => s.membercode == table1.mmsspdmbcd)
where table1.mmsspdcpcd.Equals(cpcd) && table1.mmsspdbrcd.Equals(brcd)
&& table1.mmsspdtxdt >= fromtxdate && table1.mmsspdtxdt <= totxdate
&& (table1.mmsspdtxty.Equals(null) || table1.mmsspdtxty.Equals("") || table1.mmsspdtxty.Equals("PS") || table1.mmsspdtxty.Equals("PE") || table1.mmsspdtxty.Equals("PR"))
&& (table1.mmsspdplna.Equals('1') || table1.mmsspdplna.Equals('5') || table1.mmsspdplna.Equals('P') || table1.mmsspdplna.Equals('T') || table1.mmsspdplna.Equals('A') || table1.mmsspdplna.Equals('L'))
&& !table1.mmsspdstfg.Equals('V')
select new
{
totalamt = table1.mmsspdpram
});
if(grQuery.Count() > 0)
{
grTurn = Math.Round(grQuery.Sum(s => s.totalamt), 2);
}
}
ResultList.Rows[i]["grTurn"] = grTurn;
//member total spend% with total in the range
if (grTurn == 0)
ResultList.Rows[i]["grTurnPercent"] = 0;
else
ResultList.Rows[i]["grTurnPercent"] = Math.Round((grTurn / TotalSpend) * 100, 2);
}
DataSource = ResultList;
}
Please advise. Thanks

Timer pause without thread.sleep

I try make timers without thread.sleep to stop lag so any ideas how to make this code without thread.sleep but it stop 20ms. Thanks for help! :) And sorry my bad english.
Timer BulletTimer = new Timer();
BulletTimer.Tick += (s, args) => BulletKillPlayer(Bullet);
BulletTimer.Interval = 20;
BulletTimer.Start();
private void BulletKillPlayer(Bullet)
{
bool pX, pY, pD1, pD2, nX, nY, nD1, nD2;
pX = false; pY = false; pD1 = false; pD2 = false; nX = false; nY = false;
for (int i = 1; i < 20; i++)
{
if (User.Range >= i)
{
Thread.Sleep(20);
foreach (MapItem MapItem in MapItem.Boxs.Values)
{
if (MapItem.X == Bullet.X && MapItem.Y == Bullet.Y + i && !pX) { pX = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X && MapItem.Y == Bullet.Y - i && !pY) { pY = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X + i && MapItem.Y == Bullet.Y && !nX) { nX = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
if (MapItem.X == Bullet.X - i && MapItem.Y == Bullet.Y && !nY) { nY = BreakBox(Bullet, MapItem); KillPlayer(Bullet, MapItem); }
}
}
}
}

C# Windowsform - How made this code better (runtime created textbox and label) change too many ifs to repetition structure

i have code that its working but its not as i would like.
in runtime i create many new textbox/labels so i had to use 10 ifs to check each "future" textbox have textlength = 0 and != null
i would like to use repeat structure like for or while, dont know if its possible.
For example, if i create more textbox/labels will be impossible have really big code.
See my code:
private void cadeiaapagarcampos(TextBox _text, EventArgs e)
{
if (_text.Text == "")
{
Label lblAcessorio2 = (Label)gpbCategoria.Controls.Find("lblAcessorio2", false).FirstOrDefault();
TextBox txtAcessorio2 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio2", false).FirstOrDefault();
Label lblAcessorio3 = (Label)gpbCategoria.Controls.Find("lblAcessorio3", false).FirstOrDefault();
TextBox txtAcessorio3 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio3", false).FirstOrDefault();
Label lblAcessorio4 = (Label)gpbCategoria.Controls.Find("lblAcessorio4", false).FirstOrDefault();
TextBox txtAcessorio4 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio4", false).FirstOrDefault();
Label lblAcessorio5 = (Label)gpbCategoria.Controls.Find("lblAcessorio5", false).FirstOrDefault();
TextBox txtAcessorio5 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio5", false).FirstOrDefault();
Label lblAcessorio6 = (Label)gpbCategoria.Controls.Find("lblAcessorio6", false).FirstOrDefault();
TextBox txtAcessorio6 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio6", false).FirstOrDefault();
Label lblAcessorio7 = (Label)gpbCategoria.Controls.Find("lblAcessorio7", false).FirstOrDefault();
TextBox txtAcessorio7 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio7", false).FirstOrDefault();
Label lblAcessorio8 = (Label)gpbCategoria.Controls.Find("lblAcessorio8", false).FirstOrDefault();
TextBox txtAcessorio8 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio8", false).FirstOrDefault();
Label lblAcessorio9 = (Label)gpbCategoria.Controls.Find("lblAcessorio9", false).FirstOrDefault();
TextBox txtAcessorio9 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio9", false).FirstOrDefault();
Label lblAcessorio10 = (Label)gpbCategoria.Controls.Find("lblAcessorio10", false).FirstOrDefault();
TextBox txtAcessorio10 = (TextBox)gpbCategoria.Controls.Find("txtAcessorio10", false).FirstOrDefault();
if (txtAcessorio2 != null && txtAcessorio2.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio2);
gpbCategoria.Controls.Remove(lblAcessorio2);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio3 != null && txtAcessorio3.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio3);
gpbCategoria.Controls.Remove(lblAcessorio3);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio4 != null && txtAcessorio4.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio4);
gpbCategoria.Controls.Remove(lblAcessorio4);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio5 != null && txtAcessorio5.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio5);
gpbCategoria.Controls.Remove(lblAcessorio5);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio6 != null && txtAcessorio6.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio6);
gpbCategoria.Controls.Remove(lblAcessorio6);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio7 != null && txtAcessorio7.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio7);
gpbCategoria.Controls.Remove(lblAcessorio7);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio8 != null && txtAcessorio8.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio8);
gpbCategoria.Controls.Remove(lblAcessorio8);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
if (txtAcessorio9 != null && txtAcessorio9.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio9);
gpbCategoria.Controls.Remove(lblAcessorio9);
btnSalvar.Focus();
if (test != 1)
{
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
}
}
Would something like this be what you are looking for? If you have more labels just update iAcessorioContar and it will automatically check them as well as long as you name the labels incrementally.
private void cadeiaapagarcampos(TextBox _text, EventArgs e)
{
if (_text.Text == "")
{
int iAcessorioContar = 10;
for (int iContador = 2; iContador <= iAcessorioContar; iContador++) {
Label lblAcessorio = (Label)gpbCategoria.Controls.Find("lblAcessorio"+iContador, false).FirstOrDefault();
TextBox txtAcessorio = (TextBox)gpbCategoria.Controls.Find("txtAcessorio"+iContador, false).FirstOrDefault();
if (txtAcessorio != null && txtAcessorio.TextLength == 0)
{
gpbCategoria.Controls.Remove(txtAcessorio);
gpbCategoria.Controls.Remove(lblAcessorio);
btnSalvar.Focus();
if (test != 1) {
n--;
t++;
if (n >= 1 && n <= 10)
{
testeapagar();
test = 1;
}
}
}
}
}
}
You could add the controls in pairs (label and textbox) to a list when you create them so you do not have to search the controls collection which can cost a lot of time.
You can then simply loop the list.

WPF Tree View event

I want to make an application in WPF same as windows application. When i use treeview event in wpf i did not find any event similar to treeview_NodeMouseClick of WIndows Application.
//Windows Application Code
private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != null)
{
GetAllchield(e.Node, e.Node.Level);
}
}
// GetAllchield
public void GetAllchield(TreeNode clickednode, int indexDepth)
{
if (clickednode.Nodes.Count > 0 && !clickednode.IsExpanded)
{
clickednode.Collapse();
return;
}
string[] FullPath = clickednode.FullPath.Split('\\');
string rootnode = FullPath[0].ToString();
//get all market for root event type
int eventTypeID = DictionaryAllActiveEventTypes[rootnode];
string[] allMarkets = GetAllMarketForEventID(eventTypeID);
//selecting unque chield node and populating in tree
for (int i = 0; i < allMarkets.Length; i++)
{
if (allMarkets[i].Contains(clickednode.Text))
{
string[] marketDetails = allMarkets[i].Split('~');
int marketID = Convert.ToInt32(marketDetails[0]);
string MarketName = marketDetails[1].ToString();
string MarketStatus = marketDetails[3].ToString();
string EventHeirarchy = marketDetails[6].ToString();
string Menupath = marketDetails[5].ToString();
string[] Arrmenupath = Menupath.Trim(':').Split('\\');
string chieldText = "";
if (indexDepth == 0)
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
else
{
if (Arrmenupath[Arrmenupath.Length - 1] == clickednode.Text)
chieldText = MarketName;
else
{
if (allMarkets[i].Contains(clickednode.Text) && allMarkets[i].Contains(clickednode.Parent.Text) && allMarkets[i].Contains(rootnode))
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
}
//check whether node is already added
//if (chieldText.Contains("MiWay"))
//{ }
if (!string.IsNullOrEmpty(chieldText))
{
if (clickednode.Nodes.Count >= 1)
{
bool doesNodeAlreadyExist = false;
foreach (TreeNode node in clickednode.Nodes)
{
if (node.Text == chieldText)
{
doesNodeAlreadyExist = true;
break;
}
}
if (!doesNodeAlreadyExist)
{
clickednode.Nodes.Add(chieldText);
}
}
else
{
clickednode.Nodes.Add(chieldText);
}
}
}
}
clickednode.Expand();
}
I want to use same as in WPF. Please help me or if you get it.
You can use MouseLeftButtonDown and check sender argument or use SelectedItemChanged

Categories

Resources