I created three text boxes calls HighOffSetX, HighOffSetY, and HigOffSetZ.
I want to add them to a list called HighOffSets. The problem that I am running into is how to add the text boxes into a list in another class.
When I try to add HighOffSetX to my list I get an error that HighOffSetX is not recognized. What do I do so my class recognizes the text boxes? I do not have a lot of experience in GUIs with C#. I looked on Google and stack and could not find an answer that helped me out. Here is the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace UCAGui
{
public class CaliberationGUI: Form
{
List<TextBox> HighOffSets = new List<TextBox>();
public CaliberationGUI()
{
HighOffSets.Add(this.HighOffSetX); //This line gives me an error
}
}
}
I changed the names of the text boxes to HighOffSetX, HighOffSetY, and HigOffSetZ. Here is the generated code from ConfigForm.Designer.cs
this.HighOffSetX.Location = new System.Drawing.Point(60, 28);
this.HighOffSetX.Name = "HighOffSetX";
this.HighOffSetX.Size = new System.Drawing.Size(100, 20);
this.HighOffSetX.TabIndex = 5;
If you want me to post the generated code C# created when I added the text boxes let me know.
You have declared your textbox in a different form.You can't access that textbox without an instance of ConfigForm.Create a new instance of your second Form, or pass it via the constructor:
public class CaliberationGUI: Form
{
private ConfigForm configForm;
List<TextBox> HighOffSets = new List<TextBox>();
public CaliberationGUI(ConfigForm config)
{
InitializeComponent();
this.configForm = config;
HighOffSets.Add(this.configForm.HighOffSetX);
}
}
Related
I am trying to create a custom panel where I can change the properties using Properties -> Buttons -> [(Collection) ...]:
The following is what I have tried
public class CustomPanel : Panel
{
private List<Button> buttons = new List<Button>();
public List<Button> Buttons
{
get { return buttons; }
set
{
buttons = value;
this.Controls.Clear();
foreach (var button in buttons)
{
button.Size = new Size(200, 30);
this.Controls.Add(button);
}
}
}
}
But when I add a new button using Properties -> Buttons -> [(Collection) ...]: it is not adding it to my panel.
Assuming a CustomPanel class has been added as a custom control, and its base class changed to Panel, you can try the following code.
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Windows.Forms;
namespace TEST
{
public partial class CustomPanel : Panel
{
public CustomPanel()
{
InitializeComponent();
}
[Editor(typeof(ArrayEditor), typeof(UITypeEditor))]
public Button[] Buttons
{
get { return this.Controls.OfType<Button>().ToArray(); }
set
{
SuspendLayout();
try
{
this.Controls.Clear();
foreach (var button in value)
{
button.Size = new Size(200, 30);
this.Controls.Add(button);
}
}
finally { ResumeLayout(); }
}
}
}
}
Make sure the project has a reference to System.Design.dll, for ArrayEditor to be recognized.
Keep in mind that:
Every Button control added by the designer through the Buttons property will be serialized like it was added in the usual design way.
Adding other (non-Button) controls is still possibile in the usual design way, or programmatically.
this.Controls.Clear will remove any control, even the ones added without using Buttons; you can delete this instruction if you want to preserve them, but you will need a way to recognize the previously existing buttons and replace or keep them as needed.
All properties of any Button object can be edited inside the ArrayEditor form.
You need to handle positioning of the Button controls (both newly added and existing ones) in order to avoid overlapping.
I suggest to use a child FlowLayoutPanel for automatic positioning of the buttons and to prevent anyone to mess directly with the collection of buttons. With this approach, this.Controls will become flowLayoutPanel1.Controls (or similar).
Otherwise, you should override OnControlAdded and OnControlRemoved to be notified when any control is added or removed.
Hey guys new to C# and I am trying to setup a GUI, all I want the GUI to do is have a simple file explorer with a CheckedListBox to represent selected files.
I can get the CheckedListBox to show up and click on files but I'm not sure how to continue from here, most tutorials stop here, or go too advanced with tree view and other things that seem unnecessary for what I am trying to do.
Here is my code:
Any help is appreciated and if you guys could point me in the right direction that would be awesome.
EDIT:
To rephrase my question:
I want the user to select files through the CheckedListBox (user input stops here), and for those selected files to be put in a list that my code can manipulate.
Not sure how to accomplish this after my first foreach loop (which adds all files in the selected directory to the CheckedListBox for user selection).
The second foreach loop is an attempt at this, manipulating the files so that they output their filenames after being selected. However no Messagebox shows up and I assume that their is a disconnect between the user selecting files and the codes attempt at manipulating said files.
Second Edit:
I think I figured it out I made a second button and from here it looks like I can manipulate the chosen files however I want.
Currently the code is working the way I would expect it to work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SelectFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
checkedListBox1.CheckOnClick = true;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (string file in files)
{
checkedListBox1.Items.Add(file);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
List<string> list_all_excelfiles = new List<string>();
foreach (string item in checkedListBox1.CheckedItems)
{
list_all_excelfiles.Add(item);
MessageBox.Show(Path.GetFileName(item));
}
}
}
}
First i advice you to assign Value member and Display member for each item.
Display Member will be visible to user
Value Member will we use in code
To do this first create simple custom class
public class Int_String
{
public int _int { get; set; }
public string _string { get; set; }
}
Be careful because get; set; part is important to be there since if not code will not work
Now what you need to do is create list of items with custom class like this
class YourForm : Form
{
List<Int_String> myList = new List<Int_String>(); //Create list of our custom class
public YourForm()
{
PopulateMyList();
}
private void PopulateMyList()
{
//Here read from database or get data somehow and populate our list like this
//I will populate it manually but you do it in foreach loop
myList.Add(new Int_String { _int = 0, _string = "First Item" });
myList.Add(new Int_String { _int = 1, _string = "Second Item" });
myList.Add(new Int_String { _int = 2, _string = "Third Item" });
}
}
After that you need to assign this list to your checkedListBox which you will do like this:
public YourForm()
{
PopulateMyList();
checkedListBox1.DataSource = myList;
checkedListBox1.DisplayMember = "_string";
checkedListBox1.ValueMember = "_int";
}
And now when you can manipulate with checked items like this:
for(int i = 0; i < checkedListBox1.Items.Count; i++)
{
if(checkedListBox1.Items[i].CheckedState == CheckState.Checked)
{
int itemValueMember = (checkedListBox1.Items[i] as Int_String)._int;
int itemDisplayMember = (checkedListBox1.Items[i] as Int_String)._string;
//Use these two vars for whatever you need
}
}
TWO IMPORTANT TIPS:
I am not sure for this one since i am writing all this from head but i think that visual studio will not show you that there is DisplayMember or ValueMember for checkedBox component BUT also it will not show error. Reason is that they have hidden in intentionally for idk what reason but it will work.
You are able to assign Display and Value member to a lot of components in winforms BUT for some reason checkedListBox is specific. It is specific because you MUST first assign DataSource to it and then tell it checkedListBox.DisplayMember = "_string" ...... For new guy you will ask why it is important. Simple answer is create custom list for test and add 10k items inside it and then first declare datasource and after it Display and Value member. Test how long form will need to load (get out of freeze state). After that do everything same but first declare Display and Value member and then assign datasource and test again. I am telling this from head without testing but before when i needed about 5k rows with 1st solution it took me about 30 sec and second < 1 sec. If you want to know more about it google it but for now this is pretty much info for you.
I've searched the questions for an answer but couldn't quite find a clear cut example. I am trying to display a simple text box in C#. I am working with C#, ArcMap and ArcObjects. I have created a toolbar that has a button in it. Upon clicking the button, I just need a text box to appear on the page. So far, this is what I've got, but nothing is producing when I click my button. Thanks for your help in advance.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Map
{
public class ArcGISAddin4 : ESRI.ArcGIS.Desktop.AddIns.Button
{
public ArcGISAddin4()
{
}
protected override void OnClick()
{
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "My First Text Box";
dynamicTextBox.Name = "First Text Box";
dynamicTextBox.Enabled = true;
}
protected override void OnUpdate()
{
}
}
}
You must add the TextBox to the surrounding container (the form for example). Otherwise the program won't know where it's supposed to be displayed.
You should add dynamicTextBox to a specific container such as form.
Such as this:
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "My First Text Box";
dynamicTextBox.Name = "First Text Box";
dynamicTextBox.Enabled = true;
this.Contols.Add(dynamicTextBox);//this is a pseudo code
So, my XML file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<quiz>
</answers>
</question>
<!-- More questions here -->
</quiz>
My Form1.cs looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class FormMain : Form
{
Basically, all four buttons had the question on them rather than the answers and underneath the bottom left button, there appeared to be a blank button. How do I get the question to display where label1 is and the answers to be on four separate buttons? Also, how will I do it when I start adding multiple questions (User obviously can't move on to the next question unless they get previous one right and difficulty can be chosen at the start to show a different set of questions).
I've been on at this for a while and I think it needs a fresh set of eyes because my relatively novice C# brain can't figure it out. Anyone help me please?
Check out the answer to this question. What is much easier to deal with is Deserialize the XML into a class you've created with a matching structure.
How to Deserialize XML document
More info here:
http://msdn.microsoft.com/en-us/library/58a18dwa.aspx
You are setting the text of every button each time around your loop:
foreach (var question in _questions)
{
button1.Text = question.QuestionText;
button2.Text = question.QuestionText;
button3.Text = question.QuestionText;
button4.Text = question.QuestionText;
}
The last time around the loop, each button will have the text set to the text of the last question.
It also looks a bit odd, as you have four buttons defined as fields of the form, yet you are creating additional buttons in your PopulateForm method but doing nothing with them.
You would be better off getting rid of the fields and working with the newly-created buttons directly:
private void PopulateForm()
{
int count = 0;
foreach (var question in _questions)
{
var button = new Button();
button.Size = new Size(60, 23);
button.Location = new Point(100, 40 + (count * 30));
button.Text = question.QuestionText;
Controls.Add(button);
count++;
}
}
You have to set the location of each button to something different, otherwise they will all show in the same place.
EDIT:
From looking at your code in your zip file, what you want is something like (assuming that your questions have four answers):
private void PopulateForm()
{
foreach (var question in _questions)
{
label1.Text = question.QuestionText;
button1.Text = question.Answers[0];
button2.Text = question.Answers[1];
button3.Text = question.Answers[2];
button4.Text = question.Answers[3];
}
}
Hai all,
I want to get lookupedit display text when am giving correspond edit value.
example:
if am giving
LookupEdit1.Editvalue="3";
then it should show display text of Editvalue="3"
please help
//code
cmbChemical.Properties.DataSource = _lab.selectChemicals();
cmbChemical.Properties.DisplayMember = "labitem_Name";
cmbChemical.Properties.ValueMember = "labItem_ID";
cmbChemical.Properties.BestFitMode = BestFitMode.BestFit;
cmbChemical.Properties.SearchMode = SearchMode.AutoComplete;
cmbChemical.Properties.Columns.Add(new LookUpColumnInfo("labitem_Name", 100, "Chemicals"));
cmbChemical.Properties.AutoSearchColumnIndex = 1;
You can't, at least not in the way you're trying. The LookUpEdit, as the name implies, looks up its values in a DataSource, eg. a collection of objects. Therefore, to display the value 3 you need to have a list of objects that contains this value and set it as a DataSource for the control.
List<string> values = new List<string>();
values.Add("3");
lookUpEdit.Properties.DataSource = values;
lookUpEdit.EditValue = "3";
Maybe if you specify what are you trying to do, we can help you achieve that.
I think you don't have to specify display member or value member to get your needed behaviour. Following code give me a form with the lookupedit correctly showing "4", and i can choose other values from the list too.
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
public class Form1 : Form
{
public Form1()
{
var lookUpEdit1 = new LookUpEdit();
Controls.Add(lookUpEdit1);
var source = new List<string>();
for (var i = 0; i < 10;i++ )
source.Add(i.ToString());
lookUpEdit1.Properties.DataSource = source;
lookUpEdit1.EditValue = "4";
}
}
Maybe you get wrong results because you set display member and value member of the control.
This code worked for me.
private void lookUpEdit1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show((e.OriginalSource as SLTextBox).Text);
}
}