Working with a WPF application.
I am very much wonderin if it is possible to get a function luke the examle below into a class function (im not yet very experianced with C#).
private void counter01_Tick(object sender, EventArgs e)
{
if (counter01Ticks > 0)
{
//subtract 1 each time
counter01Ticks--;
//subtrack 1 secon each time
counter01Span = counter01Span.Subtract(TimeSpan.FromSeconds(1));
//update the progressbar
progBar01.Value++;
//get the % to show
progBar01Text.Text = Convert.ToString(Math.Round(((progBar01.Value / progBar01.Maximum) * 100), 0)) + "%";
//Label1 will show the count down.
string countDown = counter01Span.ToString();
TimeRemain01.Content = countDown;
}
else
{
counter01.Stop();
resetCounter01();
WarningMessage msgWarnOne = new WarningMessage();
msgWarnOne.warnMessage.Text = Properties.Settings.Default.msgScout01;
msgWarnOne.ShowDialog();
}
}
It is just a part of a counter. but i want to add more counters to my application later on.
Therefore i marked all the parameters with a number (01) in my code.
So what i do not want to do, i copy-paste the code and change the number for every counter, but rather have the number as a input number or something.
Would that be possible?
if i9 would understand it for this small part of code, i think i will be able to do it with the other parts too (above is only the tick form a counter).
#Users that user the answer below:
http://www.c-sharpcorner.com/uploadfile/mahesh/user-control-in-wpf/
Has helped me understand it better and migh be usefull to read too.
Yes you can put all of this (together with your XAML declaration) into a WPF-user control and put multiple of these into other Windows/Controls/...
Just look at the tutorial I linked in - should explain everything you need.
You can use method Control.FindControl;
Another approach is to make user control - and therefore you will work with only 1 array of controls in every counter_Tick
Related
While working on a small app that pulls test cases, runs, and results from an SQL Server Database, I encountered a dilemma in my methodology for attempting to create dynamic controller names in a TableLayoutPanel in WinForms. I am creating the rows dynamically when the user chooses the particular test case, and from there the TableLayoutPanel will open another window with the test steps preloaded and two radio buttons to indicate whether or not the test passed. My issue is that when I select one of the radio buttons on the right of the step, I get the same console read every single time. I need to be able to determine which exact radio button the user has pressed so I can therefore determine what row it's in and subsequently what test either passed or failed. My main code is as follows:
FormManualTest.cs (section when adding to the TableLayoutPanel)
private void addRowToolStripMenuItem_Click(object sender, EventArgs anotherEvent)
{
tableLayoutTest.RowStyles.Clear(); // Clear row styles to ensure a clean start when adding to the TableLayoutPanel
List<RadioButton> listOfRadioControls = new List<RadioButton>(); // Create array of radio buttons
List<UserCustomStep> listOfStepControls = new List<UserCustomStep>(); // Create array of custom controls
for (int i = 0; i < 5; i++)
{
UserCustomStep step = new UserCustomStep(Counter, "Step: " + i + " Push the button to elicit a response."); // Creates new user custom step control instance
RadioButton pass = new RadioButton();
pass.Text = "Pass";
pass.AutoSize = true;
RadioButton fail = new RadioButton();
fail.Text = "Fail";
fail.AutoSize = true;
fail.Margin = new Padding(3,3,20,3); // Needed to see the fail button without having to scroll over
listOfStepControls.Add(step); // Add step to UserCustomStep array
listOfRadioControls.Add(pass); // Add radio buttons to the RadioButton array
listOfRadioControls.Add(fail);
listOfRadioControls[i * 2].CheckedChanged += (s, e) => // Subscribes the pass radio button to listen for when a user has clicked on it
{
Console.WriteLine("Pass " + i + " was clicked");
};
listOfRadioControls[(i * 2) + 1].CheckedChanged += (s, e) => // Subscribes the fail radio button to listen for when a user has clicked on it
{
Console.WriteLine("Fail " + i + " was clicked");
};
tableLayoutTest.Controls.Add(listOfStepControls[i], 0, i); // Adds CustomStep to first column
tableLayoutTest.Controls.Add(listOfRadioControls[i*2], 1, i); // Adds Pass Radio Button to second column
tableLayoutTest.Controls.Add(listOfRadioControls[(i * 2) + 1], 2, i); // Add Fail Raido Button to third column
Counter++; // Increment couter to add subsequent steps underneath the previous ones.
}
}
Screenshots of App with Console Readout:
After Test Case Has Been Clicked and Radio Button Has Been Pressed
(From clicking this I would expect the console to read "Pass 1 was clicked")
Console Read:
Click Fail Button:
(I know from this image below that since the Pass button doesn't remain clicked I'm somehow using the same controller for all 5 of them)
Console Read
So from all of these issues that I've been presented with, I know that I'm somehow using the same controller for all 5 instances regardless of the fact that I'm storing everything in a controller array and grabbing from there. The for loop will have to be converted to a for each loop later, but that still doesn't solve my issue. I believe that if I could say something like:
RadioButton (pass+id) = new RadioButton();
or something similar while looping through to dynamically create the name for the controls, then each one would be a completely separate control and I could go from there. Any help would be greatly appreciated! I come from a heavy web background so my normal skills to remedy this in JS land aren't coming in handy as of right now. Thanks again for the assistance.
The Name property is optional, you don't need to specify it and it doesn't need to be unique. You can use property Tag for your own purpose (you can assign there ID or event instance of some object).
However you can also create your own control/usercontrol which encapsulate the whole row, and you can declare your own properties exactly for your purpose.
I have a list of objects which have been taken form a stored procedure. I then take this list pass it through a method I created which wraps it in HTML and then out puts to the webform.
I'm looking to create paging for this list. I have stored the list in a session and have two buttons ( next and previous ) I'm using LINQ to skip and take form the list on each button click.
Button Click
protected void lnkNext_Click(object sender, EventArgs e)
{
ListOfAdvertAndUsers = (List<Advert_User>)Session["list"];
var list = from item in ListOfAdvertAndUsers select item;
var pgNo = 1;
var pgRec = 6;
list = list.Skip(pgRec * pgNo).Take(pgRec).ToList();
ListOfAdvertAndUsers = list.ToList();
PopulateData(ListOfAdvertAndUsers);
}
I don't have enough rep to make a comment so I will just post my comments as an answer.
I presume that you are using Asp.Net WebForms, so how are you outputting the final html, Response.Write or setting Text on a Literal?
But if you are using WebForms, would it not be easier to use some of the builtin Controls. I donĀ“t exactly know how your data looks like, but maybe a GridView would be a good solution as it has builtin support for paging. Here is an example from MS on how it can be used: http://www.asp.net/web-forms/overview/presenting-and-managing-data/model-binding/sorting-paging-and-filtering-data
Not a direct answer, but you're overwriting queries and lists, which seem to convolute the code unnecessarily. Your method could be simplified to:
protected void lnkNext_Click(object sender, EventArgs e)
{
ListOfAdvertAndUsers = (List<Advert_User>)Session["list"];
var pgNo = 1; // don't you need to increment the page number somewhere?
var pgRec = 6; // should this be defined somewhere other than this method?
var page = ListOfAdvertAndUsers.Skip(pgRec * pgNo).Take(pgRec);
PopulateData(page);
}
Perhaps that will help you determine what the real problem is...
EDIT - Sorry folks, i guess i wanted to "obscure" my work code too much... i don't know why it got so many downvotes but anyway. see below for update/edit with actual code.
I am trying to insert a piece of text into an existing section of a line (<data) which resides at the beginning of a line in my RichTextBox control. However, whenever i do that in the following manner:
private void AddSelectedIntellisense(object sender, EventArgs e)
{
ToolStripItem x = sender as ToolStripItem;
int cursorpos = this.txt_Body.SelectionStart;
string final = this.txt_Body.Text.Insert(cursorpos, x.Text);
//final var at breakpoint is equal to "<data log=\"Original\""
//then i assign it/that to the RTB.Text
this.txt_Body.Text = final;
//when checked with breakpoint, this.txt_Body.Text is equal to
//"log=\"\"<data log=\"Original\""
this.txt_Body.SelectionStart = cursorpos + x.Text.Length;
}
I am thinking that it is the < character that is causing issues when i assign the string to the .Text property (because if i replace the < with a [ in my logic, no problems), but i don't know how to fix it... if you could help me i would really appreciate it.
I also checked all of the indexes manually and they all lign up perfectly... so i don't know why the RTB.Text value is different than the string but if someone knows please tell me.
Cheers!
You are first setting:
txt = this.RTB1.Text.Substring(starts, length);
Then on the next line you are replacing the value of txt:
txt = this.RTB1.Text.Insert(index,"log='test'></data>");
You are probably looking to concatenate the strings:
string txt = this.RTB1.Text.Substring(starts, length);
txt += this.RTB1.Text.Insert(index,"log='test'></data>");
this.RTB1.Text = txt;
Ok folks... i suppose i'll give it to Aaron, since it's like somewhat related and nobody else answered.
The answer was:
I am using the RTB.On_TextChanged event to fire off the intellisense based on a condition. However, because i am also setting text the RTB.Text value within the Intellisense, the condition became true twice and added the specific text twice. So i setup a flag when i add intellisense text and check it in the on_textchanged event.
Cheers and sorry for the confusion.
If I have a candle series drawn on the teechart, and I change the last candle's CloseValue, the candle does not automatically update on the chart. I don't really want to invalidate the whole chart to show it because if there are a lot of candles, that's slow. I thought the DrawValue method of the Candle series would solve this, but it seems that it actually redraws the whole chart as well. Is there a way to update/redraw on screen just that one candle?
I know when I was doing this in Delphi 5 the candle seemed to update without redrawing the whole chart - although maybe the whole chart updates were fast enough that it just seemed that way. In general, it seems that C#'s Teechart draws are 3 to 5 times slower than the Delphi 5 VCL Teechart's draws...
I think you can use the method that allow refresh only the series, RefreshSeries, as do in next line of code:
candle1.RefreshSeries();
Could you tell me, if it works for you? If you have any problems please let me know.
Update information to answer next question:
Ok, I guess I am not explaining myself well here, Sandra. Let me try
again. Let's say I have a chart that has only one series - 30,000
candles. Let's say I show ALL candles on the chart. Redrawing all 30K
candles takes time. Let's say the code changes only ONE candle - the
last one. Is there any way to just repaint that little portion of the
chart that has the new candle, and not repaint the whole chart in
order to show the new change? I tried Invalidate function passing it
the candle's rectangle, but it seems that Invalidate() and
Invalidate(rect) produce exactly same results.
Can you tell us which version of TeeChart are you using?. In the other hand, I have made a simple code where I have modified the CloseValue and I have released that isn't necessary repaint, redraw or refresh Series because the value is updated automatically. I have made the test using last version of TeeChartFor.Net Build number [4.1.2012.01312] and next code:
Steema.TeeChart.Styles.Candle candleSeries1;
Random r;
double tmpOpen;
double tmpClose;
int count;
DateTime dt;
TimeSpan ts;
private void InitializeChart()
{
tChart1.Aspect.View3D=false;
tChart1.AutoRepaint = false;
r = new Random();
candleSeries1.Clear();
candleSeries1.XValues.DateTime = true;
candleSeries1.GetHorizAxis.Labels.Angle = 90;
count = 0;
dt = DateTime.Today;
ts = TimeSpan.FromDays(1);
candleSeries1.Pen.Visible = false;
for (int t=0;t<30000;t++)
{
tmpOpen = r.Next(100);
tmpClose = tmpOpen - r.Next(100);
++count;
candleSeries1.Add(dt,tmpOpen,tmpOpen + r.Next(50),
tmpClose -r.Next(50),tmpClose);
dt += ts;
}
tChart1.AutoRepaint = true;
}
private void button1_Click(object sender, EventArgs e)
{
tmpOpen = r.Next(100);
tmpClose = tmpOpen - r.Next(100);
candleSeries1[candleSeries1.LastVisibleIndex].Close = tmpOpen;
}
Thanks,
I will sound like absolutely a noob, but I am so stressed out that I am not able to do any research properly.
Basically I got 127 road names in a list, and I want to display them one by one in a random sequence in a label and I will answer them if its right one increment to green box if its wrong then increment to yellow box but at same time it will display me right answer in a label and then on pressing submit button it will take me to next road name, here is what I have done until now,
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
RL = RoadLocationNames();
i = 1;
red = 1;
y = 1;
}
List<KeyValuePair<string, string>> RL;
int i,red, y;
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
int r = random.Next(RL.Count);
lbLocation.Text = RL.ElementAt(r).Key;
if (tbRoad.Text.ToLower() == RL.ElementAt(r).Value.ToLower())
{
Green.Text = i.ToString();
i++;
RL.Remove(RL.ElementAt(r));
}
else
{
label3.Text = RL.ElementAt(r).Value.ToString();
Red.Text = i.ToString();
i++;
}
Yellow.Text = y.ToString();
y++;
}
public List<KeyValuePair<string, string>> RoadLocationNames()
{
List<KeyValuePair<string, string>> RLNs = new List<KeyValuePair<string,string>>();
RLNs.Add(new KeyValuePair<string, string>("Road Name", "Location Name"));
return RLNs;
}
Now My internet is so slow that I barely can upload any screenshot of my form but I will try to.. I got exam tomorrow and I want to do preparation using this app, but I am not sure about,
How to display a road name instead of (road name and its answer which is what my code is doing now) it could be because I am doing everything in buttton submit, I need help with logic and code (sorry but am in rush)
Yes, it's because you're doing everything in your submit. I suggest you split your code into the following:
Initialisation - create the list of road names and locations, then run setup (next step).
Setup - Clear the user's current answer, pick a road name/location pair randomly and display the question part on the form.
User submission - check answer, increment appropriate counter, if the answer is right run setup again, otherwise display what the answer should have been.
Initialisation can be done in your form constructor. Setup should be a private method. User submission should be your click handler.
(It looks like you're incrementing i for both right and wrong answers which is probably incorrect. This is why you pick better variable names than a single letter to make mistakes like that more obvious.)