values a re not being displayed in TextBox - c#

in this program, when the Recall button (recallBtn_Click()) is clicked, it calls a method (that calculates directions) from another class which should then call the showPath() method. the show path method should then display its output in a textBox. But the values don't show even though i can see from debugging that the values are being sent to the text box. can anybody tell me where i went wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
storeRetSelect.SelectedIndex = 0;
PrioritySelect.SelectedIndex = 0;
}
public void showPath(List<PathFinderNode> mPath)
{
var T = new Form1().directionsTextBox;
foreach (PathFinderNode node in mPath)
{
if ((node.X - node.PX) > 0) { T.Text += "Right" + System.Environment.NewLine ; }
if ((node.X - node.PX) < 0) { T.Text += "Left" + System.Environment.NewLine; }
if ((node.Y - node.PY) > 0) { T.Text += "UP" + System.Environment.NewLine; }
if ((node.Y - node.PY) < 0) { T.Text += "Down" + System.Environment.NewLine; }
}
}
private void recallBtn_Click(object sender, EventArgs e)
{
var path = new pathPlan();
string desigString = inputTextBox.Text;
int[] desig = new int[3];
for (int i = 0; i < desigString.Length; i++) { desig[i] = (int)char.GetNumericValue(desigString[i]); }
path.Recall(desig[1], desig[2], (-1) * desig[0]);
}
}

With this line you are initialising a new object and get the reference of the textbox there.
var T = new Form1().directionsTextBox;
But I assume you want to use the textbox of the form which is allready open. Change the line to the following to access the textbox of the current object.
var T = this.directionsTextBox;

Related

Injection of radiobuttonlist selected value into class

I want to create a webform with 4 types of phones like: LG, xiaomi, samsung and iphone. They will be in an Array and I will insert them into dynamic radiobuttonList in the page init.
Also, the user will have a textbox where he will put an amount of money he has. The user will also have a button that will calc if he have the budget for the selected phone from the list.
After the user selects the phone, write in the budget and press the button
he will get "in budget" or "not enough budget".
The class have property of the array to insert the all the array from the page init and will have 2 functions:
One function will take the budget number > will go to the 2nd function that will see the selected phone and the budget > do its calcs and return the result into the 1st func that will give the feedback.
Now where I am stuck:
if the class isnt made global - and i put it in init or in button click - it wont work, so im looking for a way to make it work but without putting it global
so far i managed to inject the selected value into a class property and than compare - but i want to know if there is a way that this can happen with array inside class property
maybe if anyone can help me out and refer me to a guide where i can learn more about this subject (how inject selected value into function of a class and etc) ill be glad! as everything i see is C# with console but i work with ASP.NET WEB APPLICATION (.netframework)
enter code here
namespace gfjsr{
public partial class WebForm1 : System.Web.UI.Page{
phone InsertUserInfo = new phone();
protected void Page_init(object sender, EventArgs e){
string[] myArr = new string[] { "samsung", "IPHONE", "XIAOMI","LG"};
RadioButtonList phoneList = new RadioButtonList();
phoneList.ID = "radioList";
for (int i = 0; i< myArr.Length; i++)
{
ListItem li = new ListItem();
li.Text = myArr[i];
li.Value = i.ToString();
phoneList.Items.Add(li);
}
Panel1.Controls.Add(phoneList);
Label budgetLb = new Label();
budgetLb.ID = "budglb";
budgetLb.Text = "write your budget";
Panel1.Controls.Add(budgetLb);
TextBox insertBudg = new TextBox();
insertBudg.ID = "budgTxt";
Panel1.Controls.Add(insertBudg);
Button myBtn = new Button();
myBtn.ID = "btn1";
myBtn.Click += new EventHandler(btn1_click);
myBtn.Text = "result";
Panel1.Controls.Add(myBtn);
Label Labelfeedback = new Label();
Labelfeedback.ID = "feedback";
Labelfeedback.Text = "";
Panel1.Controls.Add(Labelfeedback);
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn1_click(object sender, EventArgs e)
{
InsertUserInfo.phoneChosen = ((RadioButtonList)FindControl("radioList")).SelectedItem.Text;
double UserBudget =
Convert.ToDouble(((TextBox)FindControl("budgTxt")).Text);
InsertUserInfo.BudgetYN(UserBudget);
((Label)FindControl("feedback")).Text = InsertUserInfo.feedback; }}}
namespace gfjsr{
public class phone{
private string _phoneChosen;
public string phoneChosen
{
get { return _phoneChosen; }
set { _phoneChosen = value; }
}
private string _feedback;
public string feedback
{
get { return _feedback; }
set { _feedback = value; }
}
public double Func1(string x)
{
double phonePrice = 0;
if( x == "samsung")
{
phonePrice = 4000;
}
if (x == "IPHONE")
{
phonePrice = 3500;
}
if (x == "XIAOMI")
{
phonePrice = 3000;
}
if (x == "LG")
{
phonePrice = 2000;
}
return phonePrice;
}
public void BudgetYN(double y)
{
if(y >= Func1(_phoneChosen))
{
_feedback = "positive";
}
else
{
_feedback = "no";
}
}
}
}

How to annotate a changed item in a listbox with a *

I just need help on how to annotate a changed item in the list box if the user changes something using the text boxes provided.
namespace HW1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] values = new string[5];
values[0] = textBox1.Text;
values[1] = textBox2.Text;
values[2] = textBox3.Text;
values[3] = textBox4.Text;
values[4] = textBox5.Text;
string[] temp = new string[5];
temp[0] = textBox1.Text;
temp[1] = textBox2.Text;
temp[2] = textBox3.Text;
temp[3] = textBox4.Text;
temp[4] = textBox5.Text;
if(temp != values)
{
listBox1.SelectedIndex = 0 + "*";
listBox1.Text = values[1] + "*";
listBox1.Text = values[2] + "*";
listBox1.Text = values[3] + "*";
listBox1.Text = values[4] + "*";
}
listBox1.Items.Clear();
for (int i = 0; i < values.Length; i++)
{
listBox1.Items.Add(values[i].ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
The program will simply replace the old input from the text box with the new without displaying a * next to the item that has changed.
Your code doesn't actually compile... not sure how this line would ever work...
listBox1.SelectedIndex = 0 + "*";
Anyway - the main problem is that your for loop adds in values without the star to the list box
for (int i = 0; i < values.Length; i++)
{
listBox1.Items.Add(values[i].ToString()); //values[i] never has a star stored in it!
}
How about something like this...?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] values = new [] { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text };
for (int i = 0; i < values.Length; i++)
{
if (listBox1.Items.Count < i + 1)
{
listBox1.Items.Add(values[i].ToString());
continue;
}
string unedited = listBox1.Items[i].ToString();
if (!string.IsNullOrEmpty(unedited) && unedited.Last() == '*')
unedited = listBox1.Items[i].ToString().Substring(0, listBox1.Items[i].ToString().Length - 1);
if (unedited != values[i])
listBox1.Items[i] = values[i] + "*";
else
listBox1.Items[i] = values[i];
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
This compares the list items to the textbox values.
If the textbox value doesn't exist, a listbox item is created.
If the listbox item doesn't match the textbox value, it has a * appended to it.
If an existing value (ignoring the star) is the same as the textbox value, it is updated to ensure the star is removed.

how to edit the text of combobox from another view c# wpf

I have three combobx for time and I want to edit the text of this comboboxes from another view this is the code for combobpx view
public partial class Combo : Window
{
public Combo()
{
InitializeComponent();
}
private void hrComboBox_Loaded(object sender, RoutedEventArgs e)
{
List<string> hours = new List<string>();
for (int i = 0; i <= 24; i++)
{
if (i < 10)
hours.Add("0" + i);
else
hours.Add(i + "");
}
var comboBox = sender as ComboBox;
comboBox.ItemsSource = hours;
comboBox.SelectedIndex = 0;
}
private void MinComboBox_Loaded(object sender, RoutedEventArgs e)
{
List<string> Minutes = new List<string>();
for (int i = 0; i <= 60; i++)
{
if (i < 10)
Minutes.Add("0" + i);
else
Minutes.Add(i + "");
}
var comboBox = sender as ComboBox;
comboBox.ItemsSource = Minutes;
comboBox.SelectedIndex = 0;
}
private void SecComboBox_Loaded(object sender, RoutedEventArgs e)
{
List<string> Seconds = new List<string>();
for (int i = 0; i <= 60; i++)
{
if (i < 10)
Seconds.Add("0" + i);
else
Seconds.Add(i + "");
}
var comboBox = sender as ComboBox;
comboBox.ItemsSource = Seconds;
comboBox.SelectedIndex = 0;
}
private void OKButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
DialogResult = true;
}
the second view invokes this view as dialog my problem is when I add the text to one of this comboboxes the text isn't set and still "00" the code I use as follows:
var dialog = new Combo();
string[] timeArr = delay.Split(':');
//here is my problem when the dialog is loading the text
//of three comboboxes isn't change
dialog.hr.Text = timeArr[0];
dialog.Min.Text = timeArr[1];
dialog.Sec.Text = timeArr[2];
if (dialog.ShowDialog() == true)
{
string time = dialog.hr.Text+":"+dialog.Min.Text+":"+dialog.Sec.Text;
delay = time;
nextDelay = time;
}
anyone help me, thanks :)

Why will this not show MessageBox?

I am trying to write this program to tell the user if the name he entered is on the list of most popular names, for girls or boys. The problem I am having is that my button click wont display the message boxes that I have coded. I have been stuck on this problem for a while and I cant seem to get it to display.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadNames(object sender, EventArgs e)
{
const int size = 200;
string[] names = new string[size];
string[] names1 = new string[size];
int index = 0;
int index1 = 0;
StreamReader inputfile1;
StreamReader inputfile2;
inputfile1 = File.OpenText(#"F:\C# HW\CH07_HW_07\BoysNames.txt");
inputfile2 = File.OpenText(#"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < names.Length)
{
names[index] = inputfile1.ReadLine();
index++;
}
while (!inputfile2.EndOfStream && index1 < names1.Length)
{
names1[index1] = inputfile2.ReadLine();
index1++;
}
}
private Boolean FindBoyname()
{
const int size = 200;
string[] BoyNames = new string[size];
int index = 0;
string boyname = textBox1.Text;
Boolean Boyname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(#"F:\C# HW\CH07_HW_07\BoysNames.txt");
while (!inputfile1.EndOfStream && index < BoyNames.Length)
{
BoyNames[index] = inputfile1.ReadLine();
if (String.Equals(boyname, BoyNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Boyname = true;
}
index++;
}
return Boyname;
}
private Boolean FindGirlname()
{
const int size = 200;
string[] GirlNames = new string[size];
int index = 0;
string girlname = textBox2.Text;
Boolean Girlname = false;
StreamReader inputfile1;
inputfile1 = File.OpenText(#"F:\C# HW\CH07_HW_07\GirlsNames.txt");
while (!inputfile1.EndOfStream && index < GirlNames.Length)
{
GirlNames[index] = inputfile1.ReadLine();
if (String.Equals(girlname, GirlNames[index], StringComparison.OrdinalIgnoreCase) == true)
{
Girlname = true;
}
index++;
}
return Girlname;
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
boy = FindBoyname();
girl = FindGirlname();
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
Most likely the button1.Click event is't subscribed with this void button1_Click event handler. Check it in design code or in the visual designer
If you remove all the code that has nothing to do with showing a message box you get:
I am trying to write this program to tell the user if the name he entered is on the list of most popular names, for girls or boys. The problem I am having is that my button click wont display the message boxes that I have coded. I have been stuck on this problem for a while and I cant seem to get it to display.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Boolean boy;
Boolean girl;
if (boy.Equals(true))
{
MessageBox.Show(textBox1.Text + " is among the most popular boy names!");
}
if (boy.Equals(false))
{
MessageBox.Show(textBox1.Text + " is not among the most popular boy names.");
}
if (girl.Equals(true))
{
MessageBox.Show(textBox2.Text + " is among the most popular girl names!");
}
if (girl.Equals(false))
{
MessageBox.Show(textBox2.Text + " is not among the most popular girl names.");
}
}
}
This code doesn't wire up your event anywhere, so have a look inside InitializeComponent and see if it does so there.
You're looking for a line that reads:
button1.Click += button1_Click;
If it's not there, then that'll be why your code isn't showing anything.

How can i get from a window only the text without the Handle number?

I have two classes that get all the minimized windows:
The first class is WindowSnap.cs:
WindowSnap.cs
The second class is WindowSnapCollection.cs:
WindowSnapCollection.cs
In the first class in the windowSnap there is a method called GetAllWindows:
public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
{
windowSnaps = new WindowSnapCollection();
countMinimizedWindows = minimized;//set minimized flag capture
useSpecialCapturing = specialCapturring;//set specialcapturing flag
EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
return new WindowSnapCollection(windowSnaps.ToArray(), true);
}
In the end i used a breakpoint on the return line and windowSnaps variable contain 15 items.
For example the first item is:
[0] = {Window Text: , Handle: 31918532}
The second one is:
[3] = {Window Text: ?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?, Handle: 64424060}
In the first item i was able to do on my own to remove this item since it's text is only "," and this window show nothing.
But number [3] and other items i want first to remove from them the part Handle: 64424060
I don't need that the user will see this information. But only the text so for example number [3] should look like:
?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?
And to fix those "?" that are in the beginning of the line.
But the idea is to display only the name of each window and not the whole text with the handle number.
This is how i add the items(windows) to form1 listBox:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
In the listBox i want to see the items names or description/titles not the whole text with the handle number.
You can do a little trick. It is not nice but works. Add a new listbox eg listBox1 with the same size on top of listBoxSnap.
add in public Form1():
public Form1()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
buttonSnap.Enabled = true;
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
change private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e) to:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
drawpicbox(this.listBoxSnap.SelectedIndex);
}
and delete this.listBoxSnap.SelectedIndexChanged += new System.EventHandler(this.listBoxSnap_SelectedIndexChanged); from Form1.Designer.cs
Add this event:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var item = listBox1.IndexFromPoint(e.Location);
if (item >= 0)
{
listBox1.SelectedIndex = item;
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
cm.Show(listBox1, e.Location);
}
}
}
And lastly add in private void RefreshWindowsList()
private void RefreshWindowsList()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.Clear();
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
Valter

Categories

Resources