Sorry I'm still learning C#, but here's my code and I need help. Basically here's how it is:
The first part of my code went to a settings window where the textbox displays "0%" and my code enters a value of 10 to it.
public void SetDefaultAmount(int amt)
{
Settings.EnterAmt(amt);
}
public void EnterAmt(int amt)
{
string amount = amt.ToString();
Settings.TextBox.Text = amt;
}
So now in setting window, the textbox displays "10%".
My next code goes to a User window where it displays the same textbox and the default value, so the textbox displays "10%" correctly.
I need a code that grabs the textbox value in the Users page, and compare it with the value from the settings page to check if they match. But when i run my code, it gives me the error of "Expected: 10, But was: "string.Empty". How can I fix this? And any better way of streamlining my code? thanks!
This is what my 2nd code is currently, that fails:
**Check if value is 10 (amt = 10)
public void CheckValue(int amt)
{
string amount = amt.ToString();
string actualval = UserPage.GetActualVal();
Assert.AreEqual(amount, actualval, "value did not match");
}
public string GetActualVal()
{
return UserPage.Textbox.Text;
}
It looks like you're comparing amt, an integer, to actualval, a string. Try Assert.AreEqual(amount, actualval).
After another hour of checking my code, it actually works. the problem turned out to be the automation id of the textbox in the Users page, which was changed without my knowledge so i corrected it already. Thanks for checking and i'd just probably try streamline my code.
Related
So i am creating an x and o's game and i have a label which shows who's turn it is ( WhosTurnLabel) but when i try to format their chosen name into a string it only assigns the string to the label and not the formatted version.
WhosTurnLabel.Text = $"{playerXName}'s Turn";
playerXName is taken from a text box input.
game.playerXName = PlayerXName.Text;
When i am debugging and hover over the $ it shows the string i want the label to be, but when i check the form and hover my mouse over WhosTurnLabel.Text it only shows "'s Turn".
I assign the names to variables in another class then open that form up here:
Game game;
game = new Game();
game.playerXName = PlayerXName.Text;
game.playerOName = PlayerOName.Text;
game.Show();
Thanks in advance for any help.
p.s: im still learning c# and really i need someone to check my programs code, i think there may be a few hiccups
Have you tried passing in the values for playerXName and playerOName as parameters to the Game form constructor? This worked fine for me:
public partial class Game : Form
{
public string playerXName,
playerOName;
public Game(string player1, string player2)
{
InitializeComponent();
playerXName = player1;
playerOName = player2;
WhosTurnLabel.Text = $"{playerXName}'s Turn";
}
}
I just assigned the label text attribute in the constructor, however, playerXName and playerOName are fields in the object and should be accessible to any methods and event handlers within the Game class. Hope this helps!
Looks like the value, at least at the point the string is created is null, empty, or undefined.
Something else to look into, though I am unsure without seeing more of the code, but should
WhosTurnLabel.Text = $"{playerXName}'s Turn";
be
WhosTurnLabel.Text = $"{game.playerXName}'s Turn";
I don't know it would have compiled or not thrown an error in that case, but just something that stood out to me.
But to sum it up, the string is definitely empty or null, at least at the point the string is created.
Add a Game_Shown event and put WhosTurnLabel.Text = $"{playerXName}'s Turn"; in there
I see here lot of similar question, but I still not find answer that help me in situation.
I have two frame(lets say FrameChild), one is "in" another(practically FrameChild is in this frame, lets say FrameMain).
When I insert all parameters in FrameChild and tap on button witch is on bottom of FrameMain I call method that return string...
Now when i get string i need to change textbox text in FrameChild
I have tray many way.
First idea was something like:
FrameChild frm = new FrameChild;
frm.textbox.text = "somestring";
But nothing happen.
Than i thing use some property.
in FrameChield:
public string setTicNo
{
set
{
textBox.Text = value;
}
}
in FrameMain:
FrameChild frm = new FrameChild;
frm.setTicNo = "somestring";
When i debbuging I get value, but textbox still is empty...
On the end I try to bind textbox text on setTicNo;
public string setTicNo
{
get
{
return setTicNo;
}
set
{
setTicNo = value;
}
}
Xaml:
Text = {Binding setTicNo, Mode=TwoWay,UpdateSourceTrigger=Explicit}
(here i try use more bindings, but every time i get infinite loop.
Please help , I not have more ideas..
Thanx
Did you try building a single view model and bind it to both frames, if it was passed by ref which is the default it will change the value once you do.
A side note implement a INOTIFYPROPERTYCGANGED in the View model
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.
I'm calling a public method from another class. It takes in a List as a parameter, and goes through the list printing out each item into a text field. The problem is the text field is remaining empty!. I've checked that the list is populated by outputing the item to the console before I put it into the text box, and the text is coming up fine there.
The list contains strings, and should output each string to the textfield followed by a semi colon.
This is the method which is being called:
public void fillAttachment(List<string> attachList)
{
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
txtAttach.Text += attachList[i] + ";";
}
}
I would solve it in this way:
foreach(var attach in attachList)
{
Console.WriteLine(attach);
txtAttach.AppendText(string.Format("{0};", attach));
}
Setting the text property on a text box and it not displaying could be one of the following:
You are not looking at the same control as you are setting the text in
Could you have instantiated a second copy of the form object and it is this form that you are setting the txtAttach text property in?
Could the control that you are expecting to be populated be a different one? Right click the text box that you want the text to appear in click properties and check the name.
Something else is clearing the textbox after you set it
Right click the txtAttach.Text and click Find All References, this will show you all the places that the Text property is referenced - written and read - in your project. This is a very useful way to locate other interaction with this control.
Fomatting is making the text box appear empty
Is the Font too small, or in the same colour as the background. Can you select the text in the text box?
The easiest way to test all of the above is to create a new text control on your form with a different name, change your code to populate it, check that it is indeed populated, then replace the old one.
As an aside, you could also reduce the code with a single line as follows:
public void fillAttachment(List<string> attachList)
{
txtAttach.Text = String.Join(";", attachList.ToArray());
}
Although this obviously skips out the console write line function.
Not sure why yours doesn't work but I would have done it like this...
public void fillAttachment(List<string> attachList)
{
string result = "";
//OR (if you want to append to existing textbox data)
//string result = txtAttach.Text;
for (int i = 0; i < attachList.Count; i++)
{
Console.WriteLine("List: " + attachList[i]);
result += attachList[i] + ";";
}
txtAttach.Text = result;
}
Does that work for you? If not then there is something else very wrong that is not obvious from your code
I am working on C#.net windows application. i am filling combobox on my winform by using follows.
cmbEMPType.DataSource = objEntityManager.EmployeeTypes();
cmbEMPType.DisplayMember = "EMPTypeName";
cmbEMPType.ValueMember = "EMPTypeId";
where objEntityManager.EmployeeTypes(); in the manager method that gets the List from Linq to sql server. this is working fine.
but as i select the item form combo box, and clicked the button then in the button click event i am getting cmbEMPType.SelectedValue as EmpType return type rather than its Id. why should this? I don't want to create one more EmpType object. need simple selected value. also can not keep faith with SelectedIndex. it may varies for item each time.
**Edited**
public List<EMPType> EmployeeTypes()
{
List<EMPType> EMPTypeList = null;
try
{
if (CommonDataObject.dataContext.EMPAllTypes.Any())
{
EMPTypeList = CommonDataObject.dataContext.EMPAllTypes.ToList();
}
return EMPTypeList;
}
catch
{
return EMPTypeList;
}
}
Edited
private void btnSave_Click(object sender, EventArgs e)
{
iEMPTypeId = cmbEMPType.SelectedValue;
}
here I must get inte. but asking of create the EMPType object.
This is the correct and expected behavior, you can't change it.
SelectedValue should return the type of the property, e.g. if EMPTypeId is integer it should return integer - please post more code so that we can try figuring out why you get different return value.
If by any chance you're using SelectedItem then have such code to get the ID:
int selectedID = (cmbEMPType.SelectedItem as EmpType).EMPTypeId;
To handle cases when there's nothing selected:
object oSelectedEmp = cmbEMPType.SelectedItem;
int selectedID = oSelectedEmp == null ? -1 : (oSelectedEmp as EmpType).EMPTypeId;
The problem is the sequence of your code. Please remove the first line code to the last line. You will get an int value (iEMPTypeId) from cmbEMPType.SelectedValue.
cmbEMPType.DisplayMember = "EMPTypeName";
cmbEMPType.ValueMember = "EMPTypeId";
cmbEMPType.DataSource = objEntityManager.EmployeeTypes();
iEMPTypeId = cmbEMPType.SelectedValue
Another option is to override the toString function in your EMPType class. As Edwin de Koning stated "If no ValueMember is specified it gives a ToString() representation."
Something like (I cant test it at the moment):
public override string ToString()
{
return this.ID;
}
You can check out this article: http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx