Get value from richtextbox and assign them to radio button in c#? - c#

I'm working on program that will take the input from richtextboxes in a loop and then I want to assign the text that I get from user to radiobutton that will be in a groupbox. I successfully developed the form with richtextboxes in a loop but now I stuck how I will assign them to radio button in a loop? When I click button?? Please guide me in this regard?
a piece of code here
Label label1 = new Label();
label1.Size = new System.Drawing.Size(96, 24);
label1.Text = "Question" + _openCount++;
label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Size = new System.Drawing.Size(600, 91);
richTextBox1.Text = "";
Label label2 = new Label();
label2.Size = new System.Drawing.Size(96, 24);
label2.Text = "Option1";
......................
now i want to put my code in this button how i further start my code???
void button1(object sender, EventArgs e)
{
}

Create a list of your radio buttons, then create a list of your rich textbox strings.
Enumerable.Zip these two lists together then foreach through that list.
foreach(var item in zippedList)
{
item.RadioButton.Text = item.RichTextBoxText.Text;
}
The syntax for this would be way off, but I hope you get the idea. This is also assuming that you have an equal number of radio buttons and lines of text. You'll need to take that into consideration and will have to modify the two lists before zipping them if they are different lengths.

Related

C# getting form reference in an eventhandler

My problem is not complicated but i've been looking up for quite some time now and can't seem to find the correct way to phrase it so if this is a duplicate I am sorry.
I have a Form created on a button click, within this Form is a table layout which contains a set of Controls (a label, a combo box and a button). What I want to do is to store the current index selected in the combo box when the button is pressed. However, I can't access the combo box when I'm in the button click event handling block.
This is the creation of my Form, nothing extraordinary.
Form settingsWindow = new Form();
settingsWindow.Text = "Connexion settings";
settingsWindow.MaximumSize = new System.Drawing.Size(200, 200);
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
Label label = new Label();
label.Text = "Choose an available COM port";
Button bt_OK = new Button();
bt_OK.Text = "valider";
bt_OK.Anchor = AnchorStyles.Bottom;
bt_OK.Name = "bt_OK";
bt_OK.Click += Bt_OK_Click;
ComboBox cb_portList = new ComboBox();
SerialComm serial = new SerialComm(); //Filling the combo box with a list of
List<string> portList = new List<string>(); //available COM ports
portList = serial.getAllPorts();
foreach(string portName in portList)
{
cb_portList.Items.Add(portName);
}
layout.Controls.Add(label);
layout.Controls.Add(cb_portList);
layout.Controls.Add(bt_OK);
What I'd like to be able to do is the following, imagine I made a class to store the data I want :
private void Bt_OK_Click(object sender, EventArgs e)
{
datacontainer.selectItem = cb_portList.SelectedItem;
settingsWindow.close();
}
Am I not able to do so because my Form is already created within an eventhandler ? Or is it just because they're in separate blocks. If so is there a way the new Form and its controls available from outside the block ?

Unable to change backcolor property in Winforms app (C#)

I am working on a project where I want to have different select-able themes that change the color of certain UI elements. I am trying to change the BackColor property of different controls (buttons, labels, panels, etc), but I an unable to actually change the BackColor. My buttons are on FlatUI, don't have a background image, and UseVisualBackgroundStyle is false. I don't get any errors when I change the BackColor but I can't see it actually changing anything.
My code for creating the one of the buttons:
this.scriptHubButton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(50)))), ((int)(((byte)(50)))));
this.scriptHubButton.FlatAppearance.BorderSize = 0;
this.scriptHubButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.scriptHubButton.Font = new System.Drawing.Font("Showcard Gothic", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.scriptHubButton.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.scriptHubButton.Location = new System.Drawing.Point(761, 416);
this.scriptHubButton.Name = "scriptHubButton";
this.scriptHubButton.Size = new System.Drawing.Size(85, 36);
this.scriptHubButton.TabIndex = 11;
this.scriptHubButton.Text = "More";
this.scriptHubButton.UseVisualStyleBackColor = false;
this.scriptHubButton.Click += new System.EventHandler(this.scriptHubButton_Click);
And here's the idea of how I am attempting to change the backcolor:
public void changeTheme(Color c)
{
scriptHubButton.BackColor = c;
}
Edit: I am trying to change this is the main form of my app while it is already being displayed...

How do I make a Output Label on Visual Studio C#

I am new at using Visual Studio as well as C#. I'm having a hard time trying to figure out how to make a output label. Does anyone know how to do this?
What I've currently been doing is dragging the label control to the text box. Then, I would delete the text from the label but then it looks like it disappeared .
first of all: this is broad question! I'll just answer according to my understanding.
Define the AutoSize of your label to False, then you can erase its text (label1.Text = String.Empty) without the label being seen as if it disappeared
I think, this is what you want,
System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
label1.Location = new System.Drawing.Point(34, 49);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(85, 23);
label1.TabIndex = 0;
label1.Text = "label1";
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

How to add a control to a added tab C#

I am creating a simple application that keeps track of coins. I have a button that creates a new tab in a tabcontrol container. I am wanting to add some textboxes to the newly added tabs. The below code adds a textbox to my main tab called "Control". I have tried playing around with that field, but it always adds it to the main page called control. How would I do this? I have the following code:
string name = txtName.Text;
//validate information
try { }
catch { }
//create new tab
string title = name;
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
//Add text boxes
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(250, 75);
tb.Name = "TextBoxName";
tb.Size = new System.Drawing.Size(184, 20);
Control.Controls.Add(tb);
//put data inside of textboxes
tb.Text = txtCoin.Text;
myTabPage.Controls.Add(tb);
You need to add the text box to the controls collection of the new tab:
//Control.Controls.Add(tb); //Replace this line
myTabPage.Controls.Add(tb);
change
Control.Controls.Add(tb)
to
tabControl1.TabPages.Last().Controls.Add(tb)
tabControl1.TabPages.OfType<TabPage>().Last().Controls.Add(tb)

Adding and removing dynamic controls Windows Forms using C#

I have three Tabs in my Windows Forms form. Depending on the selected RadioButton in the TabPages[0], I added few dynamic controls on the relevant TabPage. On the Button_Click event the controls are added, but the problem is I'm not able to remove the dynamically added controls from the other (irrelevant) TabPage.
Here's my code:
Label label235 = new Label();
TextBox tbMax = new TextBox();
label235.Name = "label235";
tbMax.Name = "txtBoxNoiseMax";
label235.Text = "Noise";
tbMax.ReadOnly = true;
label235.ForeColor = System.Drawing.Color.Blue;
tbMax.BackColor = System.Drawing.Color.White;
label235.Size = new Size(74, 13);
tbMax.Size = new Size(85, 20);
if (radioButton1.Checked)
{
label235.Location = new Point(8, 476);
tbMax.Location = new Point(138, 473);
tabControl.TabPages[1].Controls.Add(label235);
tabControl.TabPages[1].Controls.Add(tbMax);
tabControl.TabPages[2].Controls.RemoveByKey("label235");
tabControl.TabPages[2].Controls.RemoveByKey("tbMax");
}
else
{
label235.Location = new Point(8, 538);
tbMax.Location = new Point(138, 535);
tabControl.TabPages[1].Controls.RemoveByKey("label235");
tabControl.TabPages[1].Controls.RemoveByKey("tbMax");
tabControl.TabPages[2].Controls.Add(label235);
tabControl.TabPages[2].Controls.Add(tbMax);
}
Where am I making that mistake?
First of all, tbMax's name is not "tbMax", but "txtBoxNoiseMax". So for one, it won't be able to find the TextBox on RemoveByKey.
You're making new controls each time.
As lc already mentioned:
You named your TextBox variable tbMax, but you gave it the name txtBoxNoiseMax. If you take a look into the description of RemoveByKey, you'll see it works on the Name property. So you should change
tbMax.Name = "txtBoxNoiseMax";
into
tbMax.Name = "tbMax";

Categories

Resources