Assistance regarding tabs in Windows Forms - c#

I'm currently working on a small side project as a way of getting used to Forms in Visual Studio 2012, as I usually only work with Console Applications. My current layout is designed to use tabs, and the user is to specify how many of the tabs they need for this application. They then fill out some information and it will be formatted and output to a file at a location specified by the user. On to the questions.
In order to stop duplicate tabs from existing, I'm using the following:
private void comboTabs_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboSkills.SelectedIndex == 0)
{
tabControl1.TabPages.Remove(tab8);
tabControl1.TabPages.Remove(tab7);
tabControl1.TabPages.Remove(tab6);
tabControl1.TabPages.Remove(tab5);
tabControl1.TabPages.Remove(tab4);
}
//repeat for Index 1, 2 and so on
}
There will always be a minimum of 3 tabs, so the first selection on the combo box removes tabs 4 through 8. The next selection does the same, but then adds tab4 back again. This goes on for the following selections. Is there any way I can do this more conveniently?
Second question, each tab has a series of text boxes and combo boxes that users are to select information from. The problem I'm having is that I need to identify how many tabs the user has selected and then only pull information from those tabs. I'm aware that I can get the number of tabs with:
int numberoftabs = tabControl1.TabCount;
But after that I can't seem to read the information from them. I'm intending to do
for (int i = 0; i < numberoftabs; i++)
{
//get textbox text of tab i and so on
}
Is there any way I can do this? I was hoping to use a tab layout since I like my current layout very much. If it makes a difference, all the tabs have the same layout, and share a naming convention such as tab 1 text box 1 is textTab1Name, tab 2 text box 2 is textTab2Name and so on.

For the first part of your question, you can handle all cases with this piece of code:
var tabCount = 5 - comboBox1.SelectedIndex;
for (var i = 0; i < tabCount; i++)
{
tabControl1.TabPages.RemoveAt(7-i);
}
For the second part you will have to create this method:
private T GetControl<T>(string name) where T : Control
{
return (T) this.Controls.Find(name, true).FirstOrDefault();
}
Then you can write your text retrieval loop like this:
for (int i = 0; i < numberoftabs; i++)
{
//get textbox text of tab i and so on
TextBox textBox1 = GetControl<TextBox>("textTab" + i + "Name");
...
etc..
}

Related

Search for word in ListBox written on InputBox (C# VS)

I have a ListBox with words and I need to click a button that opens an InputBox where I can search for a word and the program will run the ListBox and highlight the word I wrote in the InputBox if it's there. If the program reaches the end of the list and doesn't find the word then I'll get a MessageBox saying the word I'm looking for isn't there. I need to use some sort of cycle for this program.
I know how to make the button, InputBox and the error MessageBox, but I don't know how to do the searching and cycle.
I've read a lot of similar questions here but I don't think any of them return the result I'm looking for.
Can anyone help me? Or redirect me to a post with the answer?
This is for Winforms.
That should get you on track, it's pretty much self-explanative:
whenever text changes
find matching items in list
select them
Code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox ?? throw new InvalidOperationException();
var text = textBox.Text;
if (string.IsNullOrWhiteSpace(text))
return; // nothing to search for
const StringComparison comparison = StringComparison.InvariantCultureIgnoreCase; // maybe change this
// find items matching text
var indices = new List<int>();
for (var i = 0; i < listBox1.Items.Count; i++)
{
var item = listBox1.Items[i];
if (string.Equals(item?.ToString(), text, comparison))
indices.Add(i);
}
// select them in list
if (!indices.Any())
return;
listBox1.SelectedIndices.Clear();
foreach (var index in indices)
listBox1.SelectedIndices.Add(index);
}
Of course, list selection mode has to be multiple for it to work properly.
Also, you will need to clear selection if there are no matches so as to not leave the UI in an ambiguous state (not done).

Generate multiple data in Acumatica

I am trying to create a button that able to generate multiple data into grid in Acumatica. Here is what I am trying to accomplish.
Based on the figure above, if I press button (number 1), the grid (number 2) will filled automatically for certain amount of data, is this possible in Acumatica? Thank you in advance
The answer would be yes and here is the design pattern.
Assumptions: You have a graph, and publicly declared a PXSelect on MyLineDac as Lines;
public PXSelect<MyLineDac , Where<...>> Lines;
Under the button press event:
Clear out the current lines if needed
foreach (MyLineDac oldline this.Lines.Select())
{
this.Lines.Delete(oldline);
}
Populate lines:
foreach (MyDacSourceLines lineadd in
PXSelectReadonly<MyDacSourceLines,
Where<........>>>.Select(this, row.RefValueIfweHaveRequiredFields)
)
{
MyLineDac newline = new MyLineDac();
newline.No = lineadd.No;
newline.SubElement = lineadd.SubElement;
newline = this.Lines.Insert(newline);
}

tabcontrol not refreshing

I have a TabControl that starts with three TabPages in it. On the first tab there is a NumericUpDown (spinner) which displays the number of tabs and allows a user to add up to 10 extra tabs. Once they add more than about 5 or 6 it goes beyond the width of the form and the rest of the tabs are accessible by a couple of left/right arrows at the top. When going all the way to the right and then using the spinner to go back down to 0 (removing all the extra tabs and leave the starting three) it removes all tabs from the top of the pane and only by setting the spinner back to 1 does it refresh and display all 4 (3 from the start plus the 1 from the spinner).
I have tried several commbinations of
Application.DoEvents()
this.Refresh()
this.Invalidate()
this.Update()
but nothing seems to work. can anybody suggest a reason why it is not updating/refreshing?
public partial class Form1 : Form
{
TabPage[] tabs;
public Form1()
{
InitializeComponent();
tabs = new TabPage[tabControl1.Controls.Count];
tabs[0] = tabPage1;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
int numTabs = tabControl1.Controls.Count;
decimal spinnerValue = numericUpDown1.Value;
if (numTabs < spinnerValue) //add a tab
{
TabPage[] newTabs = new TabPage[(int)spinnerValue];
for (int i = 0; i < numTabs; i++)
{
newTabs[i] = tabs[i];
}
TabPage tab = new TabPage("Tab " + numTabs);
newTabs[(int)spinnerValue-1] = tab;
tabControl1.Controls.Add(tab);
tabs = newTabs;
}
else //remove a tab
{
TabPage[] newTabs = new TabPage[(int)spinnerValue];
for (int i = 0; i < spinnerValue; i++)
{
newTabs[i] = tabs[i];
}
tabControl1.Controls.Remove(tabs[(int)spinnerValue]);
tabs = newTabs;
}
}
}
Without seeing any code or knowing what type of project this is winforms, WPF, ASP.NET etc..
it's hard to give a definite answer, I am going to assume that this is WinForms
I'm not sure if you can. The following is a quote from MSDN:
"Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."
However, instead of having the update code get the values from the controls directly, maybe you could create a class that could hold the Data you use to populate the controls and then when the update code is called it asks the class for the value and the class checks if the control is loaded and otherwise it gets the value from the Data instead.

C# Text box remaining empty

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

Clearing text boxes inside of a Control

I have a series of of tabs that hold text boxes in them. Some of the tabs have a control that contains Text Boxes inside of a Scrollview as well. I am trying to iterate through the tabs and clear the content of the text boxes.
I was going to use this:
foreach(TabItem item in Tabs.Items)
{
ClearTextBoxes(this);
}
I then use this to clear to the text boxes:
TextBox tb = obj as TextBox;
if (tb != null)
tb.Text = "";
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
ClearTextBoxes(VisualTreeHelper.GetChild(obj, i));
}
It is currently only clearing the first tab and none of the rest.
Any ideas?
Use the LogicalTreeHelper. Only the items of the currently active tab are contained in the visual tree, therefore the visual tree helper is not the best choice for your task.
Iterating over the tab items is IMO not necessary, only if you have a lot of other controls not residing in the tab-items and therefore want to spare cpu power. As already mentioned by Bela R, there is an error in your call to ClearTextBoxes().
I think it should be ClearTextBoxes(item) and not ClearTextBoxes(this)
foreach(TabItem item in Tabs.Items)
{
ClearTextBoxes(item);
}

Categories