I have two forms, create order and display order. Once the confirm order button has been pressed on CO form, the products are stored into a string variable
productsPlaced = Convert.ToString(textBox1.Text + Environment.NewLine + textBox2.Text);
Upon confirming the order, console.writeline shows the data still in the variable.
Upon opening DO form the variable clears and console.writeline shows nothing.
I am trying to display this variable data into another textbox on a different form.
textBox1.Text = createOrder.productsPlaced;
Any ideas?
Thanks
You could Declare a Static variable in Form
public static string productsPlaced = null;
productsPlaced = textBox1.Text + Environment.NewLine + textBox2.Text;
In 2nd Form:
tBox1.Text = createOrder.productsPlaced;
Related
I have a syncfusion tab control and their Tabs are populated according to the number of accounts a user has.
foreach (SecurityAccount securityAccount in secs)
{
string name = securityAccount.AccountNumber;
Syncfusion.Windows.Forms.Tools.TabPageAdv tabPage =
new Syncfusion.Windows.Forms.Tools.TabPageAdv(securityAccount.PortfolioName
+ " [" + securityAccount.AccountNumber + "]");
tabPage.Tag = securityAccount.AccountNumber;
tcAcc.TabPages.Add(tabPage);
if (securityAccount.IsDefaultPortfolio)
defaultPortfolioName = securityAccount.AccountNumber;
}
In every security account, there is a field called IsDefaultPortfolio Account or a not.
so if it is a default one I am putting that value in the defaultPortfolioName parameter
So now I want to select the tabpage that has this security account number (Which is put into tabPage.Tag value.
I wrote this
tcAcc.SelectedIndex = tcAcc.TabPages.IndexOf(defaultPortfolioName);
But that doesn't work because I am not saying to check the Tag value of each tab.
Select the tabpage based on the value stored in the tag of each tabpage.
foreach(TabPageAdv page in tabControlAdv1.TabPages)
{
if(defaultPortfolioName==(int)page.Tag)
{
tabControlAdv1.SelectedIndex = tabControlAdv1.TabPages.IndexOf(page);
break;
}
}
I have a car class with four attributes (colour, model, mileage, year) and I get these attributes from four text boxes. I then assign these attributes to a new object of the car class and assign this new object to an object array in the car class. I need to be able to highlight the mileage attribute but I am outputting in a string builder like this it like this:
private void button1_Click(object sender, EventArgs e)
{
carColourIN = colour.Text;
carModelIN = model.Text;
carMileageIN = Double.Parse(mileage.Text);
carYearIN = Int32.Parse(year.Text);
Car user = new Car(carColourIN, carModelIN, carMileageIN, carYearIN);
Car.vehicles[i] = user;
StringBuilder sb = new StringBuilder();
sb.Append("Colour: " + Car.vehicles[i].CarColour + "\nModel: " + Car.vehicles[i].CarModel + "\nMileage: " + Car.vehicles[i].CarMileage
+ "\nYear: " + Car.vehicles[i].CarYear + "\n\n");
displayLabel.Text = sb.ToString();
i++;
}
how would I highlight the mileage attribute in the appended string builder so that either the background of that particular section of text is a different colour or the actual text is.
I see a few different options. The answers to this question Multiple Colors in C# Label explain the first two options.
Option 1: Do not use your StringBuilder, and use a rich text box. Then loop through the properties and add them one at a time using the rtb_AppendText function in the provided link.
Option 2: Create a custom label control and override the OnPaint method. In the OnPaint method you can use MeasureText and DrawString to accomplish your goal.
Option 3: Create a user control with four different labels, this way you could control the color/font/appearance of each label, and in the long run gives you the most flexibility.
im working on a code-editor(winforms) and Im just wondering if its possible to call a specific box from a form to another?
sample for this set of codes:
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
label1.Text = "line: " + line.ToString() + ", column: " + column.ToString();
***code above was inside a timer which calls the count of line and column in a richtextbox like in lower rightpart of actual code editor .
now im just wondering if its possible to call the label that displays to the main form and will display to another but will still run .
like in mainform theres the code for richtextbox and on other form it should have the code of label that connects to the mainform .
my question is it possible to call a tool function from another form
to another?
hope you could help me, really in need and thanks a lot!
As long as you have a reference to that form toolbox, just expose that Label/TextBox or whatever you want to change via a public property and set it from your context.
public class ToolBox : ToolBoxBase
{
public CustomLabel
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
}
private ToolBox toolbox;
void ShowToolBox()
{
InitToolBox();
toolbox.CustomLabel = "New label";
}
As I'm not even sure what technology the question refers to I added a poor pseudo example to get you the idea. The InitToolBox method initializes the toolbox and displays it, and sets the field toolbox with a reference to it.
If the other form runs on another thread, then you'll have to invoke the label setter asynchronously. See this question for more instructions.
I am trying to set my ComboBoxes so that the user can either choose from the list or set their own value (the combo box is for a custom resolution so there will be default values or they can give their own).
I am trying to make it so that if their value is incorrect (below 0 or not an Int) then it shows a tooltip and prevents it from losing focus. Here is my code:
private void cmbX_Graphics_Width_LostFocus(object sender, EventArgs e)
{
int i = 0, width = 0;
TLQAShared._debug("Lost Focus Fired");
for (i = 0; i < cmbX_Graphics_Width.Items.Count; i++)
{
if (cmbX_Graphics_Width.Text.Equals(cmbX_Graphics_Width.Items[i].ToString()))
{
Properties.X.Default.Graphics_Width = int.Parse(cmbX_Graphics_Width.Items[i].ToString());
TLQAShared._debug("FOUND!");
return;
}
TLQAShared._debug("FOR: " + i.ToString() + "/" + (cmbX_Graphics_Width.Items.Count - 1).ToString() + ": " + cmbX_Graphics_Width.SelectedText + ":" + cmbX_Graphics_Width.Items[i].ToString());
}
TLQAShared._debug("Not true: '" + cmbX_Graphics_Width.Text + "'");
if (int.TryParse(cmbX_Graphics_Width.Text.ToString(), out width))
{
TLQAShared._debug("TryParse: true");
Properties.X.Default.Graphics_Width = width;
}
else
{
tt.SetToolTip(cmbX_Graphics_Width, "You must supply a valid integer");
this.ActiveControl = cmbX_Graphics_Width;
TLQAShared._debug("TryParse invalid.");
}
}
However if the control loses focus, this code gets executed twice, first time it stops at this part:
TLQAShared._debug("Not true: '" + cmbX_Graphics_Width.Text + "'");
Then does it again but executes the entire code, but does not prevent the control from losing focus.
Two questions I have:
Firstly: Is this the best practice and if not what should I do?
Secondly: If it is best practice, how would I fix it?
use combobox1.Select(); for focus in combobox.
I don't think this is a good practice. I would do it like this:
Create a function to check if the input is valid (int > 0)
Call this function when the user attempts to enter the input
If the input isn't valid call combobox.focus()
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